VUE 实现公告无缝循环滚动

功能说明:

VUE 实现公告无缝向左滚动,当公告的宽度大于当前视图宽度的时候再向左滚动,否则不滚动,当鼠标移入时停止滚动,移开时继续滚动,公告获取后台返回的一个数组,将链接转换为HTML

html 复制代码
<style lang="scss">
.notice{ margin-top:24px; height:50px; overflow: hidden; position: relative;
   .notice-con{  white-space:nowrap; flex:1; height:50px; overflow: hidden; margin-left:10px; box-sizing: border-box;
     ul{ height:50px; line-height:50px; display: inline-block; white-space: nowrap; } 
   }
}
</style>

<template>
<div class="notice">
    <div class="notice-con" id="box1" :scrollLeft="scrollLeft" @mouseenter="noticeStop" @mouseleave="startScroll">            
             <ul id="item1" v-html="noticeText"></ul>
             <ul id="item2" v-html="noticeText" v-if="isScroll"></ul>
   </div>    
</div>

</template>

<script>
import { getNotice } from '@/api/index'
export default {
    data(){
        return{
          noticeList:[],
          timer:null,
          scrollLeft:0,        
          noticeText:'',
          isScroll:false,
        }
    },

    mounted() { 
       this.getNotice();  
    },
    
    methods: {       
        

        startScroll(){
          this.noticeScroll('box1','item1', 20, this.scrollLeft);   
        },

        //获取公告
        getNotice(){
                this.noticeList = [
                  {id:1, content:'公告内容公告内容公告[链接|https://www.baidu.com]内容公告内容公告内容公告内容公告内容'},
                  {id:1, content:'公告内容公告内容公告内容公告内容公告内容公告内容公告内容'},
                  {id:1, content:'公告内容公告内容公告内容公告内容公告内容公告内容公告内容'}   
                ]
                var regex = /\[(.*?)\|(https?:\/\/.*?)\]/g;
                this.noticeList.forEach(res=>{
                    let content = res.content.replace(regex, function(match, text, url) {
                        return '<a href="' + url + '" target="_blank" style="color:#2F80ED; text-decoration: underline;">' + text + '</a>';
                    });
                    this.noticeText += content + ';';
                })             
                this.$nextTick(()=>{
                    var box = document.getElementById('box1');	
                    var ul1 = document.getElementById('item1');
                    this.isScroll = ul1.offsetWidth > box.offsetWidth;
                    if(this.isScroll){ 
                      this.noticeScroll('box1','item1', 20); 
                    }
                })
        },

        //滚动公告
        noticeScroll(box1, item1, time, scrollLeft=0){
            var box = document.getElementById(box1);	
            var ul1 = document.getElementById(item1);          
            if(box==null||ul1==null){ return; }           
            box.scrollLeft = scrollLeft;    
            this.scrollLeft = 0;           
            this.timer = setInterval(()=>{
                if (box.scrollLeft >= ul1.scrollWidth) {
                    box.scrollLeft = 0;
                    this.scrollLeft = 0;
                } else {
                    box.scrollLeft++;
                    this.scrollLeft = box.scrollLeft 
                }                       
            }, time);	
        },

        noticeStop(){
           clearInterval(this.timer);
        },
    },
}
</script>
相关推荐
祈澈菇凉1 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w2 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好2 小时前
css文本属性
前端·css
qianmoQ2 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
记得早睡~2 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
zhoupenghui1682 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces3 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
庸俗今天不摸鱼3 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
bubusa~>_<3 小时前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js
yanglamei19623 小时前
基于Python+Django+Vue的旅游景区推荐系统系统设计与实现源代码+数据库+使用说明
vue.js·python·django