首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jQuery Ui的幻灯片效果时也可以滑动后续元素

问使用jQuery Ui的幻灯片效果时也可以滑动后续元素
EN

Stack Overflow用户
提问于 2011-08-30 13:56:13
回答 3查看 1.8K关注 0票数 3

我正在寻找一种方法来切换元素的可见性,方法是滑动元素,同时平滑地将位于目标位置的元素推开。

直到现在,我尝试了jQuery和jQuery Ui的两种幻灯片效果,看起来我需要两者的混合。

尽管jQuery的幻灯片扩展了所讨论的元素,但我更希望它能像在带有overflow: hidden的容器中那样(就像jQuery Ui那样)在容器中滑动。不幸的是,jQuery Ui不会在动画期间重新定位相邻的元素,而是让它们在动画完成后跳到自己的位置。

我创建了一个小提琴来向你展示我的意思。我的问题是:是否有一种方法可以让幻灯片在jQuery Ui中,而不需要随后的元素跳到它的位置,而是顺利地移动到那里呢?

非常感谢您的建议!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-30 14:20:13

您可以添加包含DIV的内容并设置其高度:

http://jsfiddle.net/Q2dSZ/4/

票数 0
EN

Stack Overflow用户

发布于 2011-09-01 06:58:46

我采纳了ManseUK的想法,扩展了jQuery Ui的常规幻灯片效果,使自动创建的包装器div的高度在幻灯片动画期间得到了调整。

代码语言:javascript
复制
$.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();
            }
        });

您可以在此小提琴中查看结果。

票数 6
EN

Stack Overflow用户

发布于 2012-11-15 19:53:56

谢谢,jnns。干得好!

这是您的解决方案,更新后的jQuery UI版本为1.9。

代码语言:javascript
复制
$.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();
        }
    });
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7244382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档