VAScroll.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. VAScroll = function(w) {
  2. this.window = w ? w : window;
  3. this.up = null;
  4. this.down = null;
  5. this.left = null;
  6. this.right = null;
  7. this.speed = 1;
  8. this.scrollDown = jQuery.proxy(this.scrollDown, this);
  9. this.scrollUp = jQuery.proxy(this.scrollUp, this);
  10. this.scrollRight = jQuery.proxy(this.scrollRight, this);
  11. this.scrollLeft = jQuery.proxy(this.scrollLeft, this);
  12. this.mousemove = jQuery.proxy(this.mousemove, this);
  13. this.getScrollVars = jQuery.proxy(this.getScrollVars, this);
  14. }
  15. VAScroll.prototype = {
  16. active: function() {
  17. jQuery(this.window.document).bind("mousemove", this.mousemove);
  18. },
  19. mousemove: function(e) {
  20. var vars = this.getScrollVars();
  21. var d = e.pageY - vars.st;
  22. if (10 < d && d < 50) {
  23. this.speed = d < 30 ? 5 : 1;
  24. if (!this.up) {
  25. this.up = setTimeout(this.scrollUp, 300);
  26. }
  27. }
  28. else {
  29. if (this.up) {
  30. clearTimeout(this.up);
  31. this.up = null;
  32. }
  33. }
  34. d = vars.h + vars.st - e.pageY;
  35. if (10 < d && d < 50) {
  36. this.speed = d < 30 ? 5 : 1;
  37. if (!this.down) {
  38. this.down = setTimeout(this.scrollDown, 300);
  39. }
  40. }
  41. else {
  42. if (this.down) {
  43. clearTimeout(this.down);
  44. this.down = null;
  45. }
  46. }
  47. d = e.pageX - vars.sl;
  48. if (10 < d && d < 50) {
  49. this.speed = d < 30 ? 5 : 1;
  50. if (!this.left) {
  51. this.left = setTimeout(this.scrollLeft, 300);
  52. }
  53. }
  54. else {
  55. if (this.left) {
  56. clearTimeout(this.left);
  57. this.left = null;
  58. }
  59. }
  60. d = vars.w + vars.sl - e.pageX;
  61. if (10 < d && d < 50) {
  62. this.speed = d < 30 ? 5 : 1;
  63. if (!this.right) {
  64. this.right = setTimeout(this.scrollRight, 300);
  65. }
  66. }
  67. else {
  68. if (this.right) {
  69. clearTimeout(this.right);
  70. this.right = null;
  71. }
  72. }
  73. },
  74. scrollDown: function() {
  75. var vars = this.getScrollVars();
  76. if (vars.h + vars.st < vars.sh) {
  77. this.window.scrollTo(vars.sl, vars.st + this.speed);
  78. this.down = setTimeout(this.scrollDown, 50);
  79. }
  80. },
  81. scrollUp: function() {
  82. var vars = this.getScrollVars();
  83. if (vars.st > 0) {
  84. this.window.scrollTo(vars.sl, vars.st - this.speed);
  85. this.up = setTimeout(this.scrollUp, 50);
  86. }
  87. },
  88. scrollRight: function() {
  89. var vars = this.getScrollVars();
  90. if (vars.w + vars.sl < vars.sw) {
  91. this.window.scrollTo(vars.sl + this.speed, vars.st);
  92. this.right = setTimeout(this.scrollRight, 50);
  93. }
  94. },
  95. scrollLeft: function() {
  96. var vars = this.getScrollVars();
  97. if (vars.sl > 0) {
  98. this.window.scrollTo(vars.sl - this.speed, vars.st);
  99. this.left = setTimeout(this.scrollLeft, 50);
  100. }
  101. },
  102. getScrollVars: function() {
  103. var vars = {};
  104. if (this.window.document.documentElement.clientHeight) {
  105. vars.st = this.window.document.documentElement.scrollTop;
  106. vars.sl = this.window.document.documentElement.scrollLeft;
  107. vars.sw = this.window.document.documentElement.scrollWidth;
  108. vars.sh = this.window.document.documentElement.scrollHeight;
  109. vars.w = this.window.document.documentElement.clientWidth;
  110. vars.h = this.window.document.documentElement.clientHeight;
  111. }
  112. else {
  113. vars.st = this.window.document.body.scrollTop;
  114. vars.sl = this.window.document.body.scrollLeft;
  115. vars.sw = this.window.document.body.scrollWidth;
  116. vars.sh = this.window.document.body.scrollHeight;
  117. vars.w = this.window.document.body.clientWidth;
  118. vars.h = this.window.document.body.clientHeight;
  119. }
  120. return vars;
  121. }
  122. }