back_to_top_func.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <button type="button" id="BackTop" class="toTop-arrow" style="z-index: 100;"></button>
  2. <script>
  3. //向上滚动的函数
  4. $(function () {
  5. $('#BackTop').click(function () {
  6. $('html,body').animate({scrollTop: 0}, 500);
  7. });
  8. $(window).scroll(function () {
  9. if ($(this).scrollTop() > 300) {
  10. $('#BackTop').fadeIn(300);
  11. } else {
  12. $('#BackTop').stop().fadeOut(300);
  13. }
  14. }).scroll();
  15. })
  16. </script>
  17. <style>
  18. /* 按钮边框的大小、位置、样式 */
  19. .toTop-arrow {
  20. width: 3.5rem;
  21. height: 3.5rem;
  22. padding: 0;
  23. margin: 0;
  24. border: 0;
  25. border-radius: 33%;
  26. opacity: 0.7;
  27. background: black;
  28. cursor: pointer;
  29. position: fixed;
  30. right: 1.5rem;
  31. bottom: 1.5rem;
  32. display: none;
  33. }
  34. /* 绘制按钮中的向上箭头 */
  35. .toTop-arrow::before, .toTop-arrow::after {
  36. width: 31px;
  37. height: 7px;
  38. border-radius: 3px;
  39. background: orange;
  40. position: absolute;
  41. content: "";
  42. }
  43. .toTop-arrow::before {
  44. transform: rotate(-45deg) translate(0, -50%);
  45. left: 0.4rem;
  46. }
  47. .toTop-arrow::after {
  48. transform: rotate(45deg) translate(0, -50%);
  49. right: 0.4rem;
  50. }
  51. /* 取消点击按钮时的聚焦 */
  52. .toTop-arrow:focus {
  53. outline: none;
  54. }
  55. </style>