鼠标移动效果

本文详细介绍了如何使用HTML、CSS和JavaScript实现一个鼠标跟随的粒子效果。通过动态生成多个粒子并利用鼠标移动事件,粒子会根据鼠标的位置进行移动,创建出炫酷的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

</script>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}

.spandian {
background: black;
font-size: 0;
overflow: hidden;
width: 2px;
height: 2px;
position: absolute;
}
</style>
</head>
<body>
<span class='spandian' id="spandian">.</span>
</body>
</html>
<script type="text/javascript">
$(function () {
var count = 10;//设置个数
for (var i = 1; i < count * 8; i++) {
$("#spandian").after("<span class='spandian'>.</span>");
}
})
//移动效果
window.document.onmousemove = function (e) {
e = event || e;
var left = e.clientX;
var top = e.clientY;
move(top, left);
}
//给每一个span定义位置
function move(top, left) {
$(".spandian").each(function () {
var len = 10 * 1;
var aqrt = Math.sqrt(2) * 1;
var index = $(this).index() * 1;
var indexJ = parseInt(index / 8) * 1 + 1;
var indexZ = parseInt(index % 8) * 1 + 1;
var mtop = indexZ == 1 || indexZ == 5 ? 0 : indexZ == 2 ? -indexJ * len / aqrt : indexZ == 3 ? -indexJ * len : indexZ == 4 ? -indexJ * len / aqrt : indexZ == 6 ? indexJ * len / aqrt : indexZ == 8 ? indexJ * len / aqrt : indexJ * len;
var mlef = indexZ == 3 || indexZ == 7 ? 0 : indexZ == 1 ? -indexJ * len : indexZ == 2 ? -indexJ * len / aqrt : indexZ == 4 ? indexJ * len / aqrt : indexZ == 5 ? indexJ * len : indexZ == 8 ? -indexJ * len / aqrt : indexJ * len / aqrt;
var ttop = mtop + top;
var llef = mlef + left;
var span = $(this)
setTimeout(function () { span.css({ top: ttop, left: llef }) }, index * 10)
})
}

</script>

转载于:https://www.cnblogs.com/-maomao/p/5015718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值