scrollfix

JQuery辅助文本框在浏览器中固定位置,不随浏览器滚动条滚动

当前为 2019-08-10 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/388372/723756/scrollfix.js

  1. // ==UserScript==
  2. // @name scrollfix
  3. // @version 0.0.1
  4. // @namespace tsharp.js
  5. // @description JQuery辅助文本框在浏览器中固定位置,不随浏览器滚动条滚动
  6. // @author jimbo
  7. // @license GPLv3
  8. // @match *
  9. // @icon none
  10. // @require http://code.jquery.com/jquery-3.4.1.min.js
  11. // ==/UserScript==
  12. /*
  13. * fixedScroll.js 滚动固定插件
  14. * @DH
  15. * https://denghao.me
  16. *
  17. * 示例:
  18. * $('.box').fixedScroll()
  19. */
  20. (function () {
  21. var fixedScroll = function ($fixedEl, opts) {
  22. this.defaults = {
  23. navEls: '', //nav (注意: navEls和hookEls两个参数必须成对出现)
  24. hookEls: '', //nav要滚动到的对应元素
  25. hookOffset: 0, //hook区域顶部偏移量
  26. offset: 0, //固定元素顶部偏移量
  27. stickEndEl: '', //固定结束位置的元素
  28. callback: ''
  29. };
  30. $.extend(this, this.defaults, opts);
  31. this.flag = true;
  32. this.stickTop = 0; //固定元素的原始位置
  33. this.init_stickTop = 0; //用于重计算高度
  34. this.stickBottom = 9999999; //固定元素的结束位置
  35. this.fixedEl = $fixedEl; //固定元素
  36. this.fixedElH = $fixedEl.height();
  37. this.fixedElW = $fixedEl.width();
  38. this.fixedElL = $fixedEl.offset().left;
  39. this.winEl = $(window);
  40. this.offset = parseInt(this.offset || 0);
  41. this.hookArea = [];
  42. this.isClickSwitch = false;
  43. }
  44. fixedScroll.prototype = {
  45. init: function () {
  46. if (this.fixedEl.length > 0) {
  47. this.stickTop = this.fixedEl.offset().top;
  48. this.init_stickTop = this.stickTop;
  49. }
  50.  
  51. if (this.stickEndEl.length > 0) {
  52. this.stickBottom = this.stickEndEl.offset().top;
  53. }
  54. // 限定起始位的距顶高度
  55. this.distance = this.stickBottom - this.stickTop - this.fixedElH - this.offset / 2;
  56. this.calcArea();
  57. this.flag && this.events();
  58. this.flag = false;
  59. },
  60. // 固定
  61. stickHandle: function () {
  62. if (this.winEl.scrollTop() > this.stickTop - this.offset) {
  63. if (this.winEl.scrollTop() < this.stickBottom - this.fixedElH - this.offset) {
  64. this.fixedEl.css({
  65. "position": "fixed",
  66. "top": this.offset,
  67. "left": this.fixedElL,
  68. "width": this.fixedElW,
  69. "height": this.fixedElH,
  70. "transform": "translateY(0)"
  71. });
  72. typeof this.callback == 'function' && this.callback(1);
  73. } else {
  74. this.fixedEl.css({
  75. "top": "auto",
  76. "left": "auto",
  77. "position": "static",
  78. "transform": "translateY(" + this.distance + "px)"
  79. });
  80. typeof this.callback == 'function' && this.callback(0);
  81. }
  82. } else {
  83. this.fixedEl.css({
  84. "top": "auto",
  85. "position": "static"
  86. });
  87. typeof this.callback == 'function' && this.callback(0);
  88. }
  89. },
  90.  
  91. // 动态计算高度
  92. resizeHeight: function (hasNewTop) {
  93. if (this.fixedEl.length > 0) {
  94. this.stickTop = hasNewTop ? this.fixedEl.offset().top : this.init_stickTop;
  95. }
  96. if (this.stickEndEl.length > 0) {
  97. this.stickBottom = this.stickEndEl.offset().top;
  98. }
  99. this.distance = this.stickBottom - this.stickTop - this.fixedElH - this.offset / 2;
  100. this.calcArea();
  101. },
  102.  
  103. // 计算滚动区
  104. calcArea: function () {
  105. if (this.hookEls.length <= 0) return;
  106. var areas = [];
  107. this.hookEls.each(function (i, ele) {
  108. var start = $(this).offset().top;
  109. var end = start + $(this).height();
  110. areas.push([start, end]);
  111. })
  112. this.hookArea = areas;
  113. },
  114.  
  115. // 区域判断
  116. hookScroll: function () {
  117. var t = this.winEl.scrollTop();
  118. for (var i in this.hookArea) {
  119. if ((t > this.hookArea[i][0] - this.hookOffset) && t < this.hookArea[i][1]) {
  120. this.navStatus(i)
  121. } else {
  122. this.navStatus()
  123. }
  124. }
  125. },
  126.  
  127. // nav状态
  128. navStatus: function (i) {
  129. if (i || +i === 0) {
  130. this.navEls.eq(i).addClass('active').siblings().removeClass('active');
  131. } else {
  132. this.navEls.eq(i).removeClass('active');
  133. }
  134. },
  135.  
  136. // 滚动到指定位置
  137. refresh: function (i) {
  138. this.calcArea();
  139. var top = this.hookArea[i][0] - this.hookOffset;
  140. this.scrollTop(top, 120);
  141. },
  142.  
  143. scrollTop: function (scrollTo, time) {
  144. var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  145. var scrollFrom = parseInt(scrollTop),
  146. i = 0,
  147. step = 5;
  148. scrollTo = parseInt(scrollTo);
  149. time /= step;
  150. var interval = setInterval(function () {
  151. i++;
  152. var top = (scrollTo - scrollFrom) / time * i + scrollFrom;
  153. document.body.scrollTop = top;
  154. document.documentElement.scrollTop = top;
  155. if (i >= time) {
  156. clearInterval(interval);
  157. }
  158. }, step)
  159. },
  160.  
  161. events: function () {
  162. var _this = this;
  163. // 切换nav
  164. if (_this.navEls.length > 0) {
  165. this.navEls.on('click', function () {
  166. var i = $(this).index();
  167. _this.isClickSwitch = true;
  168. _this.refresh(i);
  169. _this.navStatus(i);
  170. setTimeout(function () {
  171. _this.isClickSwitch = false;
  172. }, 300);
  173. })
  174. }
  175. // 滚动监听
  176. this.winEl.on("scroll", function () {
  177. (_this.fixedEl.length > 0) && _this.stickHandle();
  178. (_this.hookEls.length > 0 && !_this.isClickSwitch) && _this.hookScroll();
  179. });
  180. }
  181. }
  182.  
  183. $.fn.fixedScroll = function (opts) {
  184. var drag = new fixedScroll(this, opts);
  185. drag.init();
  186. return drag;
  187. }
  188.  
  189. //兼容模块
  190. if (typeof module !== 'undefined' && typeof exports === 'object') {
  191. module.exports = fixedScroll;
  192. } else if (typeof define === 'function' && (define.amd || define.cmd)) {
  193. define(function () {
  194. return fixedScroll;
  195. })
  196. } else {
  197. window.fixedScroll = fixedScroll;
  198. }
  199. }).call(function () {
  200. return (typeof window !== 'undefined' ? window : global)
  201. }, $)

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址