<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>opacity</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#op {
width: 200px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<p id="op"></p>
<script type="text/javascript">
window.onload = function(){
var op = document.getElementById('op');
op.onmouseover = function(){
startmove(0.5);
}
op.onmouseout = function(){
startmove(1);
}
var timer = null;
function startmove(target){
clearInterval(timer);
timer = setInterval(function(){
var speed = 0;
if(op.style.opacity>target){
speed = -0.1;
}else{
speed = 0.1;
};
if (op.style.opacity==target) {
clearInterval(timer);
} else {
op.style.opacity+=speed;
}
},50);
}
}
</script>
</body>
</html>
需求:鼠标放置和离开时透明度渐变。
1、求解,上述代码中有哪些错误需要更正?
2、“op.style.opacity==target”能否这样比较?
应该如何修改才能实现效果?
感谢~
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
小伙看你根骨奇佳,潜力无限,来学PHP伐。