2014-02-15 122 views
0

所以我希望能夠點擊中間的方塊,它應該將左上方的方塊向右滑動,而右上方的方塊將垂直滑動到底部等。將浮動元素動畫到浮動點的相反位置

幻燈片持續時間爲2秒。

http://jsfiddle.net/meD8y/

$(document).ready(function(){ 
    $("#container").click(function(){ 
     $("#box1, #box3").css("float", "right"); 
     $("#box2, #box4").css("float", "left"); 
    }); 
}); 

不知道如何把.animate

+0

嘗試(http://api.jquery.com/animate/ && || http://www.w3schools.com/css/css3_transitions.asp) –

+0

你不能在浮動狀態下動畫變化,你可能必須絕對定位它們。 [這是當你不這樣做時會發生的事情](http://jsfiddle.net/meD8y/1/) –

回答

0

float屬性無法進行動畫處理。但是,您可以動畫定位。

這裏有一個JS小提琴完成你的目標:http://jsfiddle.net/ygP3r/

HTML

<div id="box-1" class="move"></div> 
<div id="box-2"></div> 
<div id="box-3" class="move"></div> 
<div id="box-4"></div> 

CSS

div { 
    display: block; 
    width: 100px; 
    height: 100px; 
    position: relative; 
    left: 0; 
    cursor: pointer; 
    transition: left 2s ease; 
    -webkit-transition: left 2s ease; 
    -moz-transition: left 2s ease; 
    -ms-transition: left 2s ease; 
    -o-transition: left 2s ease; 
} 

.move { 
    left: calc(100% - 100px); 
    left: -webkit-calc(100% - 100px); 
} 

#box-1 { 
    background-color:red; 
} 

#box-2 { 
    background-color:blue; 
} 

#box-3 { 
    background-color:green; 
} 

#box-4 { 
    background-color:orange; 
} 

JS

$(document).ready(function() {  
    $('div').click(function() { 
     $(this).toggleClass('move'); 
    }); 
}); 
0

看這個

fiddle

var click_counter = 0; 
$("#rotate").click(function() { 
    if(click_counter < 4) { 
     click_counter++; 
    } else { 
     click_counter = 1 
    } 

    switch(click_counter) { 
     case 1: 
      $("#box1").html("3"); 
      $("#box1").css("background-color","#999999"); 
      $("#box2").html("1"); 
      $("#box2").css("background-color","#ffffff"); 
      $("#box3").html("4"); 
      $("#box3").css("background-color","#666666"); 
      $("#box4").html("2"); 
      $("#box4").css("background-color","#cccccc"); 
      break; 
     case 2: 
      $("#box1").html("2"); 
      $("#box1").css("background-color","#cccccc"); 
      $("#box2").html("4"); 
      $("#box2").css("background-color","#666666"); 
      $("#box3").html("1"); 
      $("#box3").css("background-color","#ffffff"); 
      $("#box4").html("3"); 
      $("#box4").css("background-color","#999999"); 
      break; 
     case 3: 
      $("#box1").html("4"); 
      $("#box1").css("background-color","#666666"); 
      $("#box2").html("3"); 
      $("#box2").css("background-color","#999999"); 
      $("#box3").html("2"); 
      $("#box3").css("background-color","#cccccc");  
      $("#box4").html("1"); 
      $("#box4").css("background-color","#ffffff"); 
      break; 
     case 4: 
      $("#box1").html("1"); 
      $("#box1").css("background-color","#ffffff"); 
      $("#box2").html("2"); 
      $("#box2").css("background-color","#cccccc"); 
      $("#box3").html("3"); 
      $("#box3").css("background-color","#999999"); 
      $("#box4").html("4"); 
      $("#box4").css("background-color","#666666"); 
      break; 
    } 
});