Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>status-bar tap event</title>
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. </head>
  7. <body style="margin: 0;">
  8. <div id="hidden-header" style="position: relative; z-index: 2; background: #808080;">hidden-header</div>
  9. <div id="header" style="position: relative; z-index: 2; background: #cccccc;">header</div>
  10. <div id="container" style="overflow: auto; position: relative; z-index: 1;">
  11. <div id="scrollable">
  12. <div id="dummy-content" style="height: 2000px; background: linear-gradient(to bottom, #ffffff 0%, #000000 100%);">
  13. <p>dummy</p>
  14. <p>dummy</p>
  15. <p>dummy</p>
  16. <p>dummy</p>
  17. <p>dummy</p>
  18. <p>dummy</p>
  19. <p>dummy</p>
  20. <p>dummy</p>
  21. <p>dummy</p>
  22. <p>dummy</p>
  23. <p>dummy</p>
  24. <p>dummy</p>
  25. <p>dummy</p>
  26. <p>dummy</p>
  27. <p>dummy</p>
  28. <p>dummy</p>
  29. <p>dummy</p>
  30. <p>dummy</p>
  31. <p>dummy</p>
  32. <p>dummy</p>
  33. </div>
  34. </div>
  35. </div>
  36.  
  37. <script type="text/javascript">
  38. var $body = document.body || document.getElementsByTagName('body')[0];
  39. var $hiddenHeader = document.getElementById('hidden-header');
  40. var $header = document.getElementById('header');
  41. var $container = document.getElementById('container');
  42. var $scrollable = document.getElementById('scrollable');
  43.  
  44. var HEADER_HEIGHT = 60;
  45. var containerHeight = window.innerHeight - HEADER_HEIGHT;
  46.  
  47. $hiddenHeader.style.height = HEADER_HEIGHT + 'px';
  48. $header.style.height = HEADER_HEIGHT + 'px';
  49. $container.style.height = containerHeight + 'px';
  50. $body.style.height = (containerHeight + HEADER_HEIGHT * 2) + 'px';
  51.  
  52. //
  53. // detect status-bar tap
  54. //
  55. var isTouching = false;
  56. window.addEventListener('touchstart', function () {
  57. isTouching = true;
  58. })
  59. window.addEventListener('touchend', function () {
  60. isTouching = false;
  61. })
  62. window.addEventListener('scroll', function () {
  63. // wait while touching
  64. if ($body.scrollTop === 0 && !isTouching) {
  65. console.log('status-bar is probably tapped because scrollTop is returned to zero');
  66. scrollContainer(0, currentScrollTop);
  67. }
  68. });
  69.  
  70. //
  71. // container scroll
  72. //
  73.  
  74. var currentScrollTop = 0;
  75.  
  76. var lastTouchedScreenY;
  77. $container.addEventListener('touchstart', function (event) {
  78. lastTouchedScreenY = event.changedTouches[0].screenY;
  79. });
  80. $container.addEventListener('touchmove', function (event) {
  81. event.preventDefault();
  82.  
  83. // need to set scrollTop not zero
  84. hideHiddenHeader();
  85.  
  86. var deltaY = lastTouchedScreenY - event.changedTouches[0].screenY;
  87. var newScrollTop = currentScrollTop + deltaY;
  88. newScrollTop < 0 && (newScrollTop = 0);
  89. newScrollTop > $container.innerHeight - containerHeight && (newScrollTop = $container.innerHeight - containerHeight);
  90. scrollContainer(newScrollTop);
  91.  
  92. lastTouchedScreenY = event.changedTouches[0].screenY;
  93. });
  94.  
  95. var isAnimate = false;
  96. var animationLoop;
  97. function scrollContainer(scrollTop, duration, startTime) {
  98. startTime || (startTime = +new Date);
  99. var elapsedTime = +new Date - startTime;
  100. if (duration && isAnimate) {
  101. return;
  102. }
  103. if (duration && elapsedTime <= duration) {
  104. var progress = elapsedTime / duration;
  105. progress > 1 && (progress = 1);
  106. var _scrollTop = currentScrollTop + (scrollTop - currentScrollTop) * (1 - Math.pow(1 - progress, 4));
  107. $scrollable.style.transform = $scrollable.style['-webkit-transform'] = 'translate(0, ' + (-_scrollTop) + 'px) translate3d(0, 0, 0)';
  108.  
  109. setTimeout(function () {
  110. scrollContainer(scrollTop, duration, startTime);
  111. }, 17)
  112. return;
  113. }
  114. clearTimeout(animationLoop);
  115. isAnimate = false;
  116.  
  117. currentScrollTop = scrollTop;
  118. $scrollable.style.transform = $scrollable.style['-webkit-transform'] = 'translate(0, ' + (-scrollTop) + 'px) translate3d(0, 0, 0)';
  119. }
  120.  
  121. //
  122. // hide #hidden-header
  123. //
  124.  
  125. function hideHiddenHeader() {
  126. window.scrollTo(0, HEADER_HEIGHT);
  127. }
  128.  
  129. setTimeout(function () {
  130. hideHiddenHeader();
  131. }, 500);
  132. </script>
  133. </body>
  134. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement