首页 > 其他分享 >vue 一次滑动整个屏幕

vue 一次滑动整个屏幕

时间:2022-12-01 14:44:24浏览次数:33  
标签:index vue screenHeight nbsp scrollFun 滑动 屏幕 main document

<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}">&nbsp;</li>
<li id="pageUlLi2" class="pageUlLi" :class="{'active': curIndex == 2}">&nbsp;</li>
<li id="pageUlLi3" class="pageUlLi" :class="{'active': curIndex == 3}">&nbsp;</li>
<li id="pageUlLi4" class="pageUlLi" :class="{'active': curIndex == 4}">&nbsp;</li>
<li id="pageUlLi5" class="pageUlLi" :class="{'active': curIndex == 5}">&nbsp;</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, // 上一次翻屏结束时间
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);
// 如果当前滚动开始时间和上次滚动结束时间的差值小于1.5s,则不执行翻页动作,这样做是为了实现类似节流的效果
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);
}
// 本次翻页结束,记录结束时间,用于下次判断
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>
————————————————
版权声明:本文为CSDN博主「Lccccb」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Lccccb/article/details/104671115

标签:index,vue,screenHeight,nbsp,scrollFun,滑动,屏幕,main,document
From: https://www.cnblogs.com/shuhan-hou/p/16941362.html

相关文章

  • Vue实用框架-Ruoyi(项目打包)
    Vue实用框架-Ruoyi(项目打包)ruoyi前后端分离版项目的服务器打包官网已经做了比较详细的说明,但实际运行踩了点小坑,可能跟我喜欢用tomcat+nginx有关系,所以特此补充:官方打......
  • [Vue3-10]表单数据绑定
    1.输入框2.单选多选3.下拉选择......
  • [Vue3-09]事件处理
    1.传参2.多事件绑定......
  • 自己维护vue项目的路由跳转历史记录
    问题:公司开发一个商城,总是遇到回退到上页面时会有很多逻辑处理,由于每个页面都要写回退的方法,所以总会漏掉一些判断,导致出现很多类似的bug。所以想到了可能自己封装一下,来......
  • vue和uni-app的区别有哪些
    vue和uni-app的区别:1、uni-app可以通过打包实现一套代码多端运行,而vue不行;2、uni-app有自动的框架预载,加载页面的速度更快,vue没有;3、uniapp使用小程序的标签,vue使用web端......
  • vue中导入monaco-editor
    monaco-editormonaco-editor是一款代码编辑器,使用它我们可以再web实现vscode的基本功能 安装npminstallmonaco-editor添加代码提示这里我只添加......
  • vue 3.0
    1.安装最新vue环境cnpmi-gvue@vue/cli  要确保环境是4.5以上的版本2.创建项目vuecreate(你的项目名称:vue3demo)根据需要选择下面相关组件......
  • vue基础笔记
    1Vue简介Vue是一个框架,也是一个生态。其功能覆盖了大部分前端开发常见的需求。但Web世界是十分多样化的,不同的开发者在Web上构建的东西可能在形式和规模上会有很大......
  • 滑动窗口
    给定一个数组。有一个大小为k 的滑动窗口,它从数组的最左边移动到最右边。你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。#include<iostream>#inclu......
  • vuecli3项目集成到springboot
    路径配置当springboot中设置项目访问路径server.servlet.context-path=/demovue项目中vue.config.js需配置publicPathmodule.exports={transpileDependenc......