鼠标追踪特效 鼠标跟随代码

在线工具箱 6个月前 (11-23) 阅读数 92 #网络技术

想要给网页增加一些特效吗?那么为你的鼠标添加一个追踪特效吧!

HTML:

<!-- 在<head>标签中引入CSS文件 -->
<link rel="stylesheet" href="style.css">

<!-- 在<body>标签中引入JS文件 -->
<script src="script.js"></script>

CSS:

.cursor-follow {
  position: fixed;
  top: 0;
  left: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: red;
  pointer-events: none;
}

JavaScript:

document.addEventListener('mousemove', function(event) {
  var cursor = document.createElement('div');
  cursor.className = 'cursor-follow';
  cursor.style.left = event.clientX - 15 + 'px';
  cursor.style.top = event.clientY - 15 + 'px';
  document.body.appendChild(cursor);
});

通过以上的代码,你可以在你的网页中使用 CSS 和 JavaScript 实现一个鼠标追踪的特效。当你的鼠标移动时,网页中会跟随着一个小圆点,让你的网页更加生动有趣。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门