建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress 主题添加鼠标跟随特效代码

GG网络技术分享 2025-03-18 16:05 0


将鼠标跟随特效分享给喜欢花草的朋友。这种鼠标跟随效果还是很酷的。它不是那种追随很多零零碎碎的鼠标效果。只有一个圆圈跟随鼠标指针。遇到超链接,圆圈会变成半透明背景,是国外网站常见的类型,国外网站常见的一种的特效,具体效果看本站。

添加方法非常简单,将下面代码添加到当前函数模板 functions.php 最后:

  1. // 加载jquery开始,如果主题已加载不加这段。
  2. function zm_jquery_script() {
  3. wp_enqueue_script( 'jquery' );
  4. }
  5. add_action( 'wp_enqueue_scripts', 'zm_jquery_script' );
  6. // 加载jquery结束
  7. function zm_mouse_cursor() { ?>
  8. jQuery(document).ready(function($){
  9. var myCursor = jQuery('.mouse-cursor');
  10. if (myCursor.length) {
  11. if ($('body')) {
  12. const e = document.querySelector('.cursor-inner'),
  13. t = document.querySelector('.cursor-outer');
  14. let n,
  15. i = 0,
  16. o = !1;
  17. window.onmousemove = function(s) {
  18. o || (t.style.transform = "translate(" + s.clientX + "px, " + s.clientY + "px)"),
  19. e.style.transform = "translate(" + s.clientX + "px, " + s.clientY + "px)",
  20. n = s.clientY,
  21. i = s.clientX
  22. },
  23. $('body').on("mouseenter", "a, .cursor-pointer",
  24. function() {
  25. e.classList.add('cursor-hover'),
  26. t.classList.add('cursor-hover')
  27. }),
  28. $('body').on("mouseleave", "a, .cursor-pointer",
  29. function() {
  30. $(this).is("a") && $(this).closest(".cursor-pointer").length || (e.classList.remove('cursor-hover'), t.classList.remove('cursor-hover'))
  31. }),
  32. e.style.visibility = "visible",
  33. t.style.visibility = "visible"
  34. }
  35. }
  36. })
  37.  
  38. .mouse-cursor {
  39. position: fixed;
  40. left: 0;
  41. top: 0;
  42. pointer-events: none;
  43. border-radius: 50%;
  44. -webkit-transform: translateZ(0);
  45. transform: translateZ(0);
  46. visibility: hidden
  47. }
  48.  
  49. .cursor-inner {
  50. margin-left: -3px;
  51. margin-top: -3px;
  52. width: 6px;
  53. height: 6px;
  54. z-index: 10000001;
  55. background: #ffa9a4;
  56. -webkit-transition: width .3s ease-in-out,height .3s ease-in-out,margin .3s ease-in-out,opacity .3s ease-in-out;
  57. transition: width .3s ease-in-out,height .3s ease-in-out,margin .3s ease-in-out,opacity .3s ease-in-out
  58. }
  59.  
  60. .cursor-inner.cursor-hover {
  61. margin-left: -18px;
  62. margin-top: -18px;
  63. width: 36px;
  64. height: 36px;
  65. background: #ffa9a4;
  66. opacity: .3
  67. }
  68.  
  69. .cursor-outer {
  70. margin-left: -15px;
  71. margin-top: -15px;
  72. width: 30px;
  73. height: 30px;
  74. border: 2px solid #ffa9a4;
  75. -webkit-box-sizing: border-box;
  76. box-sizing: border-box;
  77. z-index: 10000000;
  78. opacity: .5;
  79. -webkit-transition: all .08s ease-out;
  80. transition: all .08s ease-out
  81. }
  82.  
  83. .cursor-outer.cursor-hover {
  84. opacity: 0
  85. }
  86.  
  87. .main-wrapper[data-magic-cursor=hide] .mouse-cursor {
  88. display: none;
  89. opacity: 0;
  90. visibility: hidden;
  91. position: absolute;
  92. z-index: -1111
  93. }
  94. }
  95. add_action( 'wp_footer', 'zm_mouse_cursor' );

因默认主题未加载 jquery,所以代码中添加了 WP 自带的 jquery,如果你的主题已加载了 jquery,则不加第一段的代码(有注释),大部分主题应该都加载了 jquery。

当然你也可以将样式添加到当前主题 style.css 中,包括 JS 代码也可以加到一个单独文件中加载。

标签: WordPress 教程

提交需求或反馈

Demand feedback