vue實(shí)現(xiàn)全屏滾動(dòng)效果(非fullpage.js)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)全屏滾動(dòng)效果(的具體代碼,供大家參考,具體內(nèi)容如下
是什么
網(wǎng)頁(yè)的一個(gè)頁(yè)面占據(jù)一屏的寬高,多個(gè)頁(yè)面上下或者左右拼接在一起。當(dāng)滑動(dòng)鼠標(biāo)滾輪,或點(diǎn)擊導(dǎo)航按鈕時(shí),可以平滑到對(duì)應(yīng)的頁(yè)面。
此次只實(shí)現(xiàn)鼠標(biāo)滾動(dòng)
實(shí)現(xiàn)原理
使用mousewheel , DOMMouseScroll(火狐用)監(jiān)聽(tīng)鼠標(biāo)滾動(dòng)事件,當(dāng)鼠標(biāo)上下的滾動(dòng)的時(shí)候,當(dāng)前的頁(yè)面transformY(屏高)或者transformX(屏寬)
代碼實(shí)現(xiàn)
HTML
<template> <div class="fullPage" ref="fullPage"> <div class="fullPageContainer" ref="fullPageContainer" @mousewheel="mouseWheelHandle" //監(jiān)聽(tīng)鼠標(biāo)事件 @DOMMouseScroll="mouseWheelHandle" // 監(jiān)聽(tīng)鼠標(biāo)事件 > <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>
JS
<script>
export default {
name: "Home",
data() {
return {
fullpage: {
current: 1, // 當(dāng)前的頁(yè)面編號(hào)
isScrolling: false, // 是否在滾動(dòng),是為了防止?jié)L動(dòng)多頁(yè),需要通過(guò)一個(gè)變量來(lái)控制是否滾動(dòng)
deltaY: 0 // 返回鼠標(biāo)滾輪的垂直滾動(dòng)量,保存的鼠標(biāo)滾動(dòng)事件的deleteY,用來(lái)判斷是往下還是往上滾
}
};
},
methods: {
next() { // 往下切換
let len = 4; // 頁(yè)面的個(gè)數(shù)
if (this.fullpage.current + 1 <= len) { // 如果當(dāng)前頁(yè)面編號(hào)+1 小于總個(gè)數(shù),則可以執(zhí)行向下滑動(dòng)
this.fullpage.current += 1; // 頁(yè)面+1
this.move(this.fullpage.current); // 執(zhí)行切換
}
},
pre() {// 往上切換
if (this.fullpage.current - 1 > 0) { // 如果當(dāng)前頁(yè)面編號(hào)-1 大于0,則可以執(zhí)行向下滑動(dòng)
this.fullpage.current -= 1;// 頁(yè)面+1
this.move(this.fullpage.current);// 執(zhí)行切換
}
},
move(index) {
this.fullpage.isScrolling = true; // 為了防止?jié)L動(dòng)多頁(yè),需要通過(guò)一個(gè)變量來(lái)控制是否滾動(dòng)
this.directToMove(index); //執(zhí)行滾動(dòng)
setTimeout(() => { //這里的動(dòng)畫(huà)是1s執(zhí)行完,使用setTimeout延遲1s后解鎖
this.fullpage.isScrolling = false;
}, 1010);
},
directToMove(index) {
let height = this.$refs["fullPage"].clientHeight; //獲取屏幕的寬度
let scrollPage = this.$refs["fullPageContainer"]; // 獲取執(zhí)行tarnsform的元素
let scrollHeight; // 計(jì)算滾動(dòng)的告訴,是往上滾還往下滾
scrollHeight= -(index - 1) * height + "px";
scrollPage.style.transform = `translateY(${scrollHeight})`;
this.fullpage.current = index;
},
mouseWheelHandle(event) { // 監(jiān)聽(tīng)鼠標(biāo)監(jiān)聽(tīng)
// 添加冒泡阻止
let evt = event || window.event;
if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.returnValue = false;
}
if (this.fullpage.isScrolling) { // 判斷是否可以滾動(dòng)
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();
}
}
}
};
</script>
CSS
<style scoped>
.fullPage{
height: 100%;//一定要設(shè)置,保證占滿(mǎn)
overflow: hidden;//一定要設(shè)置,多余的先隱藏
background-color: rgb(189, 211, 186);
}
.fullPageContainer{
width: 100%;
height: 100%;
transition: all linear 0.5s;
}
.section {
width: 100%;
height: 100%;
background-position: center center;
background-repeat: no-repeat;
}
.section1 {
background-color: rgb(189, 211, 186);
background: url("./assets/intro-bg.jpg");
}
.section1 .secction1-content {
color: #fff;
text-align: center;
position: absolute;
top: 40%;
right: 0;
left: 0;
}
.secction1-content h1 {
font-size: 40px;
padding-bottom: 30px;
}
.secction1-content p {
font-size: 20px;
}
.section2 {
background-color: rgb(44, 48, 43);
}
.section3 {
background-color: rgb(116, 104, 109);
}
.section4 {
background-color: rgb(201, 202, 157);
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue使用screenfull實(shí)現(xiàn)全屏效果
- 使用Vue-cli 中為單獨(dú)頁(yè)面設(shè)置背景圖片鋪滿(mǎn)全屏
- vue-cli點(diǎn)擊實(shí)現(xiàn)全屏功能
- vue全屏事件開(kāi)發(fā)詳解
- vue-video-player 解決微信自動(dòng)全屏播放問(wèn)題(橫豎屏導(dǎo)致樣式錯(cuò)亂問(wèn)題)
- vue實(shí)現(xiàn)瀏覽器全屏展示功能
- 富文本編輯器vue2-editor實(shí)現(xiàn)全屏功能
- vue-video-player 通過(guò)自定義按鈕組件實(shí)現(xiàn)全屏切換效果【推薦】
- vue使用screenfull插件實(shí)現(xiàn)全屏功能
相關(guān)文章
Vue實(shí)現(xiàn)購(gòu)物車(chē)實(shí)例代碼兩則
這篇文章主要介紹了Vue實(shí)現(xiàn)購(gòu)物車(chē)實(shí)例代碼,需要的朋友可以參考下2020-05-05
vue實(shí)現(xiàn)公共組件傳值并及時(shí)監(jiān)聽(tīng)到數(shù)據(jù)更新視圖
這篇文章主要介紹了vue實(shí)現(xiàn)公共組件傳值并及時(shí)監(jiān)聽(tīng)到數(shù)據(jù)更新視圖方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue中按鈕操作完刷新頁(yè)面的實(shí)現(xiàn)
這篇文章主要介紹了vue中按鈕操作完刷新頁(yè)面的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue 中 a標(biāo)簽上href無(wú)法跳轉(zhuǎn)的解決方式
今天小編大家分享一篇Vue 中 a標(biāo)簽上href無(wú)法跳轉(zhuǎn)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
這篇文章主要為大家詳細(xì)介紹了vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue實(shí)現(xiàn)數(shù)字+英文字母組合鍵盤(pán)功能
這篇文章主要介紹了vue實(shí)現(xiàn)數(shù)字+英文字母組合鍵盤(pán)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
vue實(shí)現(xiàn)進(jìn)入全屏和退出全屏的示例代碼
最近一個(gè)項(xiàng)目需要進(jìn)行大屏展示,所以登錄完就要處于一個(gè)全屏的狀態(tài),本文主要介紹了vue實(shí)現(xiàn)進(jìn)入全屏和退出全屏的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12

