多物體改變是單物體運動,各路大神看下錯了那
<script type="text/javascript">
? ?window.onload=function(){
? ? ? ?var ali=Document.getElementsByTagName("li");
? ? ? ?ali.onmousemover=function(){
? ? ? ? ? ?startMove(this,400);
? ? ? ?}
? ? ? ?ali.onmouseout=function(){
? ? ? ? ? ?startMove(this,200);
? ? ? ?}
? ?}
? ?var ?timer ?= null;
? ?function startMove(obj,target){
? ? ? ?clearInterval(obj.timer);
? ? ? ?obj.timer=setInterval(function(){
? ? ? ? ? ?var speed=speed>0?-10:10;
? ? ? ? ? ?if(obj.offsetWidth==target){
? ? ? ? ? ? ? ?clearInterval(obj.timer);
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?obj.style.width=obj.offsetWidth+speed+'px';
? ? ? ? ? ?}
? ? ? ?},30)
? ?}
</script>
2016-08-29
同學(xué) 我也是新人 ?我不知道我說的能不能幫到你 ?但是我還是說下我自己的拙見 ? 首先 第一 ?document首字母不能大寫的 ?其次因為我不知道你的整個靜態(tài)頁面是什么結(jié)構(gòu) 如果是老師講的那個的話 ?首先 你獲取的ali是一個數(shù)組 要進行循環(huán)語句才能多物體運動?
for(var i=0;i<ali.length;i++){
????ali[i].onmousemover=function(){
? ? ? ? ? ?startMove(this,400);
? ? ? ?}
? ? ? ?ali[i].onmouseout=function(){
? ? ? ? ? ?startMove(this,200);
? ? ? ?}
} ?最后就是你的速度判斷我不太懂 ?沒有初始速度 也沒有賦值判定 ?不太理解怎么去做這個判斷,這里我按我個人想法提供兩種判斷第一種勻速
var speed=0;
if(target==400){
speed=10;
}
else if(target==200){
speed=-10;
}
第二種就是老師說的
var speed = (iTarget-obj.offsetWidth)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
最后代碼統(tǒng)計如下
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);
????????}
????}
}
function startMove(obj,target){
????clearInterval(obj.timer);
????obj.timer=setInterval(function(){
????????var speed=0;
????????if(target==400){
????????????speed=10;
????????}
????????else if(target==200){
????????????speed=-10;
????????}
????????if(obj.offsetWidth==target){
????????????clearInterval(timer);
????????}
????????else{
????????????obj.style.width=obj.offsetWidth+speed+"px";
????????}
?????},30)
}
希望能幫到你 ?