man豪
2016-03-16 23:02
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0px;
padding: 0px;
}
#div1{
width: 200px;
height: 180px;
background: red;
filter: alpha(opacity:30);
opacity: 0.3;
}
</style>
<script>
window.noload=function(){
var odiv=Document.getElementById('div1');
odiv.onmouseover=function(){
starmove(100);
}
odiv.onmouseout=function(){
starmove(30);
}
}
var timer = null;
var alpha=30;
function starmove(iTarget){
var odiv = Document.getElementById('div1');
clearInterval(timer);
timer = setInterval(function(){
var speed=0;
if (alpha>iTarget) {
speed=-10;
}else{
speed=10;
}
if (alpha==iTarget){
clearInterval(timer);
}else{
alpha+=speed;
// odiv.style.filter='alpha(opacity:'+alpha+')';
odiv.style.filter = "alpha(opacity:‘+alpha+')";
odiv.style.opacity = alpha/100;
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
整体没问题,但里面的小错误太多了,这是给你修改后的代码,自己去对照找一下错误吧。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0px;
padding: 0px;
}
#div1{
width: 200px;
height: 180px;
background: red;
filter: alpha(opacity:30);
opacity: 0.3;
}
</style>
<script>
window.onload=function(){
var odiv=document.getElementById('div1');
odiv.onmouseover=function(){
starmove(100);
}
odiv.onmouseout=function(){
starmove(30);
}
}
var timer = null;
var alpha=30;
function starmove(iTarget){
var odiv = document.getElementById('div1');
clearInterval(timer);
timer = setInterval(function(){
var speed=0;
if (alpha>iTarget) {
speed=-10;
}else{
speed=10;
}
if (alpha==iTarget){
clearInterval(timer);
}else{
alpha+=speed;
console.log(alpha);
// odiv.style.filter='alpha(opacity:'+alpha+')';
odiv.style.filter = "alpha(opacity:'+alpha+')";
odiv.style.opacity = alpha/100;
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
1 window.noload=function(){ //把noload改成onload
2 var odiv = Document.getElementById('div1'); //把Document改成document
3 odiv.style.filter = "alpha(opacity:‘+alpha+')"; //把‘ 改成 ” 或把“ 改成 ’
有三个小错误,都是拼写问题,window.onload你写成了 window.noload; 还有两处 Document.getElementById的‘D’不应该大写,应该是document.getElementById。。这三处拼写改了,应该就对了
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
div {
background: blue;
width: 200px;
height: 200px;
position: relative;
left:0px;
top:100px;
filter:alpha(opacity:30);
opacity:0.3;
}
</style>
</head>
<body>
<div id="div1">
</div>
</body>
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById("div1");
oDiv.onmouseover = function(){
startMove(100);
}
oDiv.onmouseout = function(){
startMove(30);
}
}
var timer = null;//可有可无
var alpha = 30;
var speed = 0;
function startMove(iTarget){
clearInterval(timer);
if(alpha > iTarget) {
speed = -10;
}else{
speed = 10;
}
var oDiv = document.getElementById("div1");
timer = setInterval(function(){
if(alpha == iTarget) {
clearInterval(timer);
}
else {
alpha += speed;
oDiv.style.filter = "alpha(opacity:"+alpha+")";
oDiv.style.opacity = alpha/100;
}
},30)
}
</script>
</html>
JS动画效果
113925 学习 · 1443 问题
相似问题