mwheelIntent.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @author trixta
  3. * @version 1.2
  4. */
  5. (function($){
  6. var mwheelI = {
  7. pos: [-260, -260]
  8. },
  9. minDif = 3,
  10. doc = document,
  11. root = doc.documentElement,
  12. body = doc.body,
  13. longDelay, shortDelay
  14. ;
  15. function unsetPos(){
  16. if(this === mwheelI.elem){
  17. mwheelI.pos = [-260, -260];
  18. mwheelI.elem = false;
  19. minDif = 3;
  20. }
  21. }
  22. $.event.special.mwheelIntent = {
  23. setup: function(){
  24. var jElm = $(this).bind('mousewheel', $.event.special.mwheelIntent.handler);
  25. if( this !== doc && this !== root && this !== body ){
  26. jElm.bind('mouseleave', unsetPos);
  27. }
  28. jElm = null;
  29. return true;
  30. },
  31. teardown: function(){
  32. $(this)
  33. .unbind('mousewheel', $.event.special.mwheelIntent.handler)
  34. .unbind('mouseleave', unsetPos)
  35. ;
  36. return true;
  37. },
  38. handler: function(e, d){
  39. var pos = [e.clientX, e.clientY];
  40. if( this === mwheelI.elem || Math.abs(mwheelI.pos[0] - pos[0]) > minDif || Math.abs(mwheelI.pos[1] - pos[1]) > minDif ){
  41. mwheelI.elem = this;
  42. mwheelI.pos = pos;
  43. minDif = 250;
  44. clearTimeout(shortDelay);
  45. shortDelay = setTimeout(function(){
  46. minDif = 10;
  47. }, 200);
  48. clearTimeout(longDelay);
  49. longDelay = setTimeout(function(){
  50. minDif = 3;
  51. }, 1500);
  52. e = $.extend({}, e, {type: 'mwheelIntent'});
  53. return $.event.handle.apply(this, arguments);
  54. }
  55. }
  56. };
  57. $.fn.extend({
  58. mwheelIntent: function(fn) {
  59. return fn ? this.bind("mwheelIntent", fn) : this.trigger("mwheelIntent");
  60. },
  61. unmwheelIntent: function(fn) {
  62. return this.unbind("mwheelIntent", fn);
  63. }
  64. });
  65. $(function(){
  66. body = doc.body;
  67. //assume that document is always scrollable, doesn't hurt if not
  68. $(doc).bind('mwheelIntent.mwheelIntentDefault', $.noop);
  69. });
  70. })(jQuery);