VUE屏幕整體滾動(滑動或滾輪)原生方法舉例
前言
一年嗖的一下兒就過去了,最近幾年很流行搞年終總結(jié),因此也研究了一下相關(guān)的內(nèi)容,主要記錄一下手機端與電腦端分辨通過滑動與滾輪使得整個屏幕滾動的效果
一、基礎(chǔ)函數(shù)
不論是使用滑動還是滾輪的方式基礎(chǔ)的轉(zhuǎn)換邏輯是相通的
1、頁面部分
首先完成頁面部分的搭建,@mousewheel、@DOMMouseScroll主要是為了監(jiān)聽滾輪的事件。@touchstart、@touchend、@touchmove為觸摸事件。
<template>
<div class="fullPage" ref="fullPage">
<div
class="fullPageContainer"
ref="fullPageContainer"
@mousewheel="mouseWheelHandle"
@DOMMouseScroll="mouseWheelHandle"
@touchstart="handleTouchstart"
@touchend="handleTouchend"
@touchmove="handleTouchmove"
>
<div class="section section1">1</div>
<div class="section section2">2</div>
<div class="section section3">3</div>
<div class="section section4">4</div>
</div>
</div>
</template>
<style scoped lang="scss">
.fullPage {
height: 100vh; //一定要設(shè)置,保證占滿
overflow: hidden; //一定要設(shè)置,多余的先隱藏
background-color: rgb(189, 211, 186);
}
.fullPageContainer {
width: 100%;
height: 100vh;
transition: all linear 0.5s;
}
.section {
width: 100%;
height: 100vh;
background-position: center center;
background-repeat: no-repeat;
}
//下面的只是為了區(qū)分每個頁面的背景顏色
.section1 {
background-color: rgb(189, 211, 186);
}
.section2 {
background-color: rgb(44, 48, 43);
}
.section3 {
background-color: rgb(116, 104, 109);
}
.section4 {
background-color: rgb(201, 202, 157);
}
</style>
2、頁面切換使用的函數(shù)
全局變量
data() {
return {
fullpage: {
current: 1, // 當(dāng)前的頁面編號
isScrolling: false, // 是否在滾動,是為了防止?jié)L動多頁,需要通過一個變量來控制是否滾動
deltaY: 0 // 返回鼠標(biāo)滾輪的垂直滾動量,保存的鼠標(biāo)滾動事件的deleteY,用來判斷是往下還是往上滾
},
startTime: undefined, // 記錄觸摸開始的時間
startX: undefined, // 記錄觸摸開始的X坐標(biāo),本次主要實現(xiàn)的是上下滑動,所以當(dāng)前坐標(biāo)不做強制要求
startY: undefined, // 記錄觸摸開始的Y坐標(biāo)
};
},
實際的滾動方法
// 滾動事件
move(index) {
this.fullpage.isScrolling = true; // 為了防止?jié)L動多頁,需要通過一個變量來控制是否滾動
this.directToMove(index); //執(zhí)行滾動
setTimeout(() => { //這里的動畫是1s執(zhí)行完,使用setTimeout延遲1s后解鎖
this.fullpage.isScrolling = false;
}, 1010);
},
// 執(zhí)行滾動
directToMove(index) {
let height = this.$refs["fullPage"].clientHeight; //獲取屏幕的寬度
let scrollPage = this.$refs["fullPageContainer"]; // 獲取執(zhí)行tarnsform的元素
let scrollHeight; // 計算滾動的告訴,是往上滾還往下滾
scrollHeight = -(index - 1) * height + "px";
scrollPage.style.transform = `translateY(${scrollHeight})`;
this.fullpage.current = index;
}
上下切換事件
// 往下切換
next() {
let len = 4; // 頁面的個數(shù)
if (this.fullpage.current + 1 <= len) { // 如果當(dāng)前頁面編號+1 小于總個數(shù),則可以執(zhí)行向下滑動
this.fullpage.current += 1; // 頁面+1
this.move(this.fullpage.current); // 執(zhí)行切換
}
},
// 往上切換
pre() {
if (this.fullpage.current - 1 > 0) { // 如果當(dāng)前頁面編號-1 大于0,則可以執(zhí)行向下滑動
this.fullpage.current -= 1;// 頁面+1
this.move(this.fullpage.current);// 執(zhí)行切換
}
},
二、鼠標(biāo)的滾輪控制實現(xiàn)切換
// 監(jiān)聽鼠標(biāo)監(jiān)聽
mouseWheelHandle(event) {
// 添加冒泡阻止
let evt = event || window.event;
if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.returnValue = false;
}
if (this.fullpage.isScrolling) { // 判斷是否可以滾動
return false;
}
let e = event.originalEvent || event;
this.fullpage.deltaY = e.deltaY || e.detail; // Firefox使用detail
if (this.fullpage.deltaY > 0) {
this.next();
} else if (this.fullpage.deltaY < 0) {
this.pre();
}
},
三、移動端觸摸實現(xiàn)切換
觸摸事件相當(dāng)于滾動事件來說更為復(fù)雜,首先要移除手指滑動時對屏幕的影響,否則很容易出現(xiàn)白邊,影響視覺效果。然后需要記錄移動的距離是否有效,移動的事件是否過長等等…
// 清除觸摸事件
handleTouchmove(event) {
event.preventDefault()
},
//手指按下屏幕
handleTouchstart(event) {
this.startTime = Date.now()
this.startX = event.changedTouches[0].clientX
this.startY = event.changedTouches[0].clientY
},
//手指離開屏幕
handleTouchend(event) {
const endTime = Date.now()
const endX = event.changedTouches[0].clientX
const endY = event.changedTouches[0].clientY
//判斷按下的時長
if (endTime - this.startTime > 2000) {
return
}
//滑動的方向
let direction = "";
//先判斷用戶滑動的距離,是否合法,合法:判斷滑動的方向 注意 距離要加上絕對值
if (Math.abs(endY - this.startY) > 10) {
//滑動方向
direction = endY - this.startY > 0 ? "down" : "up"
} else {
return
}
//用戶做了合法的滑動操作
// console.log('方向'+direction)
if (direction === 'up') {
this.next();
}
if (direction === 'down') {
this.pre();
}
}
總結(jié)
最后,將全部頁面文件貼一下,方便大家搬運.
<template>
<div class="fullPage" ref="fullPage">
<div
class="fullPageContainer"
ref="fullPageContainer"
@mousewheel="mouseWheelHandle"
@DOMMouseScroll="mouseWheelHandle"
@touchstart="handleTouchstart"
@touchend="handleTouchend"
@touchmove="handleTouchmove"
>
<div class="section section1">1</div>
<div class="section section2">2</div>
<div class="section section3">3</div>
<div class="section section4">4</div>
</div>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
fullpage: {
current: 1, // 當(dāng)前的頁面編號
isScrolling: false, // 是否在滾動,是為了防止?jié)L動多頁,需要通過一個變量來控制是否滾動
deltaY: 0 // 返回鼠標(biāo)滾輪的垂直滾動量,保存的鼠標(biāo)滾動事件的deleteY,用來判斷是往下還是往上滾
},
startTime: undefined, // 記錄觸摸開始的時間
startX: undefined, // 記錄觸摸開始的X坐標(biāo),本次主要實現(xiàn)的是上下滑動,所以當(dāng)前坐標(biāo)不做強制要求
startY: undefined, // 記錄觸摸開始的Y坐標(biāo)
};
},
methods: {
// 往下切換
next() {
let len = 4; // 頁面的個數(shù)
if (this.fullpage.current + 1 <= len) { // 如果當(dāng)前頁面編號+1 小于總個數(shù),則可以執(zhí)行向下滑動
this.fullpage.current += 1; // 頁面+1
this.move(this.fullpage.current); // 執(zhí)行切換
}
},
// 往上切換
pre() {
if (this.fullpage.current - 1 > 0) { // 如果當(dāng)前頁面編號-1 大于0,則可以執(zhí)行向下滑動
this.fullpage.current -= 1;// 頁面+1
this.move(this.fullpage.current);// 執(zhí)行切換
}
},
// 滾動事件
move(index) {
this.fullpage.isScrolling = true; // 為了防止?jié)L動多頁,需要通過一個變量來控制是否滾動
this.directToMove(index); //執(zhí)行滾動
setTimeout(() => { //這里的動畫是1s執(zhí)行完,使用setTimeout延遲1s后解鎖
this.fullpage.isScrolling = false;
}, 1010);
},
// 執(zhí)行滾動
directToMove(index) {
let height = this.$refs["fullPage"].clientHeight; //獲取屏幕的寬度
let scrollPage = this.$refs["fullPageContainer"]; // 獲取執(zhí)行tarnsform的元素
let scrollHeight; // 計算滾動的告訴,是往上滾還往下滾
scrollHeight = -(index - 1) * height + "px";
scrollPage.style.transform = `translateY(${scrollHeight})`;
this.fullpage.current = index;
},
// 監(jiān)聽鼠標(biāo)監(jiān)聽
mouseWheelHandle(event) {
// 添加冒泡阻止
let evt = event || window.event;
if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.returnValue = false;
}
if (this.fullpage.isScrolling) { // 判斷是否可以滾動
return false;
}
let e = event.originalEvent || event;
this.fullpage.deltaY = e.deltaY || e.detail; // Firefox使用detail
if (this.fullpage.deltaY > 0) {
this.next();
} else if (this.fullpage.deltaY < 0) {
this.pre();
}
},
// 清除觸摸事件
handleTouchmove(event) {
event.preventDefault()
},
//手指按下屏幕
handleTouchstart(event) {
this.startTime = Date.now()
this.startX = event.changedTouches[0].clientX
this.startY = event.changedTouches[0].clientY
},
//手指離開屏幕
handleTouchend(event) {
const endTime = Date.now()
const endX = event.changedTouches[0].clientX
const endY = event.changedTouches[0].clientY
//判斷按下的時長
if (endTime - this.startTime > 2000) {
return
}
//滑動的方向
let direction = "";
//先判斷用戶滑動的距離,是否合法,合法:判斷滑動的方向 注意 距離要加上絕對值
if (Math.abs(endY - this.startY) > 10) {
//滑動方向
direction = endY - this.startY > 0 ? "down" : "up"
} else {
return
}
//用戶做了合法的滑動操作
// console.log('方向'+direction)
if (direction === 'up') {
this.next();
}
if (direction === 'down') {
this.pre();
}
}
}
};
</script>
<style scoped lang="scss">
.fullPage {
height: 100vh; //一定要設(shè)置,保證占滿
overflow: hidden; //一定要設(shè)置,多余的先隱藏
background-color: rgb(189, 211, 186);
}
.fullPageContainer {
width: 100%;
height: 100vh;
transition: all linear 0.5s;
}
.section {
width: 100%;
height: 100vh;
background-position: center center;
background-repeat: no-repeat;
}
.section1 {
background-color: rgb(189, 211, 186);
}
.section2 {
background-color: rgb(44, 48, 43);
}
.section3 {
background-color: rgb(116, 104, 109);
}
.section4 {
background-color: rgb(201, 202, 157);
}
</style>總結(jié)
到此這篇關(guān)于VUE屏幕整體滾動(滑動或滾輪)原生方法的文章就介紹到這了,更多相關(guān)VUE屏幕整體滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Composition API思想封裝NProgress示例詳解
這篇文章主要為大家介紹了Composition API思想封裝NProgress示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
element-ui動態(tài)添加表單項并實現(xiàn)事件觸發(fā)驗證代碼示例
這篇文章主要給大家介紹了關(guān)于element-ui動態(tài)添加表單項并實現(xiàn)事件觸發(fā)驗證的相關(guān)資料,其實就是利用了vue的v-for循環(huán)渲染,通過添加數(shù)組實現(xiàn)動態(tài)添加表單項,需要的朋友可以參考下2023-12-12
vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽
本文主要介紹了vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽的相關(guān)資料。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04

