Vue 實現(xiàn)簡易多行滾動"彈幕"效果
看一下效果:
實現(xiàn)任意行數(shù)的定時翻滾效果,不使用重復標簽的方式,而是根據(jù)展示個數(shù)判斷是否缺省,并添加對應展示個數(shù)的重復項來實現(xiàn)。
Vue 的演示代碼如下:
<template> <div class="demo-comment-container"> <div class="scroll-container" :style="{height: `${height/100}rem`}" > <ul class="scroll-ul" :style="{transform: `translate3d(0, ${y/100}rem, 0)`, transition: `${transition}`}" > <div v-for="(item, index) in list" :key="index" v-bind="baseStyle.style" :bgColor="baseStyle.style.bgColor__bg" :style="{ color: rgba(baseStyle.color__nf) }" :class="['c-item-bg', 'c-item', !item.content && 'empty']" > <li class="c-item" > <div class="avatar"> <img v-if="item.content" class="avatar-item" :src="item.photo" > </div> <div v-if="item.content" class="c-content" v-html="item.content" /> </li> </div> </ul> </div> <div class="comment-entry"> <div class="f"> <div class="text-c"> <span v-if="entry.type === 1" class="text" v-text="entry.text" /> <div v-else class="img-container" > <img :src="entry.image"> </div> <i v-if="count" class="c-c" >{{ count }}</i> </div> <div> <span class="fake-input">來發(fā)表你的看法吧~</span> </div> </div> </div> </div> </template>
<script> export default { inject: ['rgba'], props: { urlConfig: { type: Object, default: function () { return {} } }, type: { type: String, default: 'hot' }, pageSize: { type: Number, default: 50 }, open: { type: Boolean, default: true }, entry: { type: Object, default: function () { return { type: 1, // 1 文字 2 自定義 text: '', image: '' } } }, number: { type: Number, default: 2 }, }, data () { return { count: 2334, dom: null, height: 0, // 單項高度 y: 0, // 每次移動距離 list: [], // 接口列表 originLength: 0, // 原始數(shù)組長度 transition: 'ease all .4s', round: 0 // 需要滾動多少輪 } }, created () { this.getCommentList() }, mounted () { this.dom = document.querySelector('.c-item') // 計算可視區(qū)域高度 this.height = 64 * this.number + (12 * (this.number - 1)) }, methods: { getCommentList () { // 接口數(shù)據(jù) const _list = [] this.originLength = _list.length const mod = this.originLength % this.number let need = this.originLength < this.number ? (this.number - this.originLength) : mod > 0 ? this.number - mod : 0 // 計算出要添加的空白元素個數(shù) this.list = _list // set empty item const empty = JSON.parse(JSON.stringify(_list[0])) empty.content = '' // 補齊空白元素 while (need) { this.list.push(empty) need-- } // 填充重復元素 let repeat = this.number let index = 0 while (repeat) { this.list.push(_list[index]) index++ repeat-- } this.round = this.list.length / this.number this.scroll() }, scroll () { let count = 1 setInterval(() => { count++ this.y -= this.height + 12 // 移動可視區(qū)域高度 + 最后一個 item 的 margin-bottom this.transition = '.4s ease all' setTimeout(() => { if (count === this.round) { count = 1 this.transition = '' this.y = 0 } }, 800) }, 2000) } } } </script>
總結
以上所述是小編給大家介紹的Vue 實現(xiàn)簡易多行滾動"彈幕",希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
欄 目:JavaScript
本文標題:Vue 實現(xiàn)簡易多行滾動"彈幕"效果
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9312.html
您可能感興趣的文章
- 04-02java后端代碼分頁 java后端實現(xiàn)分頁page
- 01-10Echarts實現(xiàn)單條折線可拖拽效果
- 01-10在Vue項目中使用Typescript的實現(xiàn)
- 01-10js實現(xiàn)上傳圖片并顯示圖片名稱
- 01-10Vue中使用Lodop插件實現(xiàn)打印功能的簡單方法
- 01-10echarts實現(xiàn)折線圖的拖拽效果
- 01-10d3.js實現(xiàn)圖形縮放平移
- 01-10小程序簡單兩欄瀑布流效果的實現(xiàn)
- 01-10H5實現(xiàn)手機拍照和選擇上傳功能
- 01-10Echarts實現(xiàn)多條折線可拖拽效果


閱讀排行
本欄相關
- 04-02javascript點線,點線的代碼
- 04-02javascript潛力,javascript強大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報,JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面
- 04-02javascript前身,javascript的前身
隨機閱讀
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子