Trigger 'dummy' mouse wheel event

前端 未结 9 906
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 22:30

Is there a way to trigger a scroll wheel event with an arbitrary delta. Just like jQuery does with \'click\' like:

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


        
相关标签:
9条回答
  • 2021-01-03 23:13

    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>

    0 讨论(0)
  • 2021-01-03 23:17

    I used this code in making my own scroll.

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

    I added * -1 to invert. you can remove it.

    0 讨论(0)
  • 2021-01-03 23:17

    Try:

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

    (Completely untested. Inspired by Binding to the scroll wheel when over a div)

    0 讨论(0)
提交回复
热议问题