2012-02-22 7 views
17

任意のデルタでスクロールホイールイベントをトリガーする方法はありますか? jQueryのような「クリック」してい同じように:私はCSSの策略で「偽それ」のような何かを行うことはできませんトリガー 'ダミー'マウスホイールイベント

$('#selector').trigger('DOMMouseScroll',{delta: -650}); 
//or mousewheel for other browsers 

$('#selector').trigger('click'); 

私はちょうどのようなスクロールホイールでそのようなものが必要、私は実際のネイティブ(または可能な限り)イベントを引き起こす必要があります。

ありがとうございました!

+0

[jQuery Docs](http://api.jquery.com/scroll/) – Chad

+0

はい、スクロールは位置に基づいています。それらのイベントにバインドされたアクション。とにかくありがとう – escusado

+0

この記事をチェックしようhttp://www.howtocreate.co.uk/tutorials/javascript/browserwindow私はそれがあなたが必要としていると思う。 –

答えて

-4

試してみてください。

.trigger("mousewheel", {wheelDelta: 100})

(完全にテストされていないBinding to the scroll wheel when over a divからインスピレーションを受けている。)

+0

申し訳ありません私はOSレベルのマクロアプリケーションで別のアプローチを見つけようとしています。とにかくありがとう:) – escusado

-6
this.trigger("mousewheel", {intDelta:0, deltaX:0, deltaY:0}); 

実際に、これは良い作品:

this.trigger("mousewheel", [0]) 
-2

ます場合、これはスクロール機能を呼び出します何でも書いてあります

$(window).scroll(); 
-5

こんにちは、イベントリスナーを追加することでこれを行うことができます。私は同じものを探して、下のコードスニペットを得て、それは働いています。あなたがあなた自身のカスタム機能を書くことができます同じ要件にデルタ変数に基づいて


<html> 
<head> 
<title>Mouse Scroll Wheel example in JavaScript</title> 
<style> 
#scroll { 
width: 250px; 
height: 50px; 
border: 2px solid black; 
background-color: lightyellow; 
top: 100px; 
left: 50px;`enter code here` 
position:absolute; 
} 
</style> 
<script language="javascript"> 
window.onload = function() 
{ 
//adding the event listerner for Mozilla 
if(window.addEventListener) 
    document.addEventListener('DOMMouseScroll', moveObject, false); 

//for IE/OPERA etc 
document.onmousewheel = moveObject; 
} 
function moveObject(event) 
{ 
var delta = 0; 

if (!event) event = window.event; 

// normalize the delta 
if (event.wheelDelta) { 

    // IE and Opera 
    delta = event.wheelDelta/60; 

} else if (event.detail) { 

    // W3C 
    delta = -event.detail/2; 
} 

var currPos=document.getElementById('scroll').offsetTop; 

//calculating the next position of the object 
currPos=parseInt(currPos)-(delta*10); 

//moving the position of the object 
document.getElementById('scroll').style.top = currPos+"px"; 
document.getElementById('scroll').innerHTML = event.wheelDelta + ":" + event.detail; 
} 
</script> 
</head> 
<body> 
Scroll mouse wheel to move this DIV up and down. 
<div id="scroll">Event Affect</div> 
</body> 
</html> 
------------------------------------------------------------ 

を持って確認してください。

HTMLで5マウスのスクロールイベントを使用すると、私は自分のスクロールを行う際に、このコードを使用し、またそのように

+0

私はこれを違反すると信じています "私はcssトリッキーと"それを "偽造"のような何かを行うことはできません。 –

-2

を試すことができます処理されています。

$('body').bind('mousewheel DOMMouseScroll', function(event) { 
    var delta = Math.max(-1, Math.min(1, (event.originalEvent.wheelDelta || -event.originalEvent.detail))) * -1; 
} 

逆にするには* -1を追加しました。それを削除することができます。

1

あなたは、例えばjQuery.Event を使用する必要があります。もっとここ

// Create a new jQuery.Event object with specified event properties. 
var e = jQuery.Event("DOMMouseScroll",{delta: -650}); 

// trigger an artificial DOMMouseScroll event with delta -650 
jQuery("#selector").trigger(e); 

はチェック:http://api.jquery.com/category/events/event-object/

+0

ドキュメントでは、クロスブラウザの方法で 'delta'プロパティをサポートしているとは言えません。実際には、イベントオブジェクト内ではサポートされていません。 – Alvaro

+2

これは動作していないようです:http://jsfiddle.net/95ssxLc9/1/ – Alvaro

1

あなたはjqueryのスクロールイベントを使用することができますし、コールバックにuはウル数学を行うことができます。役に立つコードスニペットを見つけてください。

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

0

コンソールにこれを入力してみてください、とその効果を確認することができます。

document.body.scrollTop = document.body.scrollTop + 200 

200は任意の数です。 同様に、あなたもscrollLeftでこれを試すことができます。

document.body.scrollLeft = document.body.scrollLeft + 200 

または

document.body.scrollLeft = document.body.scrollLeft - 200 

あなたはこの概念とウィンドウのsetInterval()メソッドで遊んで試みることができます。

さらに........

次のように、要素のオフセットを取得することができます。

あなたのような結果得られます
jQuery("#vote-to-add-section").offset() 

{top: 9037.1875, left: 268} 

このような要素にスクロールすると、次のようになります。

document.body.scrollTop = jQuery("#vote-to-add-section").offset().top 

また........

あなただけのサイトが最初にロードされたときにサイトが既に特定の要素を下にスクロールさせたい場合は、持っている要素でこれを行うことができますid:

あなたが座っているURLの末尾に#idnameを追加します。そのID名を持つ要素がページ上に存在する必要があります。例えば

は、これらのURLの比較、そしてあなたはURLアドレスバーにそれらを入力するとき、あなたは何を得る:

https://travel.usnews.com/rankings/worlds-best-vacations https://travel.usnews.com/rankings/worlds-best-vacations/#vote-to-add-section

最後に#投票ツーアドオン部分を有するものですid#vote-to-add-sectionを持つ要素まで自動的にスクロールします。