2011-04-02 43 views
46

我有一個div與顯示:無; 現在我想同時使用fadeIn和slideDown來顯示它。如何同時運行jQuery fadeIn()和slideDown()?

$(this).slideDown({duration: 'slow', queue: false}); 
$(this).fadeIn({duration: 'slow', queue: false}); 

該div被正確選擇。但是當我觸發效果時,它所做的只是slideDown。如果我只是刪除slideDown,我可以看到fadeIn,所以語法沒有任何問題。 但爲什麼不觸發這兩個動畫?

+0

我一直在努力工作,這一點對一點。我沒有得到'正確'的答案,但這可能會幫助你:$('bla')。slideDown基本上映射到源代碼中:$('bla')。animate({height:'show',paddingTop: 'show',paddingBottom:'show',marginTop:'show',marginBottom:'show'}。所以你可以放一下,動畫就像slideDown一樣動作 – Brandon 2011-08-22 21:41:59

+0

@ iWebaholic:請選擇正確的anser – 2014-06-23 11:09:45

+0

請選擇@ pbierre的答案是可以接受的答案,很好的解決方案 – earl3s 2016-07-13 18:27:10

回答

4

開始與height:0pxopacity:0; filter: alpha(opacity = 0)然後在動作做:

$(this).stop().animate({ 
    height: 200, 
    opacity: 1 
}, 350); 

改變其高度(我設置爲200),以及任何你想要的時間(我設置爲350)。

140

使用animate()代替fadeIn()

$(this) 
    .css('opacity', 0) 
    .slideDown('slow') 
    .animate(
    { opacity: 1 }, 
    { queue: false, duration: 'slow' } 
); 
+7

這個答案應該選擇正確的答案!完美的工作。謝謝! – 2013-01-21 16:58:41

+1

是...這是正確的答案。但是,我更喜歡「快速」而不是「慢速」動畫,因爲即使UI響應速度的微小延遲也會刺激用戶。 – PhilNicholas 2015-08-06 02:56:08

+1

也許更多的解釋爲什麼他們不能同時運行?不知道爲什麼這還沒有被選作正確的答案。 – 2017-01-20 18:29:58

5

這裏是我的解決方案,你可以使用它作爲一個jQuery插件。

(function($) { 
    'use strict'; 
    // Sort us out with the options parameters 
    var getAnimOpts = function (a, b, c) { 
      if (!a) { return {duration: 'normal'}; } 
      if (!!c) { return {duration: a, easing: b, complete: c}; } 
      if (!!b) { return {duration: a, complete: b}; } 
      if (typeof a === 'object') { return a; } 
      return { duration: a }; 
     }, 
     getUnqueuedOpts = function (opts) { 
      return { 
       queue: false, 
       duration: opts.duration, 
       easing: opts.easing 
      }; 
     }; 
    // Declare our new effects 
    $.fn.showDown = function (a, b, c) { 
     var slideOpts = getAnimOpts(a, b, c), fadeOpts = getUnqueuedOpts(slideOpts); 
     $(this).hide().css('opacity', 0).slideDown(slideOpts).animate({ opacity: 1 }, fadeOpts); 
    }; 
    $.fn.hideUp = function (a, b, c) { 
     var slideOpts = getAnimOpts(a, b, c), fadeOpts = getUnqueuedOpts(slideOpts); 
     $(this).show().css('opacity', 1).slideUp(slideOpts).animate({ opacity: 0 }, fadeOpts); 
    }; 
}(jQuery)); 

現在您可以像使用jQuery的.fadeIn(或fadeOut)效果一樣使用它了。

// Show 
$('.alert').showDown('slow'); 
// Hide 
$('.alert').hideUp('fast', function() { 
    // Animation complete: '.alert' is now hidden 
}); 

這將調整我們的元素的高度與衰落效果。

它是originally posted on my blog

+0

+1非常好。我刪除了'.css('opacity',0)'和'.css('opacity',1)',因爲當對象仍然是'showDown'時,如果我想'隱藏',不透明度將直接變爲1在它試圖隱藏之前。 – RoLYroLLs 2013-07-15 17:13:04

+1

我覺得這兩個函數都應該'返回$(this)...'來允許方法鏈接。 – Paul 2017-05-23 10:40:19

0
 $(document).ready(function() { 
    $("#test").bind("click", function() { 
      setTimeout(function() { 
      $('#slidedown').slideDown("slow"); 
     }, 500); 
     $("#content").fadeOut(500); 
     $(this).stop().animate({ "opacity": "1" }, "slow"); 
     }); 
    }); 

這是爲淡出,但我認爲這是你的後。請看看這個例子:http://jsfiddle.net/oddacon/M44md/

0

現在可以使用CSS3轉換了。它允許實現非常靈活的解決方案。

HTML

<div id="btn">Click me</div> 
<div id="hidden"></div> 

CSS

#hidden { 
    display: none; opacity: 0; height: 100px; background: red; 
    transition: opacity 600ms ease-in-out 0s; 
} 
#hidden.opened { 
    opacity: 1; 
} 

jQuery的

$('#btn').click(function() { 
    var div = $('#hidden'); 
    if (! div.is(':animated')) { 
     div.slideToggle(600).toggleClass('opened'); 
    } 
}); 
1

更現代的解決辦法是,當你想動畫結合使用的'show''hide'值:

$('.show').on('click', function() { 
 
    $('.example').animate({ 
 
    opacity: 'show', 
 
    height: 'show', 
 
    marginTop: 'show', 
 
    marginBottom: 'show', 
 
    paddingTop: 'show', 
 
    paddingBottom: 'show' 
 
    }) 
 
}) 
 
$('.hide').on('click', function() { 
 
    $('.example').animate({ 
 
    opacity: 'hide', 
 
    height: 'hide', 
 
    marginTop: 'hide', 
 
    marginBottom: 'hide', 
 
    paddingTop: 'hide', 
 
    paddingBottom: 'hide' 
 
    }) 
 
})
.example { 
 
    background-color: blue; 
 
    height: 200px; 
 
    width: 200px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p> 
 
    <button type="button" class="show">Show</button> 
 
    <button type="button" class="hide">Hide</button> 
 
</p> 
 
<div class="example"></div>

+0

正是我需要的,謝謝!我在花括號的末尾添加了''「slow」,以使它更平滑。 – iorgu 2018-02-18 12:18:52