Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. (function registerScrolling($) {
  2. var prevTouchPosition = {},
  3. scrollYClass = 'scroll-y',
  4. scrollXClass = 'scroll-x',
  5. searchTerms = '.' + scrollYClass + ', .' + scrollXClass;
  6.  
  7. $('body').on('touchstart', function (e) {
  8. var $scroll = $(e.target).closest(searchTerms),
  9. targetTouch = e.originalEvent.targetTouches[0];
  10.  
  11. // Store previous touch position if within a scroll element
  12. prevTouchPosition = $scroll.length ? { x: targetTouch.pageX, y: targetTouch.pageY } : {};
  13. });
  14.  
  15. $('body').on('touchmove', function (e) {
  16. var $scroll = $(e.target).closest(searchTerms),
  17. targetTouch = e.originalEvent.targetTouches[0];
  18.  
  19. if (prevTouchPosition && $scroll.length) {
  20. // Set move helper and update previous touch position
  21. var move = {
  22. x: targetTouch.pageX - prevTouchPosition.x,
  23. y: targetTouch.pageY - prevTouchPosition.y
  24. };
  25. prevTouchPosition = { x: targetTouch.pageX, y: targetTouch.pageY };
  26.  
  27. // Check for scroll-y or scroll-x classes
  28. if ($scroll.hasClass(scrollYClass)) {
  29. var scrollHeight = $scroll[0].scrollHeight,
  30. outerHeight = $scroll.outerHeight(),
  31.  
  32. atUpperLimit = ($scroll.scrollTop() === 0),
  33. atLowerLimit = (scrollHeight - $scroll.scrollTop() === outerHeight);
  34.  
  35. if (scrollHeight > outerHeight) {
  36. // If at either limit move 1px away to allow normal scroll behavior on future moves,
  37. // but stop propagation on this move to remove limit behavior bubbling up to body
  38. if (move.y > 0 && atUpperLimit) {
  39. $scroll.scrollTop(1);
  40. e.stopPropagation();
  41. } else if (move.y < 0 && atLowerLimit) {
  42. $scroll.scrollTop($scroll.scrollTop() - 1);
  43. e.stopPropagation();
  44. }
  45.  
  46. // If only moving right or left, prevent bad scroll.
  47. if(Math.abs(move.x) > 0 && Math.abs(move.y) < 3){
  48. e.preventDefault()
  49. }
  50.  
  51. // Normal scrolling behavior passes through
  52. } else {
  53. // No scrolling / adjustment when there is nothing to scroll
  54. e.preventDefault();
  55. }
  56. } else if ($scroll.hasClass(scrollXClass)) {
  57. var scrollWidth = $scroll[0].scrollWidth,
  58. outerWidth = $scroll.outerWidth(),
  59.  
  60. atLeftLimit = $scroll.scrollLeft() === 0,
  61. atRightLimit = scrollWidth - $scroll.scrollLeft() === outerWidth;
  62.  
  63. if (scrollWidth > outerWidth) {
  64. if (move.x > 0 && atLeftLimit) {
  65. $scroll.scrollLeft(1);
  66. e.stopPropagation();
  67. } else if (move.x < 0 && atRightLimit) {
  68. $scroll.scrollLeft($scroll.scrollLeft() - 1);
  69. e.stopPropagation();
  70. }
  71. // If only moving up or down, prevent bad scroll.
  72. if(Math.abs(move.y) > 0 && Math.abs(move.x) < 3){
  73. e.preventDefault();
  74. }
  75.  
  76. // Normal scrolling behavior passes through
  77. } else {
  78. // No scrolling / adjustment when there is nothing to scroll
  79. e.preventDefault();
  80. }
  81. }
  82. } else {
  83. // Prevent scrolling on non-scrolling elements
  84. e.preventDefault();
  85. }
  86. });
  87. })(jQuery);
  88.  
  89. function _touchScrollingHandler(event) {
  90. var p = event.target;
  91. var pos;
  92. var scrollable = null;
  93. do {
  94. if (!p.computedStyle) {
  95. continue;
  96. }
  97. var overflow = p.computedStyle().overflow;
  98. var hiddenHeight = p.scrollHeight - p.offsetHeight;
  99. var s = (['hidden', 'visible'].indexOf(overflow) < 0);
  100. if ((s || p.tagName === 'HTML') && hiddenHeight > 0) {
  101. if ((Q.Pointer.movement.positions.length == 1)
  102. && (pos = Q.Pointer.movement.positions[0])) {
  103. var sy = Q.Pointer.getY(event)
  104. + Q.Pointer.scrollTop();
  105. if ((sy > pos.y && p.scrollTop == 0)
  106. || (sy < pos.y && p.scrollTop >= hiddenHeight)) {
  107. continue;
  108. }
  109. }
  110. scrollable = p;
  111. break;
  112. }
  113. } while (p = p.parentNode);
  114. if (!scrollable) {
  115. Q.Pointer.preventDefault(event);
  116. }
  117. }
  118.  
  119. var Q = {
  120. preventTouchScrolling: function () {
  121. Q.addEventListener(window, 'touchmove', _touchScrollingHandler);
  122. },
  123. restoreTouchScrolling: function () {
  124. Q.removeEventListener(window, 'touchmove', _touchScrollingHandler);
  125. }
  126. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement