我按照老師的思路寫的,內(nèi)容也沒區(qū)別,但是在最后obj.style.left的時候一直報錯顯示obj.style未定義是為什么,我代碼在下面,就大神解惑
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{padding:0;margin:0;}
ul,li{
list-style:none;
}
ul li{
width:200px;
height: 100px;
background: yellow;
margin-bottom:20px;
}
</style>
<script>
window.onload=function(){
var oLi=document.getElementsByTagName('li');
for(var i=0,l=oLi.length;i<l;i++){
this.onmouseover=function(){
move(this,400);
}
this.onmouseout=function(){
move(this,200);
}
}}
var timer=null;
function move(obj,iTarget){
clearInterval(timer);
timer=setInterval(function(){
var speed=(iTarget-obj.offsetWidth)/2>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==iTarget){
clearInterval(timer);
}else{
obj.style.width=obj.offsetWidth+speed+'px';//這里的obj.style一直顯示未定義
}
},50);
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
2016-10-09
參照你的代碼看看,也許對你有幫助
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{padding:0;margin:0;}
ul,li{
list-style:none;
}
ul li{
width:200px;
height: 100px;
background: yellow;
margin-bottom:20px;
}
</style>
<script>
window.onload=function(){
var oLi=document.getElementsByTagName('li');
for(var i=0;i<oLi.length;i++){
oLi[i].onmouseover=function(){/*有改動*/
move(this,400);
}
oLi[i].onmouseout=function(){{/*有改動*/
move(this,200);
}
}}
var timer=null;
function move(obj,iTarget){
clearInterval(timer);
timer=setInterval(function(){
?? ?var speed=(iTarget-obj.offsetWidth)/8;{<!--有改動,原來的那樣寫運算次序有問題-->
var speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==iTarget){
clearInterval(timer);
}else{
obj.style.width=obj.offsetWidth+speed+'px';//這里的obj沒問題
}
},50);
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
2016-10-31
<!DOCTYPE HTML>
<html>
? ? <head>
? ? ? ? <meta charset="utf-8">
? ? ? ? <title></title>
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? margin: 0px;
? ? ? ? ? ? ? ? padding: 0px;
? ? ? ? ? ? }
? ? ? ? ? ? ul,li{
? ? ? ? ? ? ? ? list-style: none;
? ? ? ? ? ? }
? ? ? ? ? ? ul li{
? ? ? ? ? ? ? ? width: 200px;
? ? ? ? ? ? ? ? height: 100px;
? ? ? ? ? ? ? ? background:yellow;
? ? ? ? ? ? ? ? margin-bottom:20px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? ? ? <script type="text/javascript">
? ? ? ? ? ? window.onload = function(){
? ? ? ? ? ? ? ? var aLi = document.getElementsByTagName('li');
? ? ? ? ? ? ? ? for(var i = 0. i < aLi.length; i++){
? ? ? ? ? ? ? ? ? ? aLi[i].onmouseover = function(){
? ? ? ? ? ? ? ? ? ? ? ? startMove(this, 400);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? aLi[i].onmouseout = function(){
? ? ? ? ? ? ? ? ? ? ? ? startMove(this, 200);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? var timer = null;
? ? ? ? ? ? function startMove(obj, Target){
? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? ? ? timer = setInterval(function(){
? ? ? ? ? ? ? ? ? ? var speed = (Target - obj.offsetWidth)/8;
? ? ? ? ? ? ? ? ? ? speed = (speed > 0) ? Math.ceil(speed): Math.floor(speed);
? ? ? ? ? ? ? ? ? ? if(obj.offsetWidth == Target){
? ? ? ? ? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? ? ? obj.style.width = obj.offsetWidth + speed + 'px';
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }, 30)
? ? ? ? ? ? }
? ? </script>
? ? </head>
? ? <body>
? ? ? ? <ul>
? ? ? ? ? ? <li></li>
? ? ? ? ? ? <li></li>
? ? ? ? ? ? <li></li>
? ? ? ? </ul>
? ? </body>
</html>
2016-10-09
首先。
for(var i=0,l=oLi.length;i<l;i++){
this.onmouseover=function(){
這里就不該直接用this,而應該使用
oLi[i].onmouseover=function(){}
因為你遍歷循環(huán)的時候沒指名是哪個object在里面,所以this是屬于window object。而不是具體的某個html元素。