這裏是我的解決方案,你可以使用它作爲一個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。
我一直在努力工作,這一點對一點。我沒有得到'正確'的答案,但這可能會幫助你:$('bla')。slideDown基本上映射到源代碼中:$('bla')。animate({height:'show',paddingTop: 'show',paddingBottom:'show',marginTop:'show',marginBottom:'show'}。所以你可以放一下,動畫就像slideDown一樣動作 – Brandon 2011-08-22 21:41:59
@ iWebaholic:請選擇正確的anser – 2014-06-23 11:09:45
請選擇@ pbierre的答案是可以接受的答案,很好的解決方案 – earl3s 2016-07-13 18:27:10