2012-11-13 189 views
2

我一直在試驗jQuery動畫與CSS3動畫,我還想測試2D/3D翻譯以查看哪個更好。CSS3動畫translate3d無法正常工作

有誰知道爲什麼我的CSS3 translate3d不起作用?我已經在桌面和移動設備上嘗試過了。

任何幫助表示讚賞

jsFiddle

HTML

<div id="container1" class="container">transition</div> 
    <div id="container2" class="container">translate</div> 
    <div id="container3" class="container">translate3d</div> 
    <div id="container4" class="container">jQ animate</div> 

CSS

.container   {position:absolute; left:20px; width:80px; height:80px; padding:5px;} 

/* transition */ 
#container1   {top:20px; background:red; 
        -webkit-transition:all 0.3s linear; 
        -moz-transition:all 0.3s linear; 
        -o-transition:all 0.3s linear; 
        -ms-transition:all 0.3s linear; 
        transition:all 0.3s linear;} 

#container1.on  {left:250px} /* It moves if from pos absolute of parent, the body tag in this example */ 



/* 2D translate */ 
#container2   {top:120px; background:yellow; 
        -webkit-transition:all 0.3s linear; 
        -moz-transition:all 0.3s linear; 
        -o-transition:all 0.3s linear; 
        -ms-transition:all 0.3s linear; 
        transition:all 0.3s linear;} 

#container2.on  {-webkit-transform: translate(230px); 
        -moz-transform: translate(230px); 
        -o-transform: translate(230px); 
        -ms-transform: translate(230px); 
        transform: translate(230px);} /* It moves if from the starting point, 20px left in this example */ 



/* 3D - translate */ 
#container3   {top:220px; background:lime; 
        -webkit-transition:all 0.3s linear; 
        -moz-transition:all 0.3s linear; 
        -o-transition:all 0.3s linear; 
        -ms-transition:all 0.3s linear; 
        transition:all 0.3s linear;} 

#container3.on  {-webkit-transform: translate3d(230,0,0); 
        -moz-transform: translate3d(230,0,0); 
        -o-transform: translate3d(230,0,0); 
        -ms-transform: translate3d(230,0,0); 
        transform: translate3d(230,0,0);} /* It moves if from the starting point, 20px left in this example */ 



/* jQuery Animate */ 
#container4   {top:320px; background:orange;} 

jQuery的

$('#container1').click(function() 
    { 
     $(this).toggleClass('on'); 
    }) 

    $('#container2').click(function() 
    { 
     $(this).toggleClass('on'); 
    }) 

    $('#container3').click(function() 
    { 
     $(this).toggleClass('on'); 
    }) 

    $('#container4').toggle(function() 
    { 
     $(this).animate({'left':'250px'}); 

    }, function() 
    { 
     $(this).animate({'left':'20px'}); 
    }) 
+0

哪些瀏覽器您使用? –

+0

桌面 - 鉻,Safari瀏覽器,IE 7/8/9,FF – sygad1

+0

Android手機 - Chrome,原生,Opera Mobile,火狐 iPhone 3Gs - 原生safari – sygad1

回答

5

你缺少長度單位px

使用此的CSS:

#container3.on { 
    -webkit-transform: translate3d(230px, 0, 0); 
    -moz-transform: translate3d(230px, 0, 0); 
    -o-transform: translate3d(230px, 0, 0); 
    -ms-transform: translate3d(230px, 0, 0); 
    transform: translate3d(230px, 0, 0); 
} /* It moves if from the starting point, 20px left in this example */ 

這裏是一個fiddle

+0

神奇,謝謝你,不能相信我錯過了總的明顯。 – sygad1

+0

翻譯3D在IE9中不起作用,翻譯只是從開始跳到結束位置,即沒有動畫......任何想法? – sygad1

+0

ie9沒有支持:http://caniuse.com/#search=3dtransform 但是,我從來沒有使用過這個,但顯然它使用CSS(轉換),如果在瀏覽器中可用,否則使用jQuery動畫作爲回退:http://addyosmani.com/blog/css3transitions-jquery/ –