纯JS阻止浏览器默认滚动事件,实现自定义滚动方法
首先该方法兼容IE7+以上浏览器,可以实现页面上下滚动,而且也可以实现页面左右滚动,每次滚动的距离为屏幕的大小,滚动为加速滚动
javaScript代码如下:
//滚动实现方法,使用鼠标滚轮每次滚动浏览器大小的距离function windowScroll(id, number, direction) { var oHtml = document.documentElement; //在IE8以下不支持使用class选取元素,这里采用id var oContent = document.getElementById(id); //获取文档的高度 var cHeight; if(direction === "top" ) { cHeight = oHtml.clientHeight; }else if( direction === "left" ) { cHeight = oHtml.clientWidth; }
//用于控制鼠标每个多长时间才能让页面滚动设置的变量 var count = 1; //在窗口尺寸改变的时候实时给cHeight赋值 window.onresize = function () { if(direction === "top" ) { cHeight = oHtml.clientHeight; }else if( direction === "left" ) { cHeight = oHtml.clientWidth; } }; if(window.addEventListener) { //用于判断当前浏览器是否为FF if( navigator.userAgent.toLowerCase().indexOf("firefox") !== -1 ) { oHtml.addEventListener("DOMMouseScroll", function (e) { //FF浏览器的滚动条滚动上下判断是与其它浏览器相反的,负值是向上滚动 if( count === 1 ) { //滚轮向上滚动时 if( e.detail < 0 ) { upRoll(); } //e.detail是正值说明是想下滚动 else if( e.detail > 0 ) { downRoll(); } } //阻止浏览器页面滚动的默认事件 e.preventDefault(); }, false); } else { oHtml.addEventListener('mousewheel',function (e) { var event = e || window.event; //当count = 1 时让页面可以滚动 if( count === 1 ) { //当鼠标向上滚动时 if( event.wheelDelta > 0 ) { upRoll(); } //当鼠标向下滚动时 if( event.wheelDelta < 0 ) { downRoll(); } } //阻止浏览器滚动的默认事件,防止页面来回晃动 event.preventDefault(); }, {passive: false}); } } else if(window.attachEvent) { oHtml.attachEvent("onmousewheel",function(){ console.log(count); if( count === 1 ){ //当鼠标向上滚动时 if( event.wheelDelta > 0 ) { upRoll(); } //当鼠标向下滚动时 if( event.wheelDelta < 0 ) { downRoll(); } } //阻止浏览器滚动的默认事件,防止页面来回晃动 return false; }); }
//向上滚动时执行的函数 function upRoll(){ if( getElemProp(oContent, direction) >= 0 ) { console.log("到顶了"); } else { elemDisplace(oContent, direction, 30, cHeight); //如果鼠标不是在顶部往上滚动的话就将count++ count++; setTimeout(function () { //当过了1000ms后把count置为1,让页面可以继续滚动 count = 1; }, 1000); } }
//向下滚动时执行的函数 function downRoll() { //判断是否滚动到底部 if( getElemProp(oContent, direction) <= -number*cHeight ) { console.log("到底了"); } else { elemDisplace(oContent, direction, -30, cHeight); //如果鼠标不是在顶部往上滚动的话就将count++ count++; setTimeout(function () { //当过了1000ms后把count置为1,让页面可以继续滚动 count = 1; }, 1000); } }}
//让元素加速运动function elemDisplace(elem, direction, speed, distance){ //记录元素当前的位置 var origin = parseInt( getElemProp(elem, direction) ); var pos; var Timer = setInterval(function(){ pos = parseInt( getElemProp(elem, direction) ); //判断元素是否位移到了指定的地方 if( Math.abs(pos - origin ) >= distance ){ if(speed > 0){ elem.style[direction] = origin + distance + 'px'; }else { elem.style[direction] = origin - distance + 'px'; } speed = 0; clearInterval(Timer); }else { //判断元素的位移方向 if(speed > 0) { speed++; } else { speed--; } elem.style[direction] = pos + speed + 'px'; } },15);}
//获取元素属性function getElemProp(elem, prop){ if(window.getComputedStyle){ return parseInt(window.getComputedStyle(elem, null)[prop]); }else{ return parseInt(elem.currentStyle[prop]); }}
使用注意事项:1.使用时直接调用windowScroll(id,number,direction)方法将需要滚动的元素id传入第一个参数(注意是直接传入id,不要将id选择器选取出来的元素传入)number是滚动的次数direction是滚动的方向,如果你需要水平滚动需要传入"left"、垂直滚动传入"top"2.其它两个方法是为了封装函数,解决浏览器的兼容性问题
对该方法的一些补充(可以添加点击元素来控制页面的滚动),点击下方链接查看https://www.cnblogs.com/hros/p/10941141.html
原创文章
纯JS阻止浏览器默认滚动事件,实现自定义滚动方法的更多相关文章
- js阻止浏览器默认行为
js阻止浏览器默认行为 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- JS 阻止浏览器默认行为和冒泡事件
JS 冒泡事件 首先讲解一下js中preventDefault和stopPropagation两个方法的区别: preventDefault方法的起什么作用呢?我们知道比如<a href=& ...
- js阻止浏览器默认事件
1.阻止浏览器的默认行为 function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) { / ...
- Javascript:阻止浏览器默认右键事件,并显示定制内容
在逛一些知名图片社区的时候,遇到自己心怡的图片,想要右键另存的时候,默认的浏览器菜单不见了,却出现了如:[©kevin版权所有]之类的信息: 今天在看Javascript事件默认行为相关的知识,所以, ...
- js 阻止浏览器默认行为
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JQuery 阻止js事件冒泡 阻止浏览器默认操作
//阻止事件冒泡 event.stopPropagation(); //阻止浏览器默认操作 event.preventDefault(); 代码不一定能执行,写给自己看的. 事件冒泡: <a h ...
- js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)
转自:http://blog.csdn.net/itchiang/article/details/7769341 添加事件 var addEvent = function( obj, type, ...
- js停止冒泡和阻止浏览器默认行为
停止冒泡通用方法: function stopBubble(e) { //如果提供了事件对象,是非IE浏览器 if ( e && e.stopPropagation ) //使用W3C ...
- 理解阻止浏览器默认事件和事件冒泡cancelBubble
一.阻止浏览器默认事件 1.先举个例子说什么是 浏览器的默认事件 : 比如有一个输入框,当我按下字母a,就会在输入框显示字母a.就是浏览器本该发生的事情.小孩子一出生就会汲取母乳一样的道理,这些都是先 ...
随机推荐
- weexapp 开发流程(二)框架搭建
1.创建 入口文件 src / entry.js /** * 入口文件 */ import App from './App.vue' import router from './router' // ...
- JAVA设计模式之 原型模式【Prototype Pattern】
一.概述: 使用原型实例指定创建对象的种类,而且通过拷贝这些原型创建新的对象. 简单的说就是对象的拷贝生成新的对象(对象的克隆),原型模式是一种对象创建型模式. 二.使用场景: 创建新的对象能够通过对 ...
- nginx 配置nginx.conf,负载均衡,逻辑分流
nginx 最重要的配置文件nginx.conf: 一般的配置我不做解释,网上到处都是,主要对主要的几点进行注释(如下) worker_processes ; error_log /data/logs ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- apk程序查找方法调用
有android killer,现在ida对android的支持等一些方便工具,此篇(关于搜索和修改代码)废弃. 没有好的调试工具下 常用插代码(如果怕影响寄存器值,可以将.locals xxx改多几 ...
- 获取Wifi密码,不知道是不是真的
package com.example.wifipassword; import java.util.List; import android.app.Activity; import android ...
- 使用libcurl的包装库cpr发起http请求
cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...
- strncpy和strlen的可能的实现
#include <stdio.h> #include <stdlib.h>//为避免与标准库中的函数发生混淆,我将它们命名为stringNCopy和stringLength ...
- super究竟是个啥?
引子: 一直以为oc的super跟java中的super是一回事,没有去深究它的本质,直到工作的时候遇到一个并不能按我的理解能解释的情况. 剖析: 在此之前先看一段代码: 有两个类 SuperClas ...
- CUE 文件格式说明
CUE 文件,即 CUESheets ,光盘镜像辅助文件.通常用于光盘刻录.音乐播放等等. 比如用 EAC 刻录CD光盘,或者用 Foobar2000 播放整轨音乐文件. CUE 文件是非常好的音乐专 ...