能不能幫忙看下哪錯(cuò)了???
window.onload=function(){
?? ?startMove();
?? ?}
function getstyle(obj,attr){
?? ??? ?if(getComputedStyle){
?? ??? ??? ??? ??? ?return getComputedStyle(obj,null)[attr]; //要return 才行,不然就是Undefined;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else{
?? ??? ??? ??? ??? ?return obj.currentStyle[attr];//要return 才行,不然就是Undefined;
?? ??? ??? ??? ?}
?? ?}
function startMove(obj,attr,itarget){
?? ?clearInterval(obj.timer);
?? ?obj.timer = setInterval(function(){
?? ? ?? ?var speed = (itarget - getstyle(obj,attr))/8;
?? ??? ?? speed = speed>0?Math.ceil(speed):Math.floor(speed);
?? ??? ?var anum = 0;
?? ??? ?if(anum == 'opacity'){
?? ??? ??? ?anum = Math.round(parseFloat(getstyle(obj,attr))*100);
?? ??? ??? ?}
?? ??? ?else{
?? ??? ??? ?anum = parseInt(getstyle(obj,attr));
?? ??? ??? ?}
?? ?
?? ??? ?if(anum==itarget){
?? ??? ??? ?clearInterval(obj.timer);
?? ??? ??? ?}
?? ??? ?else{
?? ??? ??? ?
?? ??? ??? ?if(arrt =='opacity'){
?? ??? ??? ??? ?
?? ??? ??? ??? ?anum = anum +speed;
?? ??? ??? ??? ?obj.style.opacity = anum/100;
?? ??? ??? ??? ?obj.style.filter = 'alpha(opacity:'+anum+')';
?? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ?else{
?? ??? ??? ??? ?obj.style[attr] = anum+speed+"px";
?? ??? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?}
?? ??? ?},30)
?? ?}
下面是HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<style type="text/css">
div{
?? ?width:100px;
?? ?height:100px;
?? ?background:pink;
?? ?opacity:0.3;
?? ?filter:alpha(opacity:30);
??? }
</style>
<script src="kuangjia.js"></script>
<script>
window.onload = function(){
? var div1 =document.getElementById("div1");
? div1.onmouseover = startMove(this,'width',200);
? div1.onmouseout = startMove(this,'width',100);
}
?
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
2016-05-17
?var speed = (itarget - getstyle(obj,attr))/8;這一句有錯(cuò)
getstyle(obj,"width")返回的是100px;而不是整數(shù)100,所以itarget (整數(shù))無(wú)法與100px相減
需要講getstyle(obj,attr)改成 parseInt(getstyle(obj,attr))