為什么我用margin-left來(lái)做速度動(dòng)畫(huà),出不來(lái)效果
? ?var timer=null;
window.onload=function ?() {
var obigbox=document.getElementById('bigbox');
var osmallbox=document.getElementById('smallbox');
osmallbox.onmouseover=function(){
startMove();
}
}
function startMove () {
var obigbox=document.getElementById('bigbox');
? ? ? ? clearInterval(timer);
? ?timer=setInterval(function () {
? ? console.log("hello");
? ? if (obigbox.offsetLeft==0) {
? ? ?clearInterval(timer);
? ? ? ? ?}
? ? ? ? else{
? ? obigbox.style.cssText='margin-left:'+obigbox.offsetLeft+10+'px;'
? ?
? ? }
? ? console.log(obigbox.offsetLeft+' ');
? ?},30)
? ??
? ??
}
2016-03-23
?1.obigbox.style.cssText='margin-left:'+obigbox.offsetLeft+10+'px;'使用這個(gè)方法控制css會(huì)將此元素原來(lái)的所有css樣式全部覆蓋掉(如果是將css寫(xiě)在行間的話可能運(yùn)動(dòng)了你看不出來(lái))。
2.運(yùn)動(dòng)的時(shí)候太快你看不出來(lái)(因?yàn)?margin-left:'+obigbox.offsetLeft+10+'px;'此處的obigbox.offsetLeft+10會(huì)拼接成一個(gè)字符串然后就不會(huì)達(dá)到你想要的效果了如8+10='810'而不是18).
3.我猜的