滚动进度指示器 自定义光标

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

简介

一个交互式自定义光标,自动填充圆圈以指示当前滚动位置。

如何使用它:

1.为自定义光标和滚动位置指示器编码HTML。

  1. <div class="cursor" id="cursor"></div>
  2. <div class="cursor2" id="cursor2">
  3. <div class="progress-wrap">
  4. <svg class="progress-circle svg-content" width="100%" height="100%" viewBox="-1 -1 102 102">
  5. <path d="M50,1 a49,49 0 0,1 0,98 a49,49 0 0,1 0,-98"/>
  6. </svg>
  7. </div>
  8. </div>
  9. <div class="cursor3" id="cursor3"></div>

2.自定义光标所需的CSS样式。

  1. .cursor,
  2. .cursor2,
  3. .cursor3{
  4. position: fixed;
  5. border-radius: 50%;
  6. transform: translateX(-50%) translateY(-50%);
  7. pointer-events: none;
  8. left: -100px;
  9. top: 50%;
  10. -webkit-transition: all 300ms linear;
  11. transition: all 300ms linear;
  12. }
  13. .cursor{
  14. background-color: #fff;
  15. z-index: 99999;
  16. height: 0;
  17. width: 0;
  18. }
  19. .cursor2,.cursor3{
  20. height: 46px;
  21. width: 46px;
  22. z-index:99998;
  23. -webkit-transition:all 0.3s ease-out;
  24. transition:all 0.3s ease-out
  25. }
  26.  
  27. .cursor2.hover,
  28. .cursor3.hover{
  29. -webkit-transform:scale(1.4) translateX(-35%) translateY(-35%);
  30. transform:scale(1.4) translateX(-35%) translateY(-35%);
  31. border:none
  32. }
  33.  
  34. .cursor2.hover{
  35. background: rgba(255,255,255,0.1);
  36. }
  37.  
  38. .cursor2.hover .progress-wrap {
  39. box-shadow: inset 0 0 0 2px rgba(255,255,255,0);
  40. }
  41.  
  42. .cursor2.hover .progress-wrap svg.progress-circle path {
  43. opacity: 0.4;
  44. }

3.设置滚动位置指示器的样式。

  1. .progress-wrap {
  2. height: 46px;
  3. width: 46px;
  4. cursor: pointer;
  5. display: block;
  6. border-radius: 50px;
  7. box-shadow: inset 0 0 0 2px rgba(255,255,255,0.2);
  8. z-index: 10000;
  9. -webkit-transition: all 200ms linear;
  10. transition: all 200ms linear;
  11. }
  12.  
  13. .progress-wrap svg path {
  14. fill: none;
  15. }
  16.  
  17. .progress-wrap svg.progress-circle path {
  18. stroke: var(--grey);
  19. stroke-width: 4;
  20. box-sizing: border-box;
  21. -webkit-transition: all 200ms linear;
  22. transition: all 200ms linear;
  23. }

4.在文档末尾加载所需的jQuery库。

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

5.用于激活自定义光标和滚动位置指示器的主JavaScript。

  1. (function($) { "use strict";
  2.  
  3. // Page cursors
  4.  
  5. document.getElementsByTagName("body")[0].addEventListener("mousemove", function(n) {
  6. t.style.left = n.clientX + "px",
  7. t.style.top = n.clientY + "px",
  8. e.style.left = n.clientX + "px",
  9. e.style.top = n.clientY + "px",
  10. i.style.left = n.clientX + "px",
  11. i.style.top = n.clientY + "px"
  12. });
  13. var t = document.getElementById("cursor"),
  14. e = document.getElementById("cursor2"),
  15. i = document.getElementById("cursor3");
  16. function n(t) {
  17. e.classList.add("hover"), i.classList.add("hover")
  18. }
  19. function s(t) {
  20. e.classList.remove("hover"), i.classList.remove("hover")
  21. }
  22. s();
  23. for (var r = document.querySelectorAll(".hover-target"), a = r.length - 1; a >= 0; a--) {
  24. o(r[a])
  25. }
  26. function o(t) {
  27. t.addEventListener("mouseover", n), t.addEventListener("mouseout", s)
  28. }
  29. $(document).ready(function(){"use strict";
  30. //Scroll indicator
  31. var progressPath = document.querySelector('.progress-wrap path');
  32. var pathLength = progressPath.getTotalLength();
  33. progressPath.style.transition = progressPath.style.WebkitTransition = 'none';
  34. progressPath.style.strokeDasharray = pathLength + ' ' + pathLength;
  35. progressPath.style.strokeDashoffset = pathLength;
  36. progressPath.getBoundingClientRect();
  37. progressPath.style.transition = progressPath.style.WebkitTransition = 'stroke-dashoffset 10ms linear';
  38. var updateProgress = function () {
  39. var scroll = $(window).scrollTop();
  40. var height = $(document).height() - $(window).height();
  41. var progress = pathLength - (scroll * pathLength / height);
  42. progressPath.style.strokeDashoffset = progress;
  43. }
  44. updateProgress();
  45. $(window).scroll(updateProgress);
  46. });
  47. })(jQuery);

预览截图