使用JavaScript,当鼠标滑动到文字上(p元素)时,改变该元素的align属性,使文字左右跳动,实现动态效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f,#p {
position: absolute;
top: 10px;
padding: 0;
margin: 0;
}
#p {
width: 90%;
}
#f {
color: transparent;
user-select: none;
cursor: default;
}
</style>
</head>
<body>
<p id="p" align="left">跳动的文字</p>
<font id="f" color="transparent">跳动的文字</font>
<script>
document.getElementById("f").onmouseenter = () => {
document.getElementById("p").align = "right";
}
document.getElementById("f").onmouseleave = () => {
document.getElementById("p").align = "left";
}
</script>
</body>
</html>
οnmοuseοver事件,滑入后,js改变text-align样式,οnmοuseοut事件,滑出后也一样
var p = document.getElementById("p")
p.onmouseover=function(){
p.style.textAlign="right"
}
p.onmouseout=function(){
p.style.textAlign="left"
}
有帮助的话采纳一下哦!