stopping mousewheel event from happening twice in OSX

user796443

I noticed mousewheel event is happening multiple times in mac osx. Can be atributed to inertia feature.

Is there a way to fix this behaviour?

(self signed ssl no worries please!) https://sandbox.idev.ge/roomshotel/html5_v2/

I'm using scrollSections.js https://github.com/guins/jQuery.scrollSections

And it uses mousewheel jquery plugin: https://github.com/brandonaaron/jquery-mousewheel

I'm seeing a lot of people having the same issue: https://github.com/brandonaaron/jquery-mousewheel/issues/36

There are some solutions but none works with scrollSections plugin.

Any ideas how to disable this inertia feature from JS?

My attempted fix:

// Fix for OSX inertia problem, jumping sections issue.
if (isMac) {

  var fireEvent;
  var newDelta = deltaY;

  if (oldDelta != null) {

    //check to see if they differ directions
    if (oldDelta < 0 && newDelta > 0) {
      fireEvent = true;
    }

    //check to see if they differ directions
    if (oldDelta > 0 && newDelta < 0) {
      fireEvent = true;
    }

    //check to see if they are the same direction
    if (oldDelta > 0 && newDelta > 0) {

      //check to see if the new is higher
      if (oldDelta < newDelta) {
        fireEvent = true;
      } else {
        fireEvent = false;
      }
    }

    //check to see if they are the same direction
    if (oldDelta < 0 && newDelta < 0) {

      //check to see if the new is lower
      if (oldDelta > newDelta) {
        fireEvent = true;
      } else {
        fireEvent = false;
      }
    }

  } else {

    fireEvent = true;

  }

  oldDelta = newDelta;

} else {

  fireEvent = true;

}

You can see fix implemented here: https://sandbox.idev.ge/roomshotel/html5_v2/ But it is a hit/miss.

Sam Braslavskiy

The latest solution with timeouts had one major drawback: kinetic scrolling effect could last rather long (even 1s or so)... and disabling scrolling for 1-2 seconds wouldn't be the best decision.

Soooo, as promised, here's another approach.

Our goal is to provide one response for one user action, which in this case is scrolling.

What's 'one scrolling'? For the sake of solving this problem, let's say that 'one scrolling' is an event that lasts from the moment the page has started to move till the moment the movement has ended.

Kinetic scrolling effect is achieved by moving the page many times (say, every 20ms) for a small distance. It means that our kinetic scrolling consists of many-many little linear 'scrollings'.

Empirical testing has showed that this little 'scrollings' happen every 17-18ms in the middle of kinetic scroll, and about 80-90ms at the beginning and the end. Here's a simple test we can set up to see that:

var oldD;
var f = function(){
    var d = new Date().getTime();
    if(typeof oldD !== 'undefined')
        console.log(d-oldD);
    oldD = d;
}
window.onscroll=f;

Important! Every time this mini-scroll happens, scroll event is triggered. So:

 window.onscroll = function(){console.log("i'm scrolling!")};

will be fired 15 to 20+ times during one kinetic scroll. BTW, onscroll has really good browser support (see compatibility table), so we can rely on it (except for touch devices, I'll cover this issue a bit later);

Some may say that redefining window.onscroll is not the best way to set event listeners. Yes, you're encouraged to use

 $(window).on('scroll',function(){...});

or whatever you like, it's not the point of the problem (I personally use my self-written library).

So, with the help of onscroll event we can reliably say whether this particular mini-movement of the page belongs to one long-lasting kinetic scroll, or is it a new one:

    var prevTime = new Date().getTime();
    var f = function(){
        var curTime = new Date().getTime();
        if(typeof prevTime !== 'undefined'){
            var timeDiff = curTime-prevTime;
            if(timeDiff>200)
                console.log('New kinetic scroll has started!');
        }
        prevTime = curTime;
    }
    window.onscroll=f;

Instead of "console.log" you can call your desired callback function (or event handler) and you're done! The function will be fired only once on every kinetic or simple scroll, which was our goal.

You may have noticed that I've used 200ms as a criteria of whether it's a new scroll or a part of the previous scroll. It's up to you to set it to greater values to be 999% sure you prevent any extra calls. However, please keep in mind that it's NOT what we have used in my previous answer. It's just a period of time between any two page movements (whether it's a new scroll or a little part of a kinetic scroll). To my mind, there's a very little chance that there will be a lag more than 200ms between steps in kinetic scroll (otherwise it will be not smooth at all).

As I've mentioned above, the onscroll event works differently on touch devices. It won't fire during every little step of kinetic scroll. But it will fire when the movement of the page has finally ended. Moreover, there's ontouchmove event... So, it's not a big deal. If necessary, I can provide solution for touch devices too.

P.S. I understand that I've written a bit too much, so I'd be happy to answer all your questions and provide further code if you need one.

Provided solution is supported in all browsers, very lightweight and, what's more important, is suitable not only for macs, but for every device that might implement kinetic scrolling, so I think it's really a way to go.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Javascript/jQuery: animation happening twice?

分類Dev

JQuery - Event Delegation - What is happening in my code?

分類Dev

React.useEffect hook and subscribing to a mousewheel event - the right approach

分類Dev

Swift OSX key event

分類Dev

drag and drop event fires twice

分類Dev

JavaScript Event Listener runs twice

分類Dev

Get records happening on a particular date from mysql

分類Dev

Obtaining lines from an array and stopping at a particular character

分類Dev

Stopping kids from using a machine for illegal activites?

分類Dev

Titanium - prevent exitOnClose from stopping the app

分類Dev

bot framework conversationUpdate event seems to fire twice

分類Dev

Sharepoint 2010 event receiver ItemUpdated fires twice

分類Dev

How to wait for a dispatch happening in one store from another store

分類Dev

How to find out what's preventing a click from happening?

分類Dev

My Paint method is running twice and I have no idea why. How can I fix this, and does anyone know why this is happening?

分類Dev

iTerm2: Stopping the inactive pane from dimming

分類Dev

How to start a service using GitLab runner and prevent it from stopping?

分類Dev

What is the importance of synchronized method in stopping one Thread from another?

分類Dev

Android: Stopping program flow from continuing if error condition?

分類Dev

JQuery: Defining a variable is stopping my script from running

分類Dev

inotifywait triggering event twice while converting docx to PDF

分類Dev

Have to click twice to update state (useState hook) with onClick event

分類Dev

jQuery UI drop event of droppable widget firing twice

分類Dev

How to enable mousewheel in chrome?

分類Dev

How to stop code from calculating twice

分類Dev

How to prevent from CronJob run process twice?

分類Dev

Rolling restarts are causing are app engine app to go offline. Is there a way to change the config to prevent that from happening?

分類Dev

What is happening when I run a persistent storage live USB from RAM?

分類Dev

Running R from Mac OSX terminal

Related 関連記事

  1. 1

    Javascript/jQuery: animation happening twice?

  2. 2

    JQuery - Event Delegation - What is happening in my code?

  3. 3

    React.useEffect hook and subscribing to a mousewheel event - the right approach

  4. 4

    Swift OSX key event

  5. 5

    drag and drop event fires twice

  6. 6

    JavaScript Event Listener runs twice

  7. 7

    Get records happening on a particular date from mysql

  8. 8

    Obtaining lines from an array and stopping at a particular character

  9. 9

    Stopping kids from using a machine for illegal activites?

  10. 10

    Titanium - prevent exitOnClose from stopping the app

  11. 11

    bot framework conversationUpdate event seems to fire twice

  12. 12

    Sharepoint 2010 event receiver ItemUpdated fires twice

  13. 13

    How to wait for a dispatch happening in one store from another store

  14. 14

    How to find out what's preventing a click from happening?

  15. 15

    My Paint method is running twice and I have no idea why. How can I fix this, and does anyone know why this is happening?

  16. 16

    iTerm2: Stopping the inactive pane from dimming

  17. 17

    How to start a service using GitLab runner and prevent it from stopping?

  18. 18

    What is the importance of synchronized method in stopping one Thread from another?

  19. 19

    Android: Stopping program flow from continuing if error condition?

  20. 20

    JQuery: Defining a variable is stopping my script from running

  21. 21

    inotifywait triggering event twice while converting docx to PDF

  22. 22

    Have to click twice to update state (useState hook) with onClick event

  23. 23

    jQuery UI drop event of droppable widget firing twice

  24. 24

    How to enable mousewheel in chrome?

  25. 25

    How to stop code from calculating twice

  26. 26

    How to prevent from CronJob run process twice?

  27. 27

    Rolling restarts are causing are app engine app to go offline. Is there a way to change the config to prevent that from happening?

  28. 28

    What is happening when I run a persistent storage live USB from RAM?

  29. 29

    Running R from Mac OSX terminal

ホットタグ

アーカイブ