<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#p1{
width: 200px;
height: 200px;
background-color: blue;
opacity: 0.3;
}
</style>
<script>
window.onload=function(){
var myp=document.getElementById('p1');
myp.onmouseover=function(){
startmove(0.9);
}
myp.onmouseout=function(){
startmove(0.3);
}
}
var timer=null;
function startmove(itarget){
var speed=0;
var tmd=0;
var myp=document.getElementById('p1');
clearInterval(timer);
timer=setInterval(function(){
if(tmd<itarget)
{
speed=0.01;
}
else
{
speed=-0.01;
}
if(tmd==itarget)
{
clearInterval(timer);
}
else
{
tmd+=speed;
myp.style.opacity=tmd;
}
}, 30);
}
</script>
</head>
<body>
<p id="p1">
</p>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为你已经给tmd赋值为0了,每次执行函数,tmd都是从0开始算起的。
试试我这个,只有移入部分,移出同理
链接描述