基于vue實現(xiàn)新聞自下往上滾動效果(示例代碼)
如圖所示自下往上滾動鼠標放上暫停滾動 鼠標移出繼續(xù)滾動
一、html:
<div class="newsList" @mouseover="mouseOver" @mouseout="mouseOut"> <ul id="con1" ref="con1" :class="{ anim: animate == true }"> <li v-for="(item, key) in items" :key="key" class="newsItem"> <img src="../../assets/img/icon.png" alt="" style="margin-top:5px;width:20px;height:20px"> <span>{{ item.title }}</span> <span @click="getCode(item.qrCode)">查看></span> </li> </ul> <div class="showCode" v-if="codeShow"> <p style="text-align:right;padding-right:10px;cursor:pointer" @click="codeShow=false">x</p> <img :src="code" alt=""> <p>更多內(nèi)容可掃碼查看</p> </div> </div>
二.css動畫
.newsList { width: 90%; margin: 10px auto; height: 90px; overflow: hidden; font-size: 12px; font-family: PingFang SC; font-weight: 400; line-height: 17px; color: #ffffff; /* background-color: red; */ } .anim { transition: all 1s; margin-top: -30px; } #con1 li { list-style: none; line-height: 30px; height: 30px; }
三、js代碼
mounted() { this.timer=setInterval(this.scroll, 2000); this.$api.newsList({ limit:12, page:1 }).then(res=>{ this.items=res.data.records }) }, methods: { scroll() { this.animate = true; // 因為在消息向上滾動的時候需要添加css3過渡動畫,所以這里需要設(shè)置true setTimeout(() => { // 這里直接使用了es6的箭頭函數(shù),省去了處理this指向偏移問題,代碼也比之前簡化了很多 this.items.push(this.items[0]); // 將數(shù)組的第一個元素添加到數(shù)組的 this.items.shift(); //刪除數(shù)組的第一個元素 this.animate = false; // margin-top 為0 的時候取消過渡動畫,實現(xiàn)無縫滾動 }, 1000); }, getCode(qrCode){ this.code=qrCode this.codeShow=true }, mouseOver(){ console.log('鼠標懸停') // this.animate = false; clearInterval(this.timer) }, mouseOut(){ // this.animate = true; this.timer=setInterval(this.scroll, 2000); } },
到此這篇關(guān)于vue新聞自下往上滾動效果的文章就介紹到這了,更多相關(guān)vue新聞滾動效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn)方法
這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05vue前端HbuliderEslint實時校驗自動修復(fù)設(shè)置
這篇文章主要為大家介紹了vue前端中Hbulider中Eslint實時校驗自動修復(fù)設(shè)置操作過程,有需要的朋友可以借鑒參考下希望能夠有所幫助2021-10-10Vue3+Canvas實現(xiàn)坦克大戰(zhàn)游戲(二)
本文主要給大家講解一下子彈擊中物體、物體銷毀、敵方坦克構(gòu)建生成、運動算法、爆炸效果、以及障礙物的生成,感興趣的小伙伴可以了解一下2022-03-03Vue?報錯-4058?ENOENT:no?such?file?or?directory的原因及解決方法
Vue?報錯-4058?ENOENT:?no?such?file?or?directory的原因和解決辦法,關(guān)于為什么為會報這個錯誤,按照字面意思的理解就是沒有找到這個文件或這個路徑,說明是路徑不對,本文給大家分享解決方案,感興趣的朋友一起看看吧2023-10-10vue使用$attrs和$listeners多級組件嵌套傳遞數(shù)據(jù)
這篇文章主要為大家介紹了vue使用$attrs和$listeners多級組件嵌套傳遞數(shù)據(jù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08