Home >Web Front-end >CSS Tutorial >How to achieve the effect of scrolling up a single line of text (code attached)

How to achieve the effect of scrolling up a single line of text (code attached)

不言
不言Original
2018-08-06 14:33:013344browse

This article introduces to you how to achieve the effect of scrolling up a single line of text (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I am currently working on an event page, and I need a single line of text to scroll up to display the award announcement.

The effect is as follows:

How to achieve the effect of scrolling up a single line of text (code attached)

Without further ado, just paste the code directly below.

html code is as follows:

 <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 code is as follows:

 .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 code is as follows:

 // data下
 noticeTop: 0, // 公告top值
 isActive:true, // 是否显示transitionTop动画
 timer: null, // 公告滚动定时器
 noticeList: [
   {
     phone:'135****1234',
     prizeName:'50元还款券'
   },
   {
     phone:'135****1234',
     prizeName:'60元还款券'
   },
   {
     phone:'135****1234',
     prizeName:'70元还款券'
   }
 ], // 公告列表
 
 // 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();

What needs to be explained is:
1. The project is based on vue's syntax
2. A delay is added when scrolling to the bottom and returning. It is to be able to scroll to the last one and then return from the last one to the first one.                                      

# Related article Recommendation:

# CSS3 How to achieve page rolling animation special effects?

How to use css3 to create nice-looking buttons?

The above is the detailed content of How to achieve the effect of scrolling up a single line of text (code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn