首頁 > web前端 > css教學 > 主體

如何實現單行文字向上捲動的效果(附程式碼)

不言
發布: 2018-08-06 14:33:01
原創
3227 人瀏覽過

這篇文章要跟大家介紹的內容是關於如何實現單行文字向上滾動的效果(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

最近在做一個活動頁,需要一個單行文字向上滾動的效果來展示獲獎公告。

效果如下:

如何實現單行文字向上捲動的效果(附程式碼)

廢話不多說,下面直接貼上程式碼。

html程式碼如下:

 <div class="notice">
   <img src="./img/notice.png" alt="">
   <div class="wrap">
     <ul :style="{top: noticeTop + &#39;rem&#39;}" :class="{transitionTop: isActive}">
       <li v-for="(item, index) in noticeList" :key="index">{{item.phone}}抽中{{item.prizeName}}</li>
       <li v-if="noticeLen > 0">{{noticeList[0].phone}}抽中{{noticeList[0].prizeName}}</li>
       <li v-if="noticeLen === 1">{{noticeList[0].phone}}抽中{{noticeList[0].prizeName}}</li>
       <li v-if="noticeLen === 0">获奖公告</li>
     </ul>
   </div>
 </div>
登入後複製

less程式碼如下:
##

 .notice{
     display: flex;
     justify-content: center;
     padding-bottom: .26rem;
     img{
       width: .3rem;
       height: .24rem;
     }
     .wrap{
       position: relative;
       height:.3rem;
       overflow: hidden;
       margin-left: .15rem;
       font-size: .24rem;
       color: #391b03;
     }
     ul{
       position: relative;
       top: -.3rem;
       li{
         height: .3rem;
         line-height: .3rem;
       }
     }
     .transitionTop{
       transition: top 200ms ease-in-out;
     }
 }
登入後複製

js程式碼如下:

 // data下
 noticeTop: 0, // 公告top值
 isActive:true, // 是否显示transitionTop动画
 timer: null, // 公告滚动定时器
 noticeList: [
   {
     phone:&#39;135****1234&#39;,
     prizeName:&#39;50元还款券&#39;
   },
   {
     phone:&#39;135****1234&#39;,
     prizeName:&#39;60元还款券&#39;
   },
   {
     phone:&#39;135****1234&#39;,
     prizeName:&#39;70元还款券&#39;
   }
 ], // 公告列表
 
 // computed下
 noticeLen(){ // 公告列表长度
     return this.noticeList.length;
 }
 //methods下
 noticeScroll(){// 公告滚动,定时改变公告的top值
     if(this.noticeLen > 0){
       let index =1,
           len = this.noticeLen === 1 ? 3 : (this.noticeLen + 1);
       this.timer = setInterval(() => {
         this.isActive = true;
         this.noticeTop = -3 * index / 10;
         index ++;
         if(index === len){// 滚动到底部时返回
           let delayTime = setTimeout(() => {
             this.isActive = false;
             this.noticeTop = 0;
             index = 1;
             clearTimeout(delayTime);
           }, 1000);
         }
       }, 3000);
     }
 }
 //调用
 this.noticeScroll();
登入後複製
需要說明的是:

1.專案是基於vue的語法
2.滾動到底部返回時加了個延遲,是為了能滾動到最後一個,然後從最後一個回到第一個。                                                

相關文章中推薦:

css3 為頁面滾動動畫特效?

怎麼用css3製作樣式好看的按鈕?

以上是如何實現單行文字向上捲動的效果(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!