本文介绍了触发'虚拟'鼠标滚轮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有办法触发具有任意delta的滚轮事件。
就像jQuery做'click'一样:
Is there a way to trigger a scroll wheel event with an arbitrary delta.Just like jQuery does with 'click' like:
$('#selector').trigger('click');
我需要像这样的滚轮之类的东西:
I need something like that just with an scrollwheel like:
$('#selector').trigger('DOMMouseScroll',{delta: -650});
//or mousewheel for other browsers
我不能做任何像'假的'用css技巧,我需要触发实际的本机(或尽可能接近)事件。
I can't do anything like 'fake it' with css trickery, I need to trigger the actual native (or as close as possible) event.
谢谢你!
推荐答案
你可以使用jquery滚动事件,在回电话你可以做数学。请找到有用的代码snipet。
You can use the jquery scroll event and on call back u can do ur math. Please find the code snipet usefull.
//to make dummy paragraphs
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
//dummy paragraphs end here
//the below scroll() callback is triggered eachtime mouse wheel scroll happens
$( window ).scroll(function() { //i reference to window if u want u can iD a div and give div ID reference as well
console.log("scolling....");
//update with the delta u required.
});
<html lang="en">
<head>
<meta charset="utf-8">
<title>scroll demo</title>
<style>
div {
color: blue;
}
p {
color: green;
}
span {
color: red;
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>Try scrolling the iframe.</div>
<p>Paragraph - <span>Scroll happened!</span></p>
</body>
</html>
这篇关于触发'虚拟'鼠标滚轮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!