<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script src="./js/utils.js"></script>
<script>
// 给浏览器绑定移动事件
document.onmousemove = function(){
// 获取span
var active = document.querySelector('span.active');
if(active){
active.parentElement.removeChild(active)
}
// 获取鼠标位置
var x = window.event.pageX
var y = window.event.pageY
// 创建标签
var span = document.createElement('span')
// 给span设置类名
span.className = 'active'
// 设置样式
setStyle(span, {
position: 'absolute',
left: x + 15 + 'px',
top: y + 15 + 'px'
})
// 设置内容
span.innerText = '我是跟随鼠标的文字'
// 将span放在body中
document.body.appendChild(span)
}
</script>
</html>
文字跟随光标
最新推荐文章于 2024-08-11 18:40:36 发布