简体   繁体   中英

js update scroll event continuously

May I ask the detail of the giving solution from below question?? I have exactly the same problem.

jQuery's css() lags when applied on scroll event

On giving solution #3 ->> If you want continuous updates, keep track of the timestamp when you do an update, and do nothing in the handler if it's been less than some amount of time (100ms or whatever).

Can I have more detailed information how to do or example about it?

I would do it something similar to this:

var lastUpdate = new Date().getTime();
$(selector).scroll(function() {
     var now = new Date().getTime();
     if (now - lastUpdate > 100) {
          // update css
          lastUpdate = now;
     }
 });

This will update the css every .1s (which is what I think you would want. Obviously, just replace selector with whatever element you're binding the scroll event to, and replace the update css comment with your .css() call!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM