scrollfix.min.js 7.0 KB

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