多物體改變是單物體運(yùn)動(dòng),各路大神看下錯(cuò)了那
<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é) 我也是新人 ?我不知道我說(shuō)的能不能幫到你 ?但是我還是說(shuō)下我自己的拙見(jiàn) ? 首先 第一 ?document首字母不能大寫(xiě)的 ?其次因?yàn)槲也恢滥愕恼麄€(gè)靜態(tài)頁(yè)面是什么結(jié)構(gòu) 如果是老師講的那個(gè)的話 ?首先 你獲取的ali是一個(gè)數(shù)組 要進(jìn)行循環(huán)語(yǔ)句才能多物體運(yùn)動(dòng)?
for(var i=0;i<ali.length;i++){
????ali[i].onmousemover=function(){
? ? ? ? ? ?startMove(this,400);
? ? ? ?}
? ? ? ?ali[i].onmouseout=function(){
? ? ? ? ? ?startMove(this,200);
? ? ? ?}
} ?最后就是你的速度判斷我不太懂 ?沒(méi)有初始速度 也沒(méi)有賦值判定 ?不太理解怎么去做這個(gè)判斷,這里我按我個(gè)人想法提供兩種判斷第一種勻速
var speed=0;
if(target==400){
speed=10;
}
else if(target==200){
speed=-10;
}
第二種就是老師說(shuō)的
var speed = (iTarget-obj.offsetWidth)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
最后代碼統(tǒng)計(jì)如下
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)
}
希望能幫到你 ?