jquery.scrollstop.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* http://james.padolsey.com/javascript/special-scroll-events-for-jquery/ */
  2. (function(){
  3. var special = jQuery.event.special,
  4. uid1 = "D" + (+new Date()),
  5. uid2 = "D" + (+new Date() + 1);
  6. special.scrollstart = {
  7. setup: function() {
  8. var timer,
  9. handler = function(evt) {
  10. var _self = this,
  11. _args = arguments;
  12. if (timer) {
  13. clearTimeout(timer);
  14. } else {
  15. evt.type = "scrollstart";
  16. jQuery.event.dispatch.apply(_self, _args);
  17. }
  18. timer = setTimeout( function(){
  19. timer = null;
  20. }, special.scrollstop.latency);
  21. };
  22. jQuery(this).bind("scroll", handler).data(uid1, handler);
  23. },
  24. teardown: function(){
  25. jQuery(this).unbind( "scroll", jQuery(this).data(uid1) );
  26. }
  27. };
  28. special.scrollstop = {
  29. latency: 300,
  30. setup: function() {
  31. var timer,
  32. handler = function(evt) {
  33. var _self = this,
  34. _args = arguments;
  35. if (timer) {
  36. clearTimeout(timer);
  37. }
  38. timer = setTimeout( function(){
  39. timer = null;
  40. evt.type = "scrollstop";
  41. jQuery.event.dispatch.apply(_self, _args);
  42. }, special.scrollstop.latency);
  43. };
  44. jQuery(this).bind("scroll", handler).data(uid2, handler);
  45. },
  46. teardown: function() {
  47. jQuery(this).unbind( "scroll", jQuery(this).data(uid2) );
  48. }
  49. };
  50. })();