监视滚动事件 jQuery插件 ScrollWatch

  • 源码大小:12.33KB
  • 所需积分:1积分
  • 源码编号:19JP-3797
  • 浏览次数:1206次
  • 最后更新:2023年07月24日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

ScrollWatch是一个强大且可定制的jQuery scrollspy插件,它可以确定您正在查看的活动页面部分,并在垂直页面滚动上突出显示相应的导航链接。

既适用于可滚动容器,也适用于整个文档。

如何使用它:

1.在结束body标记之前包含jQuery库和jQuery ScrollWatch插件。

  1. <script src="//code.jquery.com/jquery.min.js"></script>
  2. <script src="src/scrollwatch.js"></script>

2.将ScrollWatch连接到给定的部分。

  1. // selections: selector or an array of elements
  2. // callback: function to invoke when the focus changes
  3. // options: option array
  4. $(sections).scrollWatch(callback, options)

3.当你向下滚动网页时,为导航链接分配一个“活动”类。

  1. // $(sections).scrollWatchMapTo(items, activeClass, options)
  2. $('section').scrollWatchMapTo('ul.menu > li', null, {
  3. resolutionMode: 'focus-line',
  4. viewMarginTop: 53
  5. });
  1. <ul class="menu">
  2. <li><a href="#s1">Section 1</a></li>
  3. <li><a href="#s2">Section 2/a></li>
  4. <li><a href="#s3">Section 3</a></li>
  5. ...
  6. </ul>

4.所有可能的自定义选项。

  1. {
  2.  
  3. // DOM element to spy scrolling events
  4. scroller: null,
  5.  
  6. // True: the callback is invoked only when the active section changes
  7. // False: the callback is invoked on every pulse (e.g. on scroll and resize).
  8. throttle: true,
  9.  
  10. // height - section that is occupying the largest portion of the view is chosen
  11. // focus-line - section that is directly intersecting or is closest to the focus line is chosen
  12. // custom - use a custom resolver
  13. // none - no resolution is performed (all candidates will be passed to the callback)
  14. resolutionMode: 'height',
  15.  
  16. // Function to invoke when a focus candidate resolution is needed.
  17. // It must choose and return a single focus object.
  18. resolver: null,
  19.  
  20. // in pixels
  21. topDownWeight: 0,
  22. viewMarginTop: 0,
  23. viewMarginBottom: 0,
  24. stickyOffsetTop: 5,
  25. stickyOffsetBottom: 5,
  26.  
  27. // When enabled, the space between two sections is considered a part of the first section.
  28. // Useful when there are large gaps between sections.
  29. clamp: false,
  30.  
  31. // Percentage of the view height that determines position of the focus line
  32. focusRatio: 0.38196601125010515,
  33.  
  34. // Offset added to position of the focus line position after focusRatio is applied
  35. focusOffset: 0,
  36.  
  37. // Debug mode
  38. debugFocusLine: false
  39. }

更新日志:

v2.2.0 (2021-11-28)

  • 修复回退
  • 添加“夹具”选项

预览截图