当前位置: 首页 > news >正文

滑动一整屏

需求:用户通过鼠标滚轮滑动的时候一整屏进行翻页。

参考网站效果

微派网络

实现思路:

1.首先我们需获取可视区域的高度

document.documentElement.clientHeight;

2.根据动态设置top属性即可进行翻页。

3.需判断上滑还是下滑

根据onmousewheel滚轮事件即可判断,开始时间为当前时间,结束时间为事件结束以后当时间,若是差值为负数到一定区间,即可判断为向上滑动,否则为向下滑动。

注意点:

我们需通过onresize缩放来监听当前的可视窗口的高度。并且每次把当前窗口的值修改到显示区域,同时top值也需进行修改。

比如我们在第二屏幕的时候,每次改变的时候我会动态生成一个数组,并且滑动的时候,是会改变其页数的,因此我可以根据页数和高度数组进而来改变其top值。

代码展示

<template><div id="wrap" :style="[{ height: `${clientHeight}px` }]"> <div id="main" :style="{ top: `${topHeight}px` }"><div class="page" :style="{ height: `${clientHeight}px` }">1</div><div class="page" :style="{ height: `${clientHeight}px` }">2</div><div class="page" :style="{ height: `${clientHeight}px` }">3</div><div class="page" :style="{ height: `${clientHeight}px` }">4</div><div class="page" :style="{ height: `${clientHeight}px` }">5</div></div><view class="pagination"><view class="circle" v-for="v in maxCount" :key="v" :class="{ 'active':currentIdx == v }" @click="gotoPage(v)"></view></view></div>
</template><script>
export default {name: 'homeIndex',components: {},data() {return {clientHeight: 0,topHeight: 0,heightArr:[],currentIdx: 1,maxCount: 5,}},mounted() {this.clientHeight = document.documentElement.clientHeight;this.getHeightArr();this.getMouseEvent();window.onresize = () => {return (() => {this.clientHeight = document.documentElement.clientHeight;this.getHeightArr();})()}},methods: {getHeightArr(){let maxCount = this.maxCount;let heightArr = [];var height = this.clientHeight;for(let i = 0; i < maxCount; i++){let currentHeight = `-${height*i}`;heightArr.push(currentHeight)}this.heightArr = heightArr;this.topHeight = this.heightArr[this.currentIdx-1];},getMouseEvent() {let that = this;var startTime = 0;var endTime = 0;var now = 0;var height = this.clientHeight;var main = document.getElementById("main");window.onmousewheel = function (e) {height = that.clientHeight;startTime = new Date().getTime();now = Number(that.heightArr[that.currentIdx-1]);if (endTime - startTime < -600) {//鼠标向上移动if (e.wheelDelta > 0 && parseInt(main.offsetTop) < 0) {now = now + height;//鼠标向下移动} else if (e.wheelDelta < 0 && parseInt(main.offsetTop) + (height * 4) > 0) {now = now - height;}setTimeout(() => {that.topHeight = now;let currentIdx = 1;if(that.heightArr && that.heightArr.length > 0){let idx = that.heightArr.findIndex((item)=>{return item == now;})currentIdx = idx+1;}that.currentIdx = currentIdx;that.$emit('get-home-scroll-idx',currentIdx)}, 20);endTime = startTime;}}},gotoPage(page){this.currentIdx = page;setTimeout(() => {this.topHeight = this.heightArr[page-1];})}}
}
</script><style lang="scss" scoped>
html,
body {height: 100%;margin: 0;padding: 0;
}#wrap {overflow: hidden;
}#main {position: relative;top: 0;transition: 0.5s;
}.page {width: 100%;
}
.pagination{position: fixed;right: 1.5%;top: 50%;transform: translateY(-50%);z-index: 9;display: flex;flex-direction: column;.circle{width: 10px;height: 10px;display: block;margin-top: 10px;background: rgba(236,68,39,0.4);border-radius: 50%;cursor: pointer;&.active{height: 40px;border-radius: 12px;background: #EC4427;}}
}
</style>

相关文章:

  • LeetCode:9.回文数,对整数的反转操作
  • 通过无线打通两个路由器
  • 深入探讨Python中的装饰器技术
  • C语言贪吃蛇详解
  • [软件工具]文档页数统计工具软件pdf统计页数word统计页数ppt统计页数图文打印店快速报价工具
  • Oracle笔记-为表空间新增磁盘(ORA-01691)
  • sklearn模型指标和特征贡献度查看
  • IDEA创建SpringBoot+Mybatis-Plus项目
  • 论文阅读-通过云特征增强的深度学习预测云工作负载转折点
  • Git - 每次 git pull/push 时需要账号和密码解决方案
  • 勒索病毒最新变种.halo勒索病毒来袭,如何恢复受感染的数据?
  • Elasticsearch:基本 CRUD 操作 - Python
  • freertos 源码分析一 list链表数据结构
  • 华为机考入门python3--(8)牛客8-合并表记录
  • Java实现学生信息管理系统:从Excel中提取数据的实用方法
  • [Vue CLI 3] 配置解析之 css.extract
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • CSS实用技巧
  • css选择器
  • docker-consul
  • Iterator 和 for...of 循环
  • js学习笔记
  • Koa2 之文件上传下载
  • MySQL数据库运维之数据恢复
  • SQLServer之索引简介
  • vue.js框架原理浅析
  • 翻译:Hystrix - How To Use
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 设计模式走一遍---观察者模式
  • 深入浅出Node.js
  • 微信支付JSAPI,实测!终极方案
  • 小程序开发之路(一)
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • 阿里云重庆大学大数据训练营落地分享
  • ​如何在iOS手机上查看应用日志
  • # 数据结构
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #1015 : KMP算法
  • #laravel 通过手动安装依赖PHPExcel#
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (4)logging(日志模块)
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C++17) std算法之执行策略 execution
  • (ros//EnvironmentVariables)ros环境变量
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (算法)Game
  • (算法设计与分析)第一章算法概述-习题
  • (学习日记)2024.02.29:UCOSIII第二节
  • (一)Dubbo快速入门、介绍、使用
  • (一)基于IDEA的JAVA基础10
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl