我正在寻找一种方法来切换元素的可见性,方法是滑动元素,同时平滑地将位于目标位置的元素推开。
直到现在,我尝试了jQuery和jQuery Ui的两种幻灯片效果,看起来我需要两者的混合。
尽管jQuery的幻灯片扩展了所讨论的元素,但我更希望它能像在带有overflow: hidden的容器中那样(就像jQuery Ui那样)在容器中滑动。不幸的是,jQuery Ui不会在动画期间重新定位相邻的元素,而是让它们在动画完成后跳到自己的位置。
我创建了一个小提琴来向你展示我的意思。我的问题是:是否有一种方法可以让幻灯片在jQuery Ui中,而不需要随后的元素跳到它的位置,而是顺利地移动到那里呢?
非常感谢您的建议!
发布于 2011-08-30 14:20:13
您可以添加包含DIV的内容并设置其高度:
发布于 2011-09-01 06:58:46
我采纳了ManseUK的想法,扩展了jQuery Ui的常规幻灯片效果,使自动创建的包装器div的高度在幻灯片动画期间得到了调整。
$.effects.customSlide = function(o) {
return this.queue(function() {
// Create element
var el = $(this),
props = ['position', 'top', 'bottom', 'left', 'right'];
// Set options
var mode = $.effects.setMode(el, o.options.mode || 'show'); // Set Mode
var direction = o.options.direction || 'left'; // Default Direction
// Adjust
$.effects.save(el, props);
el.show(); // Save & Show
$.effects.createWrapper(el).css({
overflow: 'hidden'
}); // Create Wrapper
var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
var distance = o.options.distance || (ref == 'top' ? el.outerHeight({
margin: true
}) : el.outerWidth({
margin: true
}));
if (mode == 'show') el.parent().css('height', 0);
if (mode == 'show') el.css(ref, motion == 'pos' ? (isNaN(distance) ? "-" + distance : -distance) : distance); // Shift
// Animation
var animation = {};
animation[ref] = (mode == 'show' ? (motion == 'pos' ? '+=' : '-=') : (motion == 'pos' ? '-=' : '+=')) + distance;
el.parent().animate({
height: (mode == 'show' ? distance : 0)
}, {
queue: false,
duration: o.duration,
easing: o.options.easing
});
el.animate(animation, {
queue: false,
duration: o.duration,
easing: o.options.easing,
complete: function() {
if (mode == 'hide') el.hide(); // Hide
$.effects.restore(el, props);
$.effects.removeWrapper(el); // Restore
if (o.callback) o.callback.apply(this, arguments); // Callback
el.dequeue();
}
});您可以在此小提琴中查看结果。
发布于 2012-11-15 19:53:56
谢谢,jnns。干得好!
这是您的解决方案,更新后的jQuery UI版本为1.9。
$.effects.effect.customSlide = function (options) {
var el = $(this),
props = ['position', 'top', 'bottom', 'left', 'right'];
// Set options
var mode = $.effects.setMode(el, options.mode || 'show'); // Set Mode
var direction = options.direction || 'left'; // Default Direction
// Adjust
$.effects.save(el, props);
el.show(); // Save & Show
$.effects.createWrapper(el).css({
overflow: 'hidden'
}); // Create Wrapper
var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
var distance = options.distance || (ref == 'top' ? el.outerHeight(true) : el.outerWidth(true));
if (mode == 'show') el.parent().css('height', 0);
if (mode == 'show') el.css(ref, motion == 'pos' ? (isNaN(distance) ? "-" + distance : -distance) : distance); // Shift
// Animation
var animation = {};
animation[ref] = (mode == 'show' ? (motion == 'pos' ? '+=' : '-=') : (motion == 'pos' ? '-=' : '+=')) + distance;
el.parent().animate({
height: (mode == 'show' ? distance : 0)
}, {
queue: false,
duration: options.duration,
easing: options.easing
});
el.animate(animation, {
queue: false,
duration: options.duration,
easing: options.easing,
complete: function () {
if (mode == 'hide') el.hide(); // Hide
$.effects.restore(el, props);
$.effects.removeWrapper(el); // Restore
if (options.callback) options.callback.apply(this, arguments); // Callback
el.dequeue();
}
});
};https://stackoverflow.com/questions/7244382
复制相似问题