新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
vue.js怎么實(shí)現(xiàn)回到頂部動(dòng)畫效果-創(chuàng)新互聯(lián)
這篇文章將為大家詳細(xì)講解有關(guān)vue.js怎么實(shí)現(xiàn)回到頂部動(dòng)畫效果,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
最近使用vue.js做了幾個(gè)頁面,其中有一個(gè)回到頂部的動(dòng)畫效果記錄下。
html如下:
回到頂部 12345回到 頂部
backTop.css如下:
* { margin: 0px; padding: 0px; border: 0; } html, body { width: 100%; height: 100%; position: relative; background: #F2F2F2; overflow-x: hidden; overflow-y: auto; z-index: 1; } [v-cloak] { display: none; } .back-to-top { position: fixed; bottom: 5px; right: 20px; z-index: 100; border-radius: 5px; box-shadow: 0px 0px 2px #222; padding: 8px 10px; cursor: pointer; } .back-to-top:hover { background: #5AC4D1; } .back-to-top:hover span { color: white; } .back-to-top span { display: block; } .page { width: 100%; height: 400px; line-height: 400px; text-align: center; } .page:nth-child(odd) { background: white; }
backTop.js 如下:
var backTopVue = new Vue({ el : "#back-to-top", data : { //是否顯示回到頂部 backTopShow : false, // 是否允許操作返回頂部 backTopAllow : true, // 返回頂部所需時(shí)間 backSeconds : 100, // 往下滑動(dòng)多少顯示返回頂部(單位:px) showPx : 200 }, mounted : function() { window.addEventListener("scroll", this.backTopShowOperate, true); }, methods : { backTopShowOperate : function() { if (!this.backTopAllow) return; if (document.body.scrollTop > this.showPx) { this.backTopShow = true; } else { this.backTopShow = false; } }, backToTop : function() { if (!this.backTopAllow) return; this.backToTopShow = false; this.backTopAllow = false; var step = document.body.scrollTop / this.backSeconds; var backTopInterval = setInterval(function() { if (document.body.scrollTop > 0) { document.body.scrollTop -= step; } else { backTopVue.backTopAllow = true; clearInterval(backTopInterval); } }, 1); } } });
關(guān)于“vue.js怎么實(shí)現(xiàn)回到頂部動(dòng)畫效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
分享名稱:vue.js怎么實(shí)現(xiàn)回到頂部動(dòng)畫效果-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.ef60e0e.cn/article/ddjjes.html