vue实现滚动鼠标滚轮切换页面


Posted in Vue.js onDecember 13, 2020

本文实例为大家分享了vue实现滚动鼠标滚轮切换页面的具体代码,供大家参考,具体内容如下

新项目产品被甲方的要求逼疯了,大概返稿了100+次吧,最后甲方网上找了个他们认为的比较有科技感的模板,让我们照着写,首页就是类似于纵向走马灯,鼠标滚动切换,一次切换一整屏的效果。之前没接触过,写了个简单的demo,仅作为学习笔记。

其实原理很简单,就是把所有页面放在一个div中,然后滚动的时候改变外层div的top即可。

因为滚动条监听事件是实时的,所以要加上节流来防止页面切换太快速,我这控制在1.5s才能切换一页。

其实vue不应该操作dom,应该用数据来渲染虚拟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,       // 上一次翻屏结束时间
        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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。 

Vue.js 相关文章推荐
Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互的实例
Nov 18 Vue.js
Vue与React的区别和优势对比
Dec 18 Vue.js
在vue项目中封装echarts的步骤
Dec 25 Vue.js
vue+element UI实现树形表格
Dec 29 Vue.js
vue中父子组件的参数传递和应用示例
Jan 04 Vue.js
vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件
Feb 20 Vue.js
Vue详细的入门笔记
May 10 Vue.js
vue实现水波涟漪效果的点击反馈指令
May 31 Vue.js
Vue实现tab导航栏并支持左右滑动功能
Jun 28 Vue.js
vue中利用mqtt服务端实现即时通讯的步骤记录
Jul 01 Vue.js
详解Vue slot插槽
Nov 20 Vue.js
vue elementUI表格控制对应列
Apr 13 Vue.js
详解vue中使用transition和animation的实例代码
Dec 12 #Vue.js
详解如何在vue+element-ui的项目中封装dialog组件
Dec 11 #Vue.js
vue使用exif获取图片经纬度的示例代码
Dec 11 #Vue.js
vue使用exif获取图片旋转,压缩的示例代码
Dec 11 #Vue.js
Vue 实现一个简单的鼠标拖拽滚动效果插件
Dec 10 #Vue.js
vuex页面刷新导致数据丢失的解决方案
Dec 10 #Vue.js
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 #Vue.js
You might like
索尼SONY ICF-7600A(W)电路分析
2021/03/01 无线电
ThinkPHP关联模型操作实例分析
2012/09/23 PHP
php中in_array函数用法分析
2014/11/15 PHP
PHP中的一些常用函数收集
2015/05/26 PHP
Laravel学习教程之View模块详解
2017/09/18 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
静态图片的十一种滤镜效果--不支持Ie7及非IE浏览器。
2007/03/06 Javascript
javascript 二分法(数组array)
2010/04/24 Javascript
关于JavaScript中var声明变量作用域的推断
2010/12/16 Javascript
js如何获取object类型里的键值
2014/02/18 Javascript
javascript 获取函数形参个数
2014/07/31 Javascript
JS实现霓虹灯文字效果的方法
2015/08/06 Javascript
jQuery unbind()方法实例详解
2016/01/19 Javascript
使用Jasmine和Karma对AngularJS页面程序进行测试
2016/03/05 Javascript
JS实现兼容各种浏览器的获取选择文本的方法【测试可用】
2016/06/21 Javascript
原生JS实现层叠轮播图
2017/05/17 Javascript
Element实现表格嵌套、多个表格共用一个表头的方法
2020/05/09 Javascript
JS实现简易贪吃蛇游戏
2020/08/24 Javascript
Python中使用异常处理来判断运行的操作系统平台方法
2015/01/22 Python
详解Swift中属性的声明与作用
2016/06/30 Python
小小聊天室Python代码实现
2016/08/17 Python
Python3使用pandas模块读写excel操作示例
2018/07/03 Python
对Python实现累加函数的方法详解
2019/01/23 Python
使用Python 统计高频字数的方法
2019/01/31 Python
Pandas中resample方法详解
2019/07/02 Python
python3 Scrapy爬虫框架ip代理配置的方法
2020/01/17 Python
Python使用sqlite3模块内置数据库
2020/05/07 Python
安装不同版本的tensorflow与models方法实现
2021/02/20 Python
英国老牌潮鞋店:Offspring
2019/08/19 全球购物
党建目标管理责任书
2014/07/25 职场文书
健康状况证明书
2014/11/26 职场文书
湘江北去观后感
2015/06/15 职场文书
浅谈由position属性引申的css进阶讨论
2021/05/25 HTML / CSS
Python办公自动化解决world文件批量转换
2021/09/15 Python
RestTemplate如何通过HTTP Basic Auth认证示例说明
2022/03/17 Java/Android
Redis+AOP+自定义注解实现限流
2022/06/28 Redis