2015-08-31 28 views

回答

0

你好的人這裏是一個解決方案:https://jsfiddle.net/leojavier/4f5z3c62/ 使用CSS動畫代替,因爲GPU需要你的CSS動畫的照顧,是不太可能有閃爍......是服務表現更好......

function CreateDiv(elm, id, bgcolor, w, h) { 
    var elm = document.createElement(elm); 
    elm.id = id; 
    elm.style.backgroundColor = bgcolor; 
    elm.style.width = w; 
    elm.style.height = h; 
    this.postXY = function(pos, posX, posY) { 
    elm.style.position = pos; 
    elm.style.left = posX; 
    elm.style.top = posY; 
    }; 
    document.getElementsByTagName('body')[0].appendChild(elm); 
}; 

var b1 = new CreateDiv('div', 'box', '#999', '50px', '50px'), 
bx = document.getElementById("box"), 
bt = document.body.offsetHeight/2 +"px", 
bl = document.body.offsetWidth/2 +"px"; 

b1.postXY('absolute', bl, bt); 

this.onclick= function(e) { 
    Move(e); 
}; 

function Move(evt) { 
    console.log(evt.target) 
    evt.target.className = "move" 
}; 

CSS

#box { 
    display:block; 
    font-size:12px; 
    line-height:19px; 
    cursor:pointer; 
} 

.move { 
    -moz-animation: animation 0.4s; 
    -o-animation: animation 0.4s; 
    -webkit-animation: animation 0.4s; 
    animation: animation 0.4s; 
    -moz-animation-timing-function: ease-out; 
    -o-animation-timing-function: ease-out; 
    -webkit-animation-timing-function: ease-out; 
    animation-timing-function: ease-out; 
    -moz-animation-fill-mode: forwards; 
    -o-animation-fill-mode: forwards; 
    -webkit-animation-fill-mode: forwards; 
    animation-fill-mode: forwards; 
} 

@keyframes animation { 
    from { 
     -moz-transform: translateX(0); 
     -ms-transform: translateX(0); 
     -o-transform: translateX(0); 
     -webkit-transform: translateX(0); 
     transform: translateX(0); 
    } 

    to { 
     -moz-transform: translateX(-250px); 
     -ms-transform: translateX(-250px); 
     -o-transform: translateX(-250px); 
     -webkit-transform: translateX(-250px); 
     transform: translateX(-250px); 
    } 
} 

@-moz-keyframes animation { 
    from { 
     -moz-transform: translateX(0); 
     -ms-transform: translateX(0); 
     -o-transform: translateX(0); 
     -webkit-transform: translateX(0); 
     transform: translateX(0); 
    } 

    to { 
     -moz-transform: translateX(-250px); 
     -ms-transform: translateX(-250px); 
     -o-transform: translateX(-250px); 
     -webkit-transform: translateX(-250px); 
     transform: translateX(-250px); 
    } 
} 

@-webkit-keyframes animation { 
    from { 
     -moz-transform: translateX(0); 
     -ms-transform: translateX(0); 
     -o-transform: translateX(0); 
     -webkit-transform: translateX(0); 
     transform: translateX(0); 
    } 

    to { 
     -moz-transform: translateX(-250px); 
     -ms-transform: translateX(-250px); 
     -o-transform: translateX(-250px); 
     -webkit-transform: translateX(-250px); 
     transform: translateX(-250px); 
    } 
} 

https://jsfiddle.net/leojavier/4f5z3c62/

+0

無需CSS3動畫的JavaScript –

0

只需在框中添加一個轉換即可。 Forked demo

transition: all 0.3s; 
+0

我之前使用的,但我的問題是你不能由x和y像素看incremation而動畫到目的地。不需要使用css3動畫 –

+0

你的問題很混亂。這是你想要的嗎? http://jsbin.com/pavuk/edit?html,js,output –

相關問題