我有一个display为none的div; 现在我想同时使用fadeIn和slideDown来显示它。
$(this).slideDown({duration: 'slow', queue: false});
$(this).fadeIn({duration: 'slow', queue: false});
元素已被正确选择。但当我触发效果时,它只会执行slideDown动画。如果我删除slideDown,我可以看到fadeIn动画,因此语法没有问题。但为什么它不会触发两个动画?
我有一个display为none的div; 现在我想同时使用fadeIn和slideDown来显示它。
$(this).slideDown({duration: 'slow', queue: false});
$(this).fadeIn({duration: 'slow', queue: false});
使用 animate()
替代 fadeIn()
:
$(this)
.css('opacity', 0)
.slideDown('slow')
.animate(
{ opacity: 1 },
{ queue: false, duration: 'slow' }
);
从 height:0px
和 opacity:0; filter: alpha(opacity = 0)
开始,然后在动作上执行:
$(this).stop().animate({
height: 200,
opacity: 1
}, 350);
将高度(我设置为200)和持续时间(我设置为350)更改为您想要的任何值。
这是我的解决方案,你可以将其用作 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));
// Show
$('.alert').showDown('slow');
// Hide
$('.alert').hideUp('fast', function() {
// Animation complete: '.alert' is now hidden
});
这将使用淡入淡出效果调整我们元素的高度。
它最初是在我的博客上发布的(链接).
.css('opacity', 0)
和 .css('opacity', 1)
,因为当我想要在对象仍在 showDown
的时候隐藏它时,不透明度会直接变为 1,然后才尝试隐藏。 - RoLYroLLs$(this)...
以允许方法链接。 - Paul$('.target')
.hide()
.slideDown(500, 'swing')
.css('opacity', 0)
.animate({opacity: 1}, {queue: false, duration: 1000});
'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>
, "slow"
以使其更加流畅。 - iorgv $(document).ready(function() {
$("#test").bind("click", function() {
setTimeout(function() {
$('#slidedown').slideDown("slow");
}, 500);
$("#content").fadeOut(500);
$(this).stop().animate({ "opacity": "1" }, "slow");
});
});
现在可以使用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');
}
});