2014-01-12 32 views
0

首先,我想從@kgd打開這個解決方案,然後我想關閉它,當我點擊另一個div ...在這個例子中的黃色div在小提琴?激活div函數並向後

var $a = $(".a"), 
 
    $b = $(".b"), 
 

 
function anim1() { 
 
    $b.animate({width: 395}, {duration: 500, complete: anim2}); 
 
} 
 

 
function anim2() { 
 
    $a.animate({top:"0px"}, {duration: 500}); 
 
} 
 

 
$(".b").click(function() { 
 
    anim1(); 
 
}); 
 

 
var $a1 = $(".a"), 
 
    $b1 = $(".b"), 
 
    
 
function anim10() { 
 
    $a1.animate({top:"-50px"}, {duration: 500}); 
 
} 
 

 
function anim20() { 
 
    $b1.animate({width: 137}, {duration: 500, complete: anim10}); 
 
} 
 

 
$(".ab").click(function() { 
 
    anim10(); 
 
});
.example { 
 
    width: 100%; 
 
} 
 

 
.a { 
 
    width: 600px; 
 
    height: 50px; 
 
    background-color: #888; 
 
    
 
    position: relative; 
 
    top: -50px; 
 
} 
 

 
.ab { 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: yellow; 
 
    float: left; 
 
} 
 

 
.b { 
 
    width: 137px; 
 
    height: 50px; 
 
    background-color: red; 
 
    margin-left: 50px; 
 
    float: left; 
 
} 
 

 
.c { 
 
    width: 395px; 
 
    height: 50px; 
 
    background-color: black; 
 
    margin-left: 100px; 
 
    
 
    position: relative; 
 
    top: 0px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<div class="example"> 
 
    <div class="c"></div> 
 
    <div class="a"> 
 
     <div class="ab"></div> 
 
     <div class="b"></div> 
 
    </div> 
 
</div>

爲什麼我的解決方案不工作?

+0

@kgd這是我的問題:) – andre34

+0

1.請包含一個鏈接到「此解決方案從@kgd」。 2.kgd不會收到您評論的通知,因爲@username僅適用於對該主題有所貢獻的用戶(即發佈了答案或評論,或編輯了問題或答案)。在英語中,代詞「I」應該總是大寫。 –

+0

哦okey謝謝你的信息! – andre34

回答

1

代碼更新here。你錯過了一些逗號,你聲明的變量阻止了代碼的運行。

var $a = $(".a"), 
    $b = $(".b"); 

var $a1 = $(".a"), 
    $b1 = $(".b"); 

UPDATE

我做了一些小的變化,但它確實是很難確定你正在嘗試基於代碼和有限的解釋完成的任務。我提供的是工作代碼,所以也許這將有助於你朝着正確的方向前進?

+0

不錯!但問題是,紅色的div有相同的寬度,而不是137px ...當我點擊黃色div – andre34

+0

,所以向後的div到頂部,然後紅色的div必須從395px到137px ...在他的正常大小 – andre34

+0

http://jsfiddle.net/UqgXc/32/我在這裏解決了它:) – andre34