字体滚动实现效果的几种方式

一、 第一种方式

         <ul
            :class="{ marquee_top: animate }"
            @mouseenter="Stop()"
            @mouseleave="Up()"
          >
            <li
              v-for="(item, index) in list"
              :key="index"
            >
              <span>{{ item.noticeTitle }}</span>
            </li>
          </ul>
          
          data(){
            return{
               animate:false,
               list:[{
                 noticeTitle:'今天是个好日子!'
               },{
                 noticeTitle:'周三意味着......'
               }{
                 noticeTitle:'在上2天班就双休啦!'
               } ]
            }
          },
         created: {
			 this.timer = setInterval(this.showMarquee, 1500)
		},
		methods:{
		   showMarquee: function () {//字体滚动
		      this.animate = true
		      setTimeout(() => {
		        this.notList.push(this.notList[0])
		        this.notList.shift()
		        this.animate = false
		      }, 500)
		    },
		    //鼠标移上去停止
		    Stop() {
		      clearInterval(this.timer)
		    },
		    Up() {
		      this.timer = setInterval(this.showMarquee, 1500)
		    },
        }//当然如果你觉得滚动不够完美,那就在加上动画效果吧!

二、 第二种方式

<div class="inner-container">
  <p class="text" v-for="(text, index) in arr" :key="index">{{text}}</p>
</div>

.inner-container {
  animation: myMove 5s linear infinite;
  animation-fill-mode: forwards;
}
  /*文字无缝滚动*/
@keyframes myMove {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-150px);
  }
}

data() {
  return {
    arr: [
    '1 创作不易,感谢支持',
    '2 今天可乐很开心!',
    '3 因为看到大佬的到来',
    '1 创作不易,感谢支持', 
    ],
  }
},

当动画结束时通过:       将动画重置为第一帧,这样就能够实现无缝的播放了
animation-fill-mode: forwards;

三、第三种方式marquee标签:

(1)几个重要属性

1.direction:滚动方向(包括4个值:up、down、left、right)
        说明:up:从下向上滚动;down:从上向下滚动;left:从右向左滚动;right:从左向右滚动。
        语法: <marquee direction="滚动方向">...</marquee>
        
2.behavior:滚动方式(包括3个值:scroll、slide、alternate)
        说明:scroll:循环滚动,默认效果;slide:只滚动一次就停止;alternate:来回交替进行滚动。
        语法:<marquee behavior="滚动方式">...</marquee>
        
3.scrollamount:滚动速度(滚动速度是设置每次滚动时移动的长度,以像素为单位)
        语法:<marquee scrollamount="5">...</marquee>
        
4.scrolldelay:设定滚动两次之间的延迟时间,值大了会有一步一停顿的效果(设置滚动的时间间隔,单位是毫秒)
        语法:<marquee scrolldelay="100">...</marquee>
        
5.loop:设定滚动循环的次数(默认值是-1,滚动会不断的循环下去)
        语法:<marquee loop="2">...</marquee>   
        <marquee loop="-1" bgcolor="#CCCCCC">我会不停地走。</marquee>
        <marquee loop="2" bgcolor="#CCCCCC">我只走两次哦</marquee>  
6.width、height:设定滚动字幕的宽度、高度
        语法:<marquee width="500" height="200">...</marquee>

7.bgcolor:设定滚动字幕的背景颜色(可以是颜色值,也可以是rgb()或rgba()函数)
        语法:<marquee bgcolor="rgba(0,0,0,0.2)">...</marquee>

8.hspace、vspace:空白空间(相当于设置margin值)
         说明:hspace:设定活动字幕里所在的位置距离父容器水平边框的距离,如hspace=“10”,即等同于:margin:0 10px;
                     vspace:设定活动字幕里所在的位置距离父容器垂直边框的距离,如vspace=“10”,即等同于:margin:10px 0;
         语法:<marquee hspace="10"  vspace="10">...</marquee>(等同于:margin:10px;)

9.align:设定滚动字幕内容的对齐方式(包括9个值:absbottom、absmiddle、baseline、bottom、left、middle、right、  
                                     texttop、top)
          说明:absbottom:绝对底部对齐(与g、p等字母的最下端对齐)       
                             absmiddle:绝对中央对齐                    
                                   baseline:底线对齐                          
                                   bottom:底部对齐(默认)                      
                                 left:左对齐                          middle:中间对齐                         
                                  right:右对齐                          texttop:顶线对齐                         
                                   top:顶部对齐
             语法:<marquee align="对齐方式">...</marquee>

10.face:设定滚动字幕的文字字体
            语法:<marquee font="楷体_GB2312"></marquee>

11.color:设定滚动字幕的文字颜色
            语法:<marquee color="#333"></marquee>

12.size:设定滚动字幕的文字字号
            语法:<marquee size="3"></marquee>

13.STRONG:设定滚动字幕的文字加粗
            语法:<marquee STRONG></marquee>   
   

(2)常用到的两个事件   
  onMouseOut="this.start()" 用来设置鼠标移出该区域时继续滚动      
  onMouseOver="this.stop()" 用来设置鼠标移入该区域时停止滚动
  <marquee onMouseOut="this.start()" onMouseOver="this.stop()">marquee常用到的两个事件</marquee>   
                                        
完整代码如下:

<marquee id="affiche" align="left" behavior="scroll" bgcolor="#FF0000" direction="up" 
height="300" width="200" hspace="50" vspace="20" loop="-1" scrollamount="10" 
scrolldelay="100" onMouseOut="this.start()" onMouseOver="this.stop()">

尽管<marquee>参数不少,但毕竟不能实现复杂的和自定义的特殊跑马灯效果,而且还有浏览器限
制,所以我们更多情况下会采用JavaScript来实现跑马灯滚动效果。

  <marquee behavior="scroll" contenteditable="true" onstart="this.firstChild.innerHTML+= this.firstChild.innerHTML;
    " scrollamount="3" width="100" οnmοuseοver="this.stop();"     οnmοuseοut="this.start();">    
              这里是要滚动的内容  
 </marquee>                           

四、 uni 你用swiper时间无限循环滚动

<template>
    <view>
        <view class="scroll_box"> 
            <swiper class="swiper" circular="true" vertical="true" display-multiple-items="4" //数字不要大于要循环的长度
             :autoplay="autoplay" :interval="interval" :duration="duration">
                <swiper-item v-for="(item,index) in 5" :key="index">
                    <view class="swiper-item uni-bg-green">我是循环数据中索引为{{index}}的数据</view>
                </swiper-item>
            </swiper>
        </view>
    </view>
</template>
<script>
    export default{
        name:'',
        data(){
            return{
                autoplay:true,
                interval:1000,
                duration:2000,
            }
        },
    }
</script>
<style>
    .scroll_box{
        background: pink;
        margin: 30rpx;
        padding:0 20rpx
        border-radius: 10rpx;
    }
    .swiper{
            height: 100rpx;
    }
</style>

在这里插入图片描述

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值