zhiqim_scroll_screen.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
  3. *
  4. * 指定登记&发行网站: https://www.zhiqim.com/ 欢迎加盟知启蒙,[编程有你,知启蒙一路随行]。
  5. *
  6. * 本文采用《知启蒙登记发行许可证》,除非符合许可证,否则不可使用该文件!
  7. * 1、您可以免费使用、修改、合并、出版发行和分发,再授权软件、软件副本及衍生软件;
  8. * 2、您用于商业用途时,必须在原作者指定的登记网站,按原作者要求进行登记;
  9. * 3、您在使用、修改、合并、出版发行和分发时,必须包含版权声明、许可声明,及保留原作者的著作权、商标和专利等知识产权;
  10. * 4、您在互联网、移动互联网等大众网络下发行和分发再授权软件、软件副本及衍生软件时,必须在原作者指定的发行网站进行发行和分发;
  11. * 5、您可以在以下链接获取一个完整的许可证副本。
  12. *
  13. * 许可证链接:http://zhiqim.org/licenses/zhiqim_register_publish_license.htm
  14. *
  15. * 除非法律需要或书面同意,软件由原始码方式提供,无任何明示或暗示的保证和条件。详见完整许可证的权限和限制。
  16. */
  17. +(function(Z)
  18. {
  19. //BEGIN
  20. /************************************************************************************************/
  21. //滚屏,每次滚轮滚动一个屏幕高度,并增加上下两个按钮点击切换屏幕,支持指定固定顶部高度和滚动速度
  22. /************************************************************************************************/
  23. //var sScreen = new Z.ScrollScreen();
  24. //sScreen.fixHeight = 55;
  25. //sScreen.execute();
  26. Z.ScrollScreen = Z.Class.newInstance();
  27. Z.ScrollScreen.prototype =
  28. {
  29. defaults:
  30. {
  31. speed: 13,
  32. times: 13,
  33. fixHeight:0,
  34. zIndex: 0
  35. },
  36. execute: function()
  37. {
  38. //支持滚屏的对象
  39. this.$screen = Z("[data-role=z-screen]");
  40. //监听初始化事件、窗口变化事件和滚轮事件
  41. if (!Z.B.firefox)
  42. {//非火狐刷新时执行window.scrollTo(0, 0)后会被浏览器跳转到原来位置,所以预先scrollTo(0, 0)
  43. Z(window).beforeunload(this.onLoad, this);
  44. }
  45. Z(window).load(this.onLoad, this).resize(this.onResize, this);
  46. Z(document).mousewheel(this.onMouseWheel, this);
  47. //向上按钮
  48. this.$up = Z('<div class="z-fixed z-w30 z-h30 z-bd-rd50p z-pointer"><i class="z-arrow z-up z-px5 z-absolute" style="margin:auto;top:0;left:0;bottom:0;right:0;"><span style="border-bottom-color:#d3d3d3"></span></i></div>');
  49. this.$up.appendTo("body").css({top: this.fixHeight+20, right: 20}).css({backgroundColor: "#d3d3d3", zIndex: this.zIndex})
  50. .click(this.onUp, this).mouseover(this.onUpMouseOver, this).mouseout(this.onUpMouseOut, this);
  51. //向下按钮
  52. this.$down = Z('<div class="z-fixed z-w30 z-h30 z-bd-rd50p z-pointer"><i class="z-arrow z-px5 z-absolute" style="margin:auto;top:0;left:0;bottom:0;right:0;"><span style="border-top-color:#d3d3d3"></span></i></div>');
  53. this.$down.appendTo("body").css({bottom: 20, right: 20}).css({backgroundColor: "#d3d3d3", zIndex: this.zIndex})
  54. .click(this.onDown, this).mouseover(this.onDownMouseOver, this).mouseout(this.onDownMouseOut, this);
  55. //初始化高度和设置滚屏高度
  56. this.onResize();
  57. },
  58. onLoad: function(e)
  59. {//刷新时回最上面
  60. window.scrollTo(0, 0);
  61. },
  62. onResize: function(e)
  63. {//重新设置高度和滚屏高度
  64. window.scrollTo(0, 0);
  65. this.running = false;
  66. this.height = Z.D.clientHeight() - this.fixHeight;
  67. this.$screen.css("height", this.height);
  68. this.maxHeight = Z.D.scrollHeight() - Z.D.clientHeight();
  69. this.$up.hide();
  70. },
  71. onUpMouseOver: function(e)
  72. {
  73. this.$up.css("backgroundColor", "#333")
  74. .find("i").css("borderBottomColor", "#fff")
  75. .find("span").css("borderBottomColor", "#333");
  76. },
  77. onUpMouseOut: function(e)
  78. {
  79. this.$up.css("backgroundColor", "#d3d3d3")
  80. .find("i").css("borderBottomColor", "#000")
  81. .find("span").css("borderBottomColor", "#d3d3d3");
  82. },
  83. onDownMouseOver: function(e)
  84. {
  85. this.$down.css("backgroundColor", "#333")
  86. .find("i").css("borderTopColor", "#fff")
  87. .find("span").css("borderTopColor", "#333");
  88. },
  89. onDownMouseOut: function(e)
  90. {
  91. this.$down.css("backgroundColor", "#d3d3d3")
  92. .find("i").css("borderTopColor", "#000")
  93. .find("span").css("borderTopColor", "#d3d3d3");
  94. },
  95. onUp: function()
  96. {//点击向上按钮
  97. if (this.running)
  98. return;
  99. this.running = true;
  100. this.scrollTo(false);
  101. },
  102. onDown: function()
  103. {//点击向下按钮
  104. if (this.running)
  105. return;
  106. this.running = true;
  107. this.scrollTo(true);
  108. },
  109. onMouseWheel: function(e)
  110. {//滚动滚轮
  111. Z.E.forbidden(e);
  112. if (this.running)
  113. return;
  114. this.running = true;
  115. this.scrollTo(Z.E.wheelDelta(e) < 0);
  116. },
  117. scrollTo: function(down)
  118. {//滚动操作
  119. var curHeight = Z.D.scrollTop();
  120. var index = 1;var endHeight, midHeight;
  121. if (down)
  122. {//向下
  123. if (this.maxHeight <= curHeight){
  124. this.running = false;
  125. return;
  126. }
  127. endHeight = curHeight + this.height;
  128. midHeight = Math.floor(this.height / this.times);
  129. Z.timer(this.speed, this.times, this,
  130. function(){window.scrollTo(0, curHeight + midHeight * index++);},
  131. function(){window.scrollTo(0, endHeight);this.running=false;}
  132. );
  133. if (curHeight >= this.maxHeight - this.height)
  134. this.$down.fadeOut(this.speed * this.times);
  135. if (curHeight == 0)
  136. this.$up.fadeIn(this.speed * this.times);
  137. }
  138. else
  139. {//向上
  140. if (curHeight <= 0){
  141. this.running = false;
  142. return;
  143. }
  144. if (this.maxHeight == curHeight)
  145. {
  146. endHeight = Math.floor(curHeight / this.height) * this.height;
  147. this.$down.fadeIn(this.speed * this.times);
  148. }
  149. else
  150. {
  151. endHeight = curHeight - this.height;
  152. }
  153. midHeight = (curHeight - endHeight) / this.times;
  154. Z.timer(this.speed, this.times, this,
  155. function(){window.scrollTo(0, curHeight - midHeight * index++);},
  156. function(){window.scrollTo(0, endHeight);this.running=false;}
  157. );
  158. if (endHeight <= 0)
  159. this.$up.fadeOut(this.speed * this.times);
  160. }
  161. }
  162. };
  163. Z.ScrollScreen.onload = function(f)
  164. {//增加滚屏加载,当本JS加载完成之后才加载函数
  165. Z.onload(f);
  166. };
  167. //END
  168. })(zhiqim);