vue實現(xiàn)滾動鼠標滾輪切換頁面
本文實例為大家分享了vue實現(xiàn)滾動鼠標滾輪切換頁面的具體代碼,供大家參考,具體內(nèi)容如下
新項目產(chǎn)品被甲方的要求逼瘋了,大概返稿了100+次吧,最后甲方網(wǎng)上找了個他們認為的比較有科技感的模板,讓我們照著寫,首頁就是類似于縱向走馬燈,鼠標滾動切換,一次切換一整屏的效果。之前沒接觸過,寫了個簡單的demo,僅作為學習筆記。
其實原理很簡單,就是把所有頁面放在一個div中,然后滾動的時候改變外層div的top即可。
因為滾動條監(jiān)聽事件是實時的,所以要加上節(jié)流來防止頁面切換太快速,我這控制在1.5s才能切換一頁。
其實vue不應(yīng)該操作dom,應(yīng)該用數(shù)據(jù)來渲染虛擬dom,但是有些地方暫時沒找到合適的方法,還是用的dom操作。
<template>
<div id="wrap" :style="{ height: screenHeight + 'px' }">
<div id="main" :style="{ top: nowTop + 'px' }">
<ul id="pageUl" type="circle">
<li id="pageUlLi1" class="pageUlLi" :class="{'active': curIndex == 1}"> </li>
<li id="pageUlLi2" class="pageUlLi" :class="{'active': curIndex == 2}"> </li>
<li id="pageUlLi3" class="pageUlLi" :class="{'active': curIndex == 3}"> </li>
<li id="pageUlLi4" class="pageUlLi" :class="{'active': curIndex == 4}"> </li>
<li id="pageUlLi5" class="pageUlLi" :class="{'active': curIndex == 5}"> </li>
</ul>
<div style="background-color: #1b6d85" id="page1" class="page"></div>
<div style="background-color: #5cb85c" id="page2" class="page"></div>
<div style="background-color: #8a6d3b" id="page3" class="page"></div>
<div style="background-color: #337ab7" id="page4" class="page"></div>
<div style="background-color: #66512c" id="page5" class="page"></div>
</div>
</div>
</template>
<script>
export default {
name: 'Home',
data(){
return{
screenWeight: 0, // 屏幕寬度
screenHeight: 0, // 屏幕高度
index: 1, // 用于判斷翻頁
curIndex: 1, // 當前頁的index
startTime: 0, // 翻屏起始時間
endTime: 0, // 上一次翻屏結(jié)束時間
nowTop: 0, // 翻屏后top的位置
pageNum: 0, // 一共有多少頁
main: Object,
obj: Object
}
},
mounted(){
this.screenWeight = document.documentElement.clientWidth;
this.screenHeight = document.documentElement.clientHeight;
this.main = document.getElementById("main");
this.obj = document.getElementsByTagName("div");
for (let i = 0; i < this.obj.length; i++) {
if (this.obj[i].className == 'page') {
this.obj[i].style.height = this.screenHeight + "px";
}
}
this.pageNum = document.querySelectorAll(".page").length;
// 瀏覽器兼容
if ((navigator.userAgent.toLowerCase().indexOf("firefox") != -1)) {
document.addEventListener("DOMMouseScroll", this.scrollFun, false);
} else if (document.addEventListener) {
document.addEventListener("mousewheel", this.scrollFun, false);
} else if (document.attachEvent) {
document.attachEvent("onmousewheel", this.scrollFun);
} else {
document.onmousewheel = this.scrollFun;
}
},
methods:{
scrollFun(event) {
this.startTime = new Date().getTime();
// mousewheel事件中的 “event.wheelDelta” 屬性值:返回的如果是正值說明滾輪是向上滾動
// DOMMouseScroll事件中的 “event.detail” 屬性值:返回的如果是負值說明滾輪是向上滾動
let delta = event.detail || (-event.wheelDelta);
// 如果當前滾動開始時間和上次滾動結(jié)束時間的差值小于1.5s,則不執(zhí)行翻頁動作,這樣做是為了實現(xiàn)類似節(jié)流的效果
if ((this.startTime - this.endTime) > 1500) {
if (delta > 0 && parseInt(this.main.offsetTop) >= -(this.screenHeight * (this.pageNum - 2))) {
// 向下滾動
this.index++;
this.toPage(this.index);
}else if (delta < 0 && parseInt(this.main.offsetTop) < 0) {
// 向上滾動
this.index--;
this.toPage(this.index);
}
// 本次翻頁結(jié)束,記錄結(jié)束時間,用于下次判斷
this.endTime = new Date().getTime();
}
},
// 翻頁
toPage(index) {
if (index != this.curIndex) {
let delta = index - this.curIndex;
this.nowTop = this.nowTop - delta * this.screenHeight;
this.curIndex = index;
}
}
}
}
</script>
<style>
html, body {
height: 100%;
}
body, ul, li, a, p, div {
/*慎刪*/
padding: 0px;
margin: 0px;
}
#wrap {
overflow: hidden;
width: 100%;
}
#main {
position: relative;
transition:top 1.5s;
}
.page {
/*謹刪*/
width: 100%;
margin: 0;
}
#pageUl {
position: fixed;
right: 10px;
bottom: 50%;
}
.active{
color: red;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3?setup語法糖中獲取slot插槽的dom對象代碼示例
slot元素是一個插槽出口,標示了父元素提供的插槽內(nèi)容將在哪里被渲染,這篇文章主要給大家介紹了關(guān)于vue3?setup語法糖中獲取slot插槽的dom對象的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-04-04
在Vue3中處理異步API調(diào)用并更新表單數(shù)據(jù)的方法示例
這篇文章主要介紹了如何在Vue3中處理異步API調(diào)用并更新表單數(shù)據(jù)(附Demo),文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-06-06
vue路由$router.push()使用query傳參的實際開發(fā)使用
在vue項目中我們用函數(shù)式編程this.$router.push跳轉(zhuǎn),用query傳遞一個對象時要把這個對象先轉(zhuǎn)化為字符串,然后在接收的時候要轉(zhuǎn)化為對象,下面這篇文章主要給大家介紹了關(guān)于vue路由$router.push()使用query傳參的實際開發(fā)使用,需要的朋友可以參考下2022-11-11
vue.js添加一些觸摸事件以及安裝fastclick的實例
今天小編就為大家分享一篇vue.js添加一些觸摸事件以及安裝fastclick的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue.js結(jié)合Ueditor富文本編輯器的實例代碼
本篇文章主要介紹了Vue.js結(jié)合Ueditor的項目實例代碼,這里整理了詳細的代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07
element el-table實現(xiàn)多級表頭的代碼
多級表頭的作用與優(yōu)勢,多級表頭能夠清晰地展示數(shù)據(jù)的層次結(jié)構(gòu),幫助我們更好地理解數(shù)據(jù)之間的關(guān)系,下面通過本文給大家介紹element el-table實現(xiàn)多級表頭的代碼,感興趣的朋友跟隨小編一起看看吧2024-04-04
vue3中emit('update:modelValue')使用簡單示例
這篇文章主要給大家介紹了關(guān)于vue3中emit('update:modelValue')使用的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-09-09

