html 禁用鼠标滚轮对网页滚动条的控制怎么实现?

html 禁用鼠标滚轮对网页滚动条的控制怎么实现?,第1张

可以用js或者css实现。

js可以监听鼠标的onscroll滚轮事件,事件内可以阻止鼠标的滚轮默认事件。

css可以设置元素的"pointer-events"属性,设置为none则所有鼠标事件失效。

比较divoffset 滚距离scrolloffset<scroll触发事件 参考:中国//jsfiddle中国/n4pdx/ 或者使用js库比waypoints 中国//imakewebthings中国/waypoints/

通过此函数可以获取到鼠标滚动事件,然后做平移就好了

/** 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

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6131860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-15
下一篇 2023-03-15

发表评论

登录后才能评论

评论列表(0条)

保存