scroll.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. (function(window,doc){
  2. var style = document.createElement('style');
  3. style.type = 'text/css';
  4. style.innerHTML='.loadDiv{margin-top:-60px;text-align: center;position: relative;\
  5. width:100%;height:60px;-webkit-transition: margin-top .3s;transition: margin-top .3s;}\
  6. .pullDown {\
  7. position: relative;display:inline-block;border-radius: 50%;height: 40px;width: 40px;\
  8. background: none repeat scroll 0 0 #fff;overflow:hidden;\
  9. box-shadow: 0 0 10px rgba(0,0,0,.1) inset, 0 0 25px rgba(0,0,255,0.075);}\
  10. .pullDown:after {\
  11. content: "";position: absolute;top: 3px; left: 3px;display: block;\
  12. height: 34px; width: 34px;background: none repeat scroll 0 0 #fff;\
  13. border-radius: 50%;}\
  14. .pullDown > span {position: absolute;height: 100%; width: 50%;overflow: hidden;}\
  15. .labelFont{font-size:13px;color:#ccc;}\
  16. .left { left:0 }\
  17. .right { left:50% }\
  18. .anim {\
  19. position: absolute;left: 100%; top: 0;height: 100%; width: 100%;\
  20. border-radius: 20px;background: none repeat scroll 0 0 #ddd;\
  21. opacity: 0.8;-webkit-animation: ui-spinner-rotate-left 3s infinite;\
  22. animation: ui-spinner-rotate-left 3s infinite;\
  23. -webkit-transform-origin: 0 50% 0;transform-origin: 0 50% 0;\
  24. -webkit-animation-delay: 0s;-webkit-animation-duration:3s;\
  25. -webkit-animation-timing-function: linear;\
  26. animation-delay: 0s;animation-duration:3s;\
  27. animation-timing-function: linear;}\
  28. .left .anim {border-bottom-left-radius: 0;border-top-left-radius: 0;}\
  29. .right .anim {\
  30. border-bottom-right-radius: 0;border-top-right-radius: 0;left: -100%;\
  31. -webkit-transform-origin: 100% 50% 0;transform-origin: 100% 50% 0;\
  32. -webkit-animation-name: ui-spinner-rotate-right;\
  33. -webkit-animation-delay:0;-webkit-animation-delay: 1.5s;\
  34. animation-name: ui-spinner-rotate-right;\
  35. animation-delay:0;animation-delay: 1.5s;}\
  36. @keyframes ui-spinner-rotate-left{\
  37. 0%,25%{transform:rotate(0deg)}\
  38. 50%,75%{transform:rotate(180deg)}\
  39. 100%{transform:rotate(360deg)}\
  40. }\
  41. @-webkit-keyframes ui-spinner-rotate-left{\
  42. 0%,25%{-webkit-transform:rotate(0deg)}\
  43. 50%,75%{-webkit-transform:rotate(180deg)}\
  44. 100%{-webkit-transform:rotate(360deg)}\
  45. }\
  46. @-webkit-keyframes ui-spinner-rotate-right{\
  47. 0%{-webkit-transform:rotate(0deg)}\
  48. 25%,50%{-webkit-transform:rotate(180deg)}\
  49. 75%,100%{-webkit-transform:rotate(360deg)}\
  50. }\
  51. @keyframes ui-spinner-rotate-right{\
  52. 0%{transform:rotate(0deg)}\
  53. 25%,50%{transform:rotate(180deg)}\
  54. 75%,100%{transform:rotate(360deg)}\
  55. }';
  56. document.getElementsByTagName('HEAD').item(0).appendChild(style);
  57. var div = doc.createElement('div');
  58. div.className = 'loadDiv';
  59. var span = doc.createElement('span');
  60. span.className = 'pullDown';
  61. var lspan = doc.createElement('span');
  62. lspan.className = 'left';
  63. var lanimSpan = doc.createElement('span');
  64. lanimSpan.className = 'anim';
  65. lspan.appendChild(lanimSpan);
  66. span.appendChild(lspan);
  67. var rspan = doc.createElement('span');
  68. rspan.className = 'right';
  69. var ranimSpan = doc.createElement('span');
  70. ranimSpan.className = 'anim';
  71. rspan.appendChild(ranimSpan);
  72. span.appendChild(rspan);
  73. var label = doc.createElement('div');
  74. label.id = "pullLabel";
  75. label.className = "labelFont";
  76. label.innerHTML = "下拉刷新";
  77. div.appendChild(span);
  78. div.appendChild(label);
  79. //var first=document.body.firstChild;//得到页面的第一个元素
  80. var wrap = doc.getElementsByClassName("scrollWrap")[0];
  81. doc.body.insertBefore(div,wrap);//把动态创建的div添加到下拉容器的上面
  82. isTouchPad = (/hp-tablet/gi).test(navigator.appVersion),
  83. hasTouch = 'ontouchstart' in window && !isTouchPad,
  84. START_EV = hasTouch ? 'touchstart' : 'mousedown',
  85. MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
  86. END_EV = hasTouch ? 'touchend' : 'mouseup',
  87. CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup';
  88. var qScroll = function(callBack){
  89. var that = this;
  90. that.callBack = callBack;
  91. that._bind(START_EV,wrap);
  92. };
  93. qScroll.prototype = {
  94. handleEvent: function (e) {
  95. var that = this;
  96. switch(e.type) {
  97. case START_EV:
  98. if (!hasTouch && e.button !== 0) return;
  99. that._start(e);
  100. break;
  101. case MOVE_EV: that._move(e); break;
  102. case END_EV:
  103. case CANCEL_EV: that._end(e); break;
  104. }
  105. },
  106. _start:function(e){
  107. var self = this,point = hasTouch ? e.touches[0] : e;
  108. //记录刚刚开始按下的时间
  109. self.startTime = new Date() * 1;
  110. //记录手指按下的坐标
  111. self.startY = point.pageY;
  112. self.actionDir = '';
  113. self._bind(MOVE_EV, window);
  114. self._bind(END_EV, window);
  115. self._bind(CANCEL_EV, window);
  116. },
  117. _move:function(e){
  118. var self = this,point = hasTouch ? e.touches[0] : e;
  119. //兼容chrome android,阻止浏览器默认行为
  120. e.preventDefault();
  121. //计算手指的偏移量
  122. self.offsetY = point.pageY - self.startY;
  123. if(self.offsetY > 0){//下拉
  124. self.actionDir = 'down';
  125. if (self.offsetY > 60) {
  126. self.offsetY = 60;
  127. label.innerHTML="释放即可刷新";
  128. }
  129. div.style.cssText = "margin-top:"+(self.offsetY-60)+"px";
  130. wrap.style.cssText = "margin-top:"+self.offsetY+"px";
  131. var endTime = new Date() * 1;
  132. if(endTime-self.startTime > 2000){
  133. div.style.cssText = "margin-top:-60px";
  134. }
  135. }else{//上拉
  136. self.actionDir = 'up';
  137. //可以扩展上拉的需求
  138. }
  139. },
  140. _end:function(e){
  141. var that = this;
  142. e.preventDefault();
  143. if(that.actionDir == 'down'){
  144. label.innerHTML="下拉刷新";
  145. div.style.cssText = "margin-top:-60px";
  146. wrap.style.cssText = "margin-top:0px";
  147. }
  148. if(that.callBack instanceof Function){
  149. that.callBack();//执行回调函数
  150. }
  151. that._unbind(MOVE_EV, window);
  152. that._unbind(END_EV, window);
  153. that._unbind(CANCEL_EV, window);
  154. },
  155. _bind: function (type, el, bubble) {
  156. el.addEventListener(type, this, !!bubble);
  157. },
  158. _unbind: function (type, el, bubble) {
  159. el.removeEventListener(type, this, !!bubble);
  160. }
  161. };
  162. if (typeof exports !== 'undefined') {
  163. exports.qScroll = qScroll;
  164. }else{
  165. window.qScroll = qScroll;
  166. }
  167. })(window,document)