鼠标滚动事件 - onmousewheel
1.Jquery-MouseWheel
jquery默认是不支持支持鼠标滚轮事件(mousewheel)
jquery MouseWheel下载:https://github.com/jquery/jquery-mousewheel/blob/master/jquery.mousewheel.js
然后就可以使用mousewheel和unmousewheel事件函数了
$('div.mousewheel_example').mousewheel(fn);
$('div.mousewheel_example').bind('mousewheel', fn);
Jquery MouseWheel兼容性
- FireFox浏览器使用DOMMouseScroll事件,其他(包括IE6)都是使用onmousewheel事件;
- FireFox中wheelDelta判断滚动方向,其值为120/-120,为负数的时候表示向下滚动,整数的时候向上滚动
- 其他:detail(属性)判断方向,返回值是3的整数倍(3/-3), 为正数表示向上滚动,负数向下滚动
- opera:同时拥有wheelDelta和detail,其中“detail”属性返回值和FF中的wheelDelta相同
使用实例
1 // jquery 兼容的滚轮事件
2 $(document).on("mousewheel DOMMouseScroll", function (e) {
3
4 var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
5 (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1)); // firefox
6
7
8 if (delta > 0) {
9 // 向上滚
10 console.log("wheelup");
11 } else if (delta < 0) {
12 // 向下滚
13 console.log("wheeldown");
14 }
15 });
2.使用js原生实现滚轮事件
但是考虑到兼容性
IE下通过attachEvent实现事件监听
Chrome和FF通过addEventListener来实现事件监听
但是FF滚轮事件是:DOMAMouseSrcoll
其他浏览器:onmousewheel
| 事件监听 | 滚轮事件 | |
| Chrome | addEventListener | onmousewheel |
| IE | attachEvent | onmousewheel |
| FF | addEventListener | DOMAMouseSrcoll |
所以我自己封装了一个兼容各浏览器的方法
addEvent: function(obj, xEvent, fn) {
if (obj.attachEvent) {
obj.attachEvent('on' + xEvent, fn);
} else {
obj.addEventListener(xEvent, fn, false);
}
},
调用:
this.addEvent($(document),"mousewheel",callback); //其他
this.addEvent($(document),"DOMMouseScroll",callback); //FF
鼠标滚动事件 - onmousewheel的更多相关文章
- JS鼠标滚动事件
-----------------------------//鼠标滚动事件以下是JS临听鼠标滚动事件 并且还考虑到了各浏览器的兼容----------------------------------- ...
- 在FireFox浏览器上,用stopImmediatePropagation阻止冒泡鼠标滚动事件
楔子 是不是在火狐用stopPropagation不太满意 很久没有笑过又不知为何 既然不快乐又不喜欢这里 不如一路向西用stopImmediatePropagation(其实我对浏览器的兼容性看不顺 ...
- js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写
js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写 一.总结 一句话总结:滚动事件scroll(),浏览器窗口调整监听resize(),思考好监听对象. 1.滚动事件scroll()的监听对象是 ...
- vue组件添加鼠标滚动事件
在一个组件标签上加鼠标滚动事件,应该写成 @mousewheel.native
- JavaScript一个鼠标滚动事件的实例
<script type="text/javascript" src="./whenReady.js"></script> <!- ...
- HTML中鼠标滚轮事件onmousewheel处理
滚轮事件是不同浏览器会有一点点区别,一个像Firefox使用DOMMouseScroll ,ff也可以使用addEventListener方法绑定DomMouseScroll事件,其他的浏览器滚轮事件 ...
- 鼠标滚动事件兼容性 wheel、onwheel
wheelEvent = "onwheel" in document.createElement("div") ? "wheel" : // ...
- js监控鼠标滚动事件
//滚动动画 windowAddMouseWheel(); function windowAddMouseWheel() { var scrollFunc = function (e) { e = e ...
- 解决ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效
C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个DataGrid.ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果 ...
随机推荐
- 原生JavaScript封装Ajax
第一次开个人技术博客了,发的第一篇技术文章,欢迎指点…… 欢迎访问本人的独立博客:蓝克比尔 Ajax的实现主要分为四部分: 1.创建Ajax对象 // 创建ajax对象 var xhr = null; ...
- js作用域详解
// 作用域:(1)域:空间.范围.区域…… (2) 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 浏览器: “JS解析器” 1)“找一些东西” :var func ...
- sqlserver 操作xml
1.xml.exist 输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 2.xml.value 输入为XQuery表达式,返回一个SQL ...
- C# winform Datagridview 标题居中
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- 关于mongodb的一些笔记
1.以服务的形式安装mongodb dos -- 进入到mongodb的bin目录下,执行 D:\mongodb\bin>mongod --logpath D:\mongodb\logs\mon ...
- ios 开发证书 appids 描述文件关系
当你准备进行真机测试或者发布应用到App Store上去的时候, 免不了要申请相应的证书.(Development--测试证书. Distribution--发布证书) 进入证书管理相应网站https ...
- 【linux-shell】rsync
https://en.wikipedia.org/wiki/Rsync https://en.wikipedia.org/wiki/Andrew_Tridgell 安德鲁.锤子,发布了rsync的第一 ...
- gem install bundler
http://stackoverflow.com/questions/7483515/rake-aborted-no-such-file-to-load-bundler-setup-rails-3-1 ...
- 父子页面(iframe)相互获取对方dom元素
现在iframe的使用虽然开始越来越少,但是还是有牵涉到iframe的使用情况,特别是多个iframe互相嵌套,又要进行获取的情况. 现在整理了父子iframe之间互相获取的方式. (1)父页面获取子 ...
- Enhancing Reliability and Response Times via Replication in Computing Clusters---INFOCOM 2015
[标题] Enhancing Reliability and Response Times via Replication in Computing Clusters [作者] Zhan Qiu an ...