jQuery和CSS 超薄单页滚动指示器

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

简介

一个细长的垂直滚动条,可以自动生成指向文档中不同部分的节点(锚链接)。非常适合单页滚动网站和全页时间表。用jQuery和CSS/CS3编写。

如何使用它:

1.为垂直滚动条创建一个emty HTML列表。

  1. <ul class="indicator">
  2. <li class="bar"></li>
  3. </ul>

2.在网页中添加内容部分。请注意,每个部分都必须有一个唯一的数据名称:

  1. <div class="container">
  2. <section data-name="Home">
  3. <h1>Slim One Page Scroll Indicator Example</h1>
  4. <p>A slim vertical scrollbar that automatically generates nodes (anchor links) pointing to different sections within the document. Great for one page scrolling website and fullpage timelines. Written in jQuery and CSS/CSS3.
  5. </p>
  6. </section>
  7. <section data-name="Two">
  8. <h1>Section Two</h1>
  9. </section>
  10. <section data-name="Three">
  11. <h1>Section Three</h1>
  12. </section>
  13. ... more sections here ..
  14. </div>

3.设置垂直滚动条的样式。

  1. .indicator {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. position: fixed;
  6. left: 100px;
  7. top: 5%;
  8. width: 1px;
  9. height: 90%;
  10. background: #464c63;
  11. }
  12.  
  13. .indicator .bar {
  14. position: absolute;
  15. width: 1px;
  16. background-color: #fcf769;
  17. top: 0;
  18. left: 0;
  19. z-index: 0;
  20. }

4.设置滚动条内节点(锚链接)的样式。

  1. .node {
  2. position: absolute;
  3. width: 3px;
  4. height: 3px;
  5. background: #FFF;
  6. left: -1px;
  7. z-index: 1;
  8. cursor: pointer;
  9. border-radius: 3px;
  10. }
  11.  
  12. .node:hover {
  13. background: #46ffdd;
  14. }
  15.  
  16. .node:hover span {
  17. opacity: 1;
  18. }
  19.  
  20. .node:before {
  21. content: "";
  22. position: absolute;
  23. width: 9px;
  24. height: 9px;
  25. left: -3px;
  26. top: -3px;
  27. }
  28.  
  29. .node span {
  30. transition: all 0.4s ease-out;
  31. text-transform: uppercase;
  32. right: 4px;
  33. top: -16px;
  34. color: #FFFFFF;
  35. position: absolute;
  36. padding: 10px;
  37. white-space: nowrap;
  38. font-size: 10px;
  39. font-weight: 200;
  40. opacity: 0;
  41. }

5.包括必要的jQuery库。

  1. <script src="/path/to/cdn/jquery.min.js"></script>

6.用于激活滚动条的主JavaScript。

  1. $(function () {
  2.  
  3. function sumSection() {
  4. return $(".container").height();
  5. }
  6.  
  7. function setDimensionBar() {
  8. $(".bar").css({
  9. "height": $(window).height() / sumSection() * 100 + "%" });
  10. }
  11. function setSection() {
  12. $.each($("section"), function (i, element) {
  13. $(element).css({
  14. "min-height": $(window).height() });
  15. });
  16. }
  17.  
  18. function addBehaviours() {
  19. let sections = $("section");
  20. $.each($(".node"), function (i, element) {
  21. $(element).on("click", function (e) {
  22. e.preventDefault();
  23. let scroll = $(sections[i]).offset().top;
  24. $('html, body').animate({
  25. scrollTop: scroll },
  26. 500);
  27. });
  28. });
  29. }
  30.  
  31. function arrangeNodes() {
  32. $(".node").remove();
  33. $.each($("section"), function (i, element) {
  34. let name = $(element).data("name");
  35. let node = $("<li class='node'><span>" + name + "</span></li>");
  36. $(".indicator").append(node);
  37. $(node).css({
  38. "top": $(".indicator").height() / $(document).height() * $(element).offset().top });
  39. });
  40. addBehaviours();
  41. }
  42.  
  43. $(window).on("scroll", function () {
  44. let top = window.scrollY / sumSection() * 100;
  45. $(".bar").css({
  46. "top": top + "%" });
  47. });
  48.  
  49. $(window).on("resize", function () {
  50. setSection();
  51. arrangeNodes();
  52. setDimensionBar();
  53. });
  54.  
  55. setTimeout(
  56. function () {
  57. setSection();
  58. arrangeNodes();
  59. setDimensionBar();
  60. },
  61. 200);
  62.  
  63. });

预览截图