哪里錯啦?老是報ReferenceError: attr is not defined這個,我上次錯了別的語法也這么報,估計是其他原因?qū)懥薬ttr沒問題
<script src="test.js"></script>
?? ??? ?<script type="text/javascript">
?? ??? ??? ?window.onload=function(){
?? ??? ??? ??? ?var li=document.getElementById("li1");
?? ??? ??? ??? ?li.onmousemove=function(){
?? ??? ??? ??? ??? ?move(li,{width:300,height:300});
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?//var json={a:1,b:2};
?? ??? ?</script>
function getstyle(obj,attr){
?? ??? ??? ??? ?if(obj.currentStyle){
?? ??? ??? ??? ??? ?return obj.currentStyle[attr];
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?return getComputedStyle(obj,false)[attr];
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?var alpha=30;
?? ??? ?function move(obj,json,fn){
?? ??? ??? ?clearInterval(obj.timer);
?? ??? ??? ?obj.timer=setInterval(function(){
?? ??? ??? ??? ?for(var i in json){
?? ??? ??? ??? ??? ?var curr=0;
?? ??? ??? ??? ??? ?if(attr=="opacity"){
?? ??? ??? ??? ??? ??? ?curr = Math.round(parseFloat(getstyle(obj,attr))*100);
?? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ???? curr=parseInt(getstyle(obj,attr));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?//算速度
?? ??? ??? ??? ??? ?var speed=(json[attr]-curr)/8;
?? ??? ??? ??? ??? ?speed=speed>0?Math.ceil(speed):Math.floor(speed);
?? ??? ??? ??? ??? ?//檢測停止
?? ??? ??? ??? ??? ?if(curr==json[attr]){
?? ??? ??? ??? ??? ??? ?clearInterval(obj.timer);
?? ??? ??? ??? ??? ??? ?if(fn){
?? ??? ??? ??? ??? ??? ??? ?fn();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ?if(attr=="opacity"){
?? ??? ??? ??? ??? ??? ??? ?obj.style.filter="alpha(opacity:"+(curr+speed)+")";
?? ??? ??? ??? ??? ??? ??? ?obj.style.opacity=(curr+speed)/100;
?? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ?obj.style[attr]=curr+speed+"px";
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?},30)
?? ??? ?}
2017-11-02
沒有attr呀~ 你代碼里的 應該把attr改為i,for循環(huán)里 json的屬性名。