jQuery 跟随鼠标是指使用 jQuery 库实现一个元素随着鼠标移动而移动的效果。这种效果通常用于创建动态的用户界面元素,如浮动菜单、提示框等。
以下是一个简单的 jQuery 跟随鼠标的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Follow Mouse</title>
<style>
#follower {
position: absolute;
width: 50px;
height: 50px;
background-color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="follower"></div>
<script>
$(document).ready(function() {
$(document).mousemove(function(event) {
$('#follower').css({
left: event.pageX,
top: event.pageY
});
});
});
</script>
</body>
</html>
cursor
属性为 none
来减少重绘。cursor
属性为 none
来减少重绘。通过以上方法,可以有效地实现和控制 jQuery 跟随鼠标的效果,并解决可能遇到的问题。
没有搜到相关的文章