我正在製作一個網站,其中一些div滑動到屏幕上。 如何讓已經在前面的div在按下相應的按鈕時不播放動畫?jquery - 如何玩.animate()一次,並用其他按鈕重置它
我設立了jQuery像這樣每個格:
$(button).click(function(event) {
$(div).css("right", "-100%");
$(div).animate({right: '0%'});
$(div).css("z-index", "1");
$(other-div).css("z-index", "0");
});
我需要做的「隱藏」 div的播放動畫,使他們走到了前面。如果div已經在前面,我只是不希望動畫播放。
這裏是什麼,我有一個小提琴:好像是這樣)
https://jsfiddle.net/cn6goeuq/
我曾嘗試使用.delay(:
$(button).click(function(event) {
$(div).animate({right: '0%'});
$(div).css("z-index", "1");
$(other-div).css("z-index", "0");
$(other-div).delay(2000).css("right", "-100");
});
,使隱藏的div會回到「右:-100%'未被注意到,但它不起作用。不知道爲什麼。
我也嘗試使用一個變量來幫助我激活和停用正在顯示的div的動畫,但也是徒勞的。我想是這樣的:
var x = 0;
var y = 0;
$(button1).click(function(event1){
$(div1).animate({right: '0%'});
x++;
y=0;
if (x >= 1) {
$(this).off(event1);
}
});
$(button2).click(function(event2){
$(div2).animate({right: '0%'});
y++;
x=0;
if (y >= 1) {
$(this).off(event2);
}
});
我想使事件1工作只有一次按下按鈕1時,如果按下另一個按鈕復位,但它也沒有工作。
我跑出瞭如何解決這個問題的想法!
編輯
恐怕使動畫的條件並沒有正常工作。我有完全一樣的結果。我試着根據z-index做出條件。就像這樣:
$(button).click(function(event) {
if($(div).css('z-index') !== '0'){
$(div).css("right", "-100%");
$(div).animate({right: '0%'});
};
$(div).css("z-index", "1");
$(other-div).css("z-index", "0");
});
恐怕有條件地製作動畫不起作用。我有完全一樣的結果。我也試着根據z-index做出條件。像這樣: $(button).click(function(event){if($(div).css('z-index')!=='0'){ $(div).css(「 $(div).css(「z-index」,「1」); $(other-div).css(「z-index」,「0」); }); – JoseSoares
@JoseSoares這個小提琴能解決你的問題嗎? https://jsfiddle.net/cn6goeuq/2/ –
@ Chris.s是的,它的工作完美。原來我以某種方式錯誤地使用了它。我只是使用z-index作爲條件,因爲它在網站本身的整個div中更加一致。我在上次編輯中使用了我在問題中發佈的代碼,但只做了輕微更改。 – JoseSoares