1. 程式人生 > >微信小程式實現跑馬燈效果(完整程式碼)

微信小程式實現跑馬燈效果(完整程式碼)

在微信小程式 裡實現跑馬燈效果,類似滾動字幕或者滾動廣告之類的,使用簡單的CSS樣式控制,沒用到JS,效果如下圖:

微信小程式跑馬燈效果

Wxml程式碼:

<!--跑馬燈 Linyufan.com-->
<view class="marquee_container" style="--marqueeWidth--:-12em">
    <view class="marquee_text">一個人活著就是為了讓更多的人更好的活著!</view>
</view>
<!--跑馬燈-->

Wxss程式碼:

/*首頁跑馬燈效果*/
@keyframes around {
  from {
   margin-left: 100%;
  }
  to {
   /* var接受傳入的變數 */
   margin-left: var(--marqueeWidth--);
  }
 }
 
.marquee_container{
  background-color: #fe4655;
  height: 50rpx;
  line-height: 44rpx;
  position: relative;
  width: 100%;
  margin-top:0rpx;
}
.marquee_container:hover{
  /* 不起作用 */
  animation-play-state: paused;
}
.marquee_text{
  color:#fff;
  font-size: 28rpx;
  display: inline-block;
  white-space: nowrap;
  animation-name: around;
  animation-duration: 10s;  /*過渡時間*/
  animation-iteration-count: infinite;
  animation-timing-function:linear;
}
/*首頁跑馬燈效果*/

林羽凡小程式