portal html css js resource

scrollfix.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. (function($) {
  2. //URI:http://caibaojian.com/scrollfix
  3. //author:caibaojian
  4. //website:http://caibaojian.com
  5. //descirption:scroll and fixed some div
  6. $.fn.scrollFix = function(options) {
  7. return this.each(function() {
  8. var opts = $.extend({}, $.fn.scrollFix.defaultOptions, options);
  9. var obj = $(this),
  10. base = this,
  11. selfTop = 0,
  12. selfLeft = 0,
  13. toTop = 0,
  14. parentOffsetLeft = 0,
  15. parentOffsetTop = 0,
  16. outerHeight,
  17. outerWidth,
  18. objWidth = 0,
  19. placeholder = jQuery('<div>'), //创建一个jquery对象
  20. optsTop = opts.distanceTop, //定义到顶部的高度
  21. endfix = 0; //开始停止固定的位置
  22. var originalPosition;
  23. var originalOffsetTop;
  24. var originalZIndex;
  25. var lastOffsetLeft = -1;
  26. var isUnfixed = true;
  27. var o_h =(window.document.body || window.document.documentElement).scrollHeight;
  28. //如果没有找到节点,不进行处理
  29. if (obj.length <= 0) {
  30. return;
  31. }
  32. if (lastOffsetLeft == -1) {
  33. originalZIndex = obj.css('z-index');
  34. position = obj.css('position');
  35. originalPosition = obj.css('position');
  36. originalOffsetTop = obj.css('top');
  37. }
  38. var zIndex = obj.css('zIndex');
  39. if (opts.zIndex != 0) {
  40. zIndex = opts.zIndex;
  41. }
  42. //获取相对定位或者绝对定位的父类
  43. var parents = obj.parent();
  44. var Position = parents.css('position');
  45. while (!/^relative|absolute$/i.test(Position)) { //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类
  46. parents = parents.parent();
  47. Position = parents.css('position');
  48. if (/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环
  49. }
  50. var ie6 = !-[1, ] && !window.XMLHttpRequest; //兼容IE6
  51. var resizeWindow = false;
  52. function resetScroll() {
  53. setUnfixed();
  54. selfTop = obj.offset().top; //对象距离顶部高度
  55. selfLeft = obj.offset().left+30; //对象距离左边宽度
  56. outerHeight = obj.outerHeight(); //对象高度
  57. outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0));
  58. outerWidth = obj.outerWidth(); //对象外宽度
  59. //objWidth = obj.width();
  60. objWidth = obj.outerWidth(true)+60;
  61. var documentHeight = $(document).height(); //文档高度
  62. var startTop = $(opts.startTop), //开始浮动固定对象
  63. startBottom = $(opts.startBottom),
  64. toBottom, //停止滚动位置距离底部的高度
  65. ScrollHeight; //对象滚动的高度
  66. //计算父类偏移值
  67. if (/^body|html$/i.test(parents[0].tagName)) { //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度
  68. parentOffsetTop = 0, parentOffsetLeft = 0;
  69. } else {
  70. parentOffsetLeft = parents.offset().left, parentOffsetTop = parents.offset().top;
  71. }
  72. // 计算父节点的上边到顶部距离
  73. // 如果 body 有 top 属性, 消除这些位移
  74. var bodyToTop = parseInt(jQuery('body').css('top'), 10);
  75. if (!isNaN(bodyToTop)) {
  76. optsTop += bodyToTop;
  77. }
  78. //计算停在底部的距离
  79. if (!isNaN(opts.endPos)) {
  80. toBottom = opts.endPos;
  81. } else {
  82. toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top);
  83. }
  84. //计算需要滚动的高度以及停止滚动的高度
  85. ScrollHeight = parseFloat(documentHeight - toBottom - optsTop), endfix = parseFloat(ScrollHeight - outerHeight);
  86. //计算顶部的距离值
  87. if (startTop[0]) {
  88. var startTopOffset = startTop.offset(),
  89. startTopPos = startTopOffset.top;
  90. selfTop = startTopPos;
  91. }
  92. if (startBottom[0]) {
  93. var startBottomOffset = startBottom.offset(),
  94. startBottomPos = startBottomOffset.top,
  95. startBottomHeight = startBottom.outerHeight();
  96. selfTop = parseFloat(startBottomPos + startBottomHeight);
  97. }
  98. toTop = selfTop - optsTop;
  99. toTop = (toTop > 0) ? toTop : 0;
  100. var selfBottom = documentHeight - selfTop - outerHeight;
  101. //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定
  102. if ((toBottom != 0) && (selfBottom <= toBottom)) {
  103. return;
  104. }
  105. }
  106. function setUnfixed() {
  107. if (!isUnfixed) {
  108. lastOffsetLeft = -1;
  109. placeholder.css("display", "none");
  110. obj.css({
  111. 'z-index': originalZIndex,
  112. 'width': '',
  113. 'position': originalPosition,
  114. 'left': '',
  115. 'top': originalOffsetTop,
  116. 'margin-left': ''
  117. });
  118. obj.removeClass('scrollfixed');
  119. isUnfixed = true;
  120. }
  121. }
  122. function onScroll() {
  123. lastOffsetLeft = 1;
  124. var ScrollTop = $(window).scrollTop();
  125. if (opts.bottom != -1) {
  126. ScrollTop = ScrollTop + $(window).height() - outerHeight - opts.bottom;
  127. }
  128. if (ScrollTop > toTop && (ScrollTop < endfix)) {
  129. if (ie6) { //IE6则使用这个样式
  130. obj.addClass(opts.baseClassName).css({
  131. "z-index": zIndex,
  132. "position": "absolute",
  133. "top": opts.bottom == -1 ? ScrollTop + optsTop - parentOffsetTop : ScrollTop - parentOffsetTop,
  134. "bottom": 'auto',
  135. "left": selfLeft - parentOffsetLeft,
  136. 'width': objWidth
  137. })
  138. } else {
  139. obj.addClass(opts.baseClassName).css({
  140. "z-index": zIndex,
  141. "position": "fixed",
  142. "top": opts.bottom == -1 ? optsTop : '',
  143. "bottom": opts.bottom == -1 ? '' : opts.bottom,
  144. "left": selfLeft,
  145. "width": objWidth
  146. });
  147. }
  148. placeholder.css({
  149. 'height': outerHeight,
  150. 'width': outerWidth,
  151. 'display': 'block'
  152. }).insertBefore(obj);
  153. } else if (ScrollTop >= endfix) {
  154. obj.addClass(opts.baseClassName).css({
  155. "z-index": zIndex,
  156. "position": "absolute",
  157. "top": endfix - parentOffsetTop + optsTop,
  158. 'bottom': '',
  159. "left": selfLeft - parentOffsetLeft,
  160. "width": objWidth
  161. });
  162. placeholder.css({
  163. 'height': outerHeight,
  164. 'width': outerWidth,
  165. 'display': 'block'
  166. }).insertBefore(obj)
  167. } else {
  168. obj.removeClass(opts.baseClassName).css({
  169. "z-index": originalZIndex,
  170. "position": "static",
  171. "top": "",
  172. "bottom": "",
  173. "left": ""
  174. });
  175. placeholder.remove()
  176. }
  177. }
  178. var Timer = 0;
  179. // if (isUnfixed) {
  180. resetScroll();
  181. // }
  182. $(window).on("scroll", function() {
  183. var n_h=(window.document.body || window.document.documentElement).scrollHeight;
  184. if (Timer) {
  185. clearTimeout(Timer);
  186. }
  187. if(n_h === o_h){
  188. Timer = setTimeout(onScroll, 0);
  189. }else{
  190. o_h = n_h;
  191. Timer = setTimeout(function(){
  192. isUnfixed=false;
  193. resetScroll();
  194. onScroll();
  195. },0);
  196. }
  197. });
  198. // $(window).on("scroll", function() {
  199. // if (Timer) {
  200. // clearTimeout(Timer);
  201. // }
  202. // Timer = setTimeout(onScroll, 0);
  203. // });
  204. // 当发现调整屏幕大小时,重新执行代码
  205. $(window).on("resize", function() {
  206. if (Timer) {
  207. clearTimeout(Timer);
  208. }
  209. Timer = setTimeout(function() {
  210. isUnfixed = false;
  211. resetScroll();
  212. onScroll();
  213. }, 0);
  214. });
  215. })
  216. }
  217. $.fn.scrollFix.defaultOptions = {
  218. startTop: null, //滑到这个位置顶部时开始浮动,默认为空
  219. startBottom: null, //滑到这个位置末端开始浮动,默认为空
  220. distanceTop: 0, //固定在顶部的高度
  221. endPos: 0, //停靠在底部的位置,可以为jquery对象
  222. bottom: -1, //底部位置
  223. zIndex: 0, //z-index值
  224. baseClassName: 'scrollfixed' //开始固定时添加的类
  225. };
  226. })(jQuery);