公告列表无缝滚动 vue + js

1. html

<div class="model_func model_notice box_shadow">
     <div class="model_title">
         <div class="tit">公告列表</div>
         <div class="add">详情</div>
     </div>
     <div class="notice_box" ref="box"  @mouseenter="onNoticeMouseenter" @mouseleave="onNoticeMouseleave">
         <div class="notice_box_warpper" ref="boxWarpper">
             <ul class="notice_cal" ref="uls" v-if="noticeList.length!==0">
                 <li v-for="item in noticeList" :key="item.id" ref="lis" @click="handleNoticeLi(item.id)">
                     <div class="tit">
                         <span class="status" :class="item.noticeType==2?'noti':'anno'">【{{item.noticeType==2?"通知":"公告"}}】</span>
                         <span class="con">{{item.noticeTitle}}</span>
                     </div>
                     <div class="text" v-html="item.noticeContent"></div>
                 </li>
             </ul>
             <ul class="notice_cal_empyt empyt" v-else>
                 <span class="icon el-icon-data-analysis"></span>
                 暂无公告~~
             </ul>
         </div>
     </div>
 </div>

2. js

data(){
	return {
		index: 0,
        timer: null,
	}
}created(){
	this.noticeRoll()
},
method:{
    noticeRoll() {
       let box = this.$refs.box,
           boxWarp = this.$refs.boxWarpper,
           ulDom = this.$refs.uls
       clearInterval(this.timer)  //清除前一个定时器
       if (boxWarp.offsetHeight > box.offsetHeight) { //列表高度超出外框高度才滚动
           boxWarp.appendChild(ulDom.cloneNode(true)) // 复制并添加ul一个节点
           this.timer = setInterval(() => {
               this.index++
               if (this.index === ulDom.offsetHeight) { //滚动距离===ul高度
                   boxWarp.appendChild(ulDom.cloneNode(true)) //添加一个ul节点
               } else if (this.index === (ulDom.offsetHeight * 2 + 30)) { // 滚动距离===2个ul高度
                   this.index = 0 //滚动距离从0 开始
                   boxWarp.lastChild.remove() //清楚最后添加得第三个ul节点
               }
               boxWarp.style.top = "-" + this.index + "px"  //滚动距离 top:-this.index,css样式添加position:relative
           }, 25);
       }
   },
   onNoticeMouseenter() {//鼠标移入公告模块
       clearInterval(this.timer) //暂停滚动
       this.noticeFlag = true
   },
   onNoticeMouseleave() { //鼠标移出公告模块
       if (this.noticeList.length !== 0 && this.timer!=null) {
           let boxWarp = this.$refs.boxWarpper
           boxWarp.lastChild.remove() //清除多余的ul节点
           this.noticeRoll() //继续滚动
           this.noticeFlag = false
       }

   },
}

3. css(scss)

.model_notice {
    height: 463px;
    overflow: hidden;
    box-sizing: border-box;
    .notice_box {
        height: calc(100% - 90px);
        padding: 0 24px;
        overflow: hidden;
        .notice_box_warpper {
            position: relative;
            .notice_cal {
                li {
                    font-size: 14px;
                    margin-bottom: 15px;
                    cursor: pointer;
                    &:hover{
                        .tit .con{
                            color: #4d21bd;
                            text-decoration: underline;
                        }
                    }
                    .tit {
                        margin-bottom: 8px;
                        .status {
                            font-weight: 700;
                            &.noti {
                                color: #f10000;
                            }
                            &.anno {
                                color: #ff8705;
                            }
                        }
                        .con {
                            font-weight: 700;
                        }
                    }
                    .text {
                        line-height: 1.5;
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Vue 列表的纵向无缝滚动,你可以使用一些插件或者自定义实现来实现这个功能。以下是一种常见的做法: 1. 使用 vue-virtual-scroller 插件:vue-virtual-scroller 是一个高性能的虚拟滚动列表插件,它可以实现纵向的无缝滚动效果。你可以使用 npm 安装该插件,并按照官方文档中的说明进行使用。 2. 自定义实现:如果你想自己实现纵向无缝滚动,你可以借助 CSS 和 JavaScript 来实现。以下是一个简单的示例: ```html <template> <div class="scroll-container"> <ul ref="list" class="scroll-list"> <li v-for="(item, index) in items" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { data() { return { items: ['Item 1', 'Item 2', 'Item 3'] // 列表数据 }; }, mounted() { this.startScroll(); }, methods: { startScroll() { const list = this.$refs.list; const height = list.offsetHeight; let top = 0; setInterval(() => { top -= 1; if (top <= -height) { top = 0; } list.style.transform = `translateY(${top}px)`; }, 20); } } }; </script> <style> .scroll-container { height: 200px; /* 设置容器高度 */ overflow: hidden; /* 隐藏溢出部分 */ } .scroll-list { padding: 0; margin: 0; list-style: none; animation: scroll 5s linear infinite; /* 使用动画实现无缝滚动 */ } @keyframes scroll { 0% { transform: translateY(0); } 100% { transform: translateY(-100%); } } </style> ``` 在上述示例中,我们通过 JavaScript 定时器和 CSS transform 属性来实现纵向的无缝滚动效果。列表项通过 translateY 属性进行垂直平移,当列表滚动到末尾时,将其重置为初始位置,从而实现无缝滚动。 希望以上信息对你有所帮助!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值