`

js鼠标滑轮滚动事件绑定(兼容主流浏览器)

    博客分类:
  • js
阅读更多
/** Event handler for mouse wheel event.
		 *鼠标滚动事件
		 */
		var wheel = function(event) {
			var delta = 0;
			if (!event) /* For IE. */
				event = window.event;
			if (event.wheelDelta) { /* IE/Opera. */
				delta = event.wheelDelta / 120;
			} else if (event.detail) {
				/** Mozilla case. */
				/** In Mozilla, sign of delta is different than in IE.
				 * Also, delta is multiple of 3.
				 */
				delta = -event.detail / 3;
			}
			/** If delta is nonzero, handle it.
			 * Basically, delta is now positive if wheel was scrolled up,
			 * and negative, if wheel was scrolled down.
			 */
			if (delta)
				handle(delta);
			/** Prevent default actions caused by mouse wheel.
			 * That might be ugly, but we handle scrolls somehow
			 * anyway, so don't bother here..
			 */
			if (event.preventDefault)
				event.preventDefault();
			event.returnValue = false;
		}

		/** Initialization code. 
		 * If you use your own event management code, change it as required.
		 */
		if (window.addEventListener) {
			/** DOMMouseScroll is for mozilla. */
			window.addEventListener('DOMMouseScroll', wheel, false);
		}
		/** IE/Opera. */
		window.onmousewheel = document.onmousewheel = wheel;

		/** This is high-level function.
		 * It must react to delta being more/less than zero.
		 */
		var handle = function(delta) {
			var random_num = Math.floor((Math.random() * 100) + 50);
			if (delta < 0) {
				// alert("鼠标滑轮向下滚动:" + delta + "次!"); // 1
				$("btn_next_pic").onclick(random_num);
				return;
			} else {
				// alert("鼠标滑轮向上滚动:" + delta + "次!"); // -1
				$("btn_last_pic").onclick(random_num);
				return;
			}
		}
分享到:
评论
3 楼 kt431128 2013-11-01  
兄台,滚轮滚动一定的距离触发一个事件,怎么搞?
2 楼 qiaolevip 2013-01-07  
qq_ji寞 写道
支持谷歌么

支持啊,纯js的,对opera支持不是很好,鼠标滚动的时候相当于两下。
1 楼 qq_ji寞 2012-12-11  
支持谷歌么

相关推荐

Global site tag (gtag.js) - Google Analytics