jQuery好看的带进度指示器《返回顶部》

jQuery返回顶部,带有进度指示器,如图所示,html+css+jq,很简易,感觉特别实用,下面分享给大家吧

QQ截图20220305224739.png

html:

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

css:

  1. /* #Progress
  2. ================================================== */
  3. .progress-wrap {
  4. position: fixed;
  5. right: 50px;
  6. bottom: 50px;
  7. height: 46px;
  8. width: 46px;
  9. cursor: pointer;
  10. display: block;
  11. border-radius: 50px;
  12. box-shadow: inset 0 0 0 2px rgba(255,255,255,0.2);
  13. z-index: 10000;
  14. opacity: 0;
  15. visibility: hidden;
  16. transform: translateY(15px);
  17. -webkit-transition: all 200ms linear;
  18. transition: all 200ms linear;
  19. }
  20. .progress-wrap.active-progress {
  21. opacity: 1;
  22. visibility: visible;
  23. transform: translateY(0);
  24. }
  25. .progress-wrap::after {
  26. position: absolute;
  27. font-family: 'unicons';
  28. content: '↑';
  29. text-align: center;
  30. line-height: 46px;
  31. font-size: 24px;
  32. color: var(--grey);
  33. left: 0;
  34. top: 0;
  35. height: 46px;
  36. width: 46px;
  37. cursor: pointer;
  38. display: block;
  39. z-index: 1;
  40. -webkit-transition: all 200ms linear;
  41. transition: all 200ms linear;
  42. }
  43. .progress-wrap:hover::after {
  44. opacity: 0;
  45. }
  46. .progress-wrap::before {
  47. position: absolute;
  48. font-family: 'unicons';
  49. content: '↑';
  50. text-align: center;
  51. line-height: 46px;
  52. font-size: 24px;
  53. opacity: 0;
  54. background-image: linear-gradient(298deg, var(--red), var(--yellow));
  55. -webkit-background-clip: text;
  56. -webkit-text-fill-color: transparent;
  57. left: 0;
  58. top: 0;
  59. height: 46px;
  60. width: 46px;
  61. cursor: pointer;
  62. display: block;
  63. z-index: 2;
  64. -webkit-transition: all 200ms linear;
  65. transition: all 200ms linear;
  66. }
  67. .progress-wrap:hover::before {
  68. opacity: 1;
  69. }
  70. .progress-wrap svg path {
  71. fill: none;
  72. }
  73. .progress-wrap svg.progress-circle path {
  74. stroke: var(--grey);
  75. stroke-width: 4;
  76. box-sizing:border-box;
  77. -webkit-transition: all 200ms linear;
  78. transition: all 200ms linear;
  79. }

jq:

  1. /* Please ❤ this if you like it! */
  2. (function($) { "use strict";
  3. //Switch dark/light
  4. $(".switch").on('click', function () {
  5. if ($("body").hasClass("light")) {
  6. $("body").removeClass("light");
  7. $(".switch").removeClass("switched");
  8. }
  9. else {
  10. $("body").addClass("light");
  11. $(".switch").addClass("switched");
  12. }
  13. });
  14. $(document).ready(function(){"use strict";
  15. //Scroll back to top
  16. var progressPath = document.querySelector('.progress-wrap path');
  17. var pathLength = progressPath.getTotalLength();
  18. progressPath.style.transition = progressPath.style.WebkitTransition = 'none';
  19. progressPath.style.strokeDasharray = pathLength + ' ' + pathLength;
  20. progressPath.style.strokeDashoffset = pathLength;
  21. progressPath.getBoundingClientRect();
  22. progressPath.style.transition = progressPath.style.WebkitTransition = 'stroke-dashoffset 10ms linear';
  23. var updateProgress = function () {
  24. var scroll = $(window).scrollTop();
  25. var height = $(document).height() - $(window).height();
  26. var progress = pathLength - (scroll * pathLength / height);
  27. progressPath.style.strokeDashoffset = progress;
  28. }
  29. updateProgress();
  30. $(window).scroll(updateProgress);
  31. var offset = 50;
  32. var duration = 550;
  33. jQuery(window).on('scroll', function() {
  34. if (jQuery(this).scrollTop() > offset) {
  35. jQuery('.progress-wrap').addClass('active-progress');
  36. } else {
  37. jQuery('.progress-wrap').removeClass('active-progress');
  38. }
  39. });
  40. jQuery('.progress-wrap').on('click', function(event) {
  41. event.preventDefault();
  42. jQuery('html, body').animate({scrollTop: 0}, duration);
  43. return false;
  44. })
  45. });
  46. })(jQuery);

样式里面的var(--grey),记得替换为自己选择的颜色,关于返回顶部的大小,都可以在css里面调整,快试试吧

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1192
1 条评论
2k

发表评论

!