首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >小马车JQuery .animation()

小马车JQuery .animation()
EN

Stack Overflow用户
提问于 2013-05-29 21:48:14
回答 2查看 419关注 0票数 0

这里我有6个div (.sticker)在一个div中,onClicking其中一个我想要fadeOut其他人将点击的div保持在它的位置(这就是为什么我做了后置/偏置的事情)然后我想把它移动到较大的div的中间,当它以高度和宽度增长时,显示一个隐藏的div (.info)。闭幕式也一样!所以,这段代码是可以工作的,但它确实是滞后的,它不像jQuery应该是平滑的,我是不是做错了什么?

感谢所有的人!

代码语言:javascript
运行
复制
$("body").on('click', '.sticker', function () {

    if (!is_open) {
        postop = $(this).position().top;
        posleft = $(this).position().left;
        $('.sticker').not(this).fadeOut(350, function () {
            $(".sticker").css("position", "absolute").css("left", posleft + "px").css("top", postop + "px");
            $(".sticker").animate({
                'top': '0px',
                'left': '300px',
                'height': '480px',
                'width': '750px',
                'left': '90px'
            }, 350);
            $(".sticker").children(".wrap").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $(".sticker").find(".imgspace").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $(".sticker").find(".info").animate({
                'height': '100px'
            }, 350);
            $('.arrow-left').animate({
                'left': '-20px'
            }, 450);
            $('.arrow-right').animate({
                'left': '880px'
            }, 450);
            is_open = true;

        });
    }
    if (is_open) {
        $(".sticker").children(".wrap").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $(".sticker").find(".imgspace").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $(".sticker").find(".info").animate({
            'height': '0px'
        }, 350);
        $(".sticker").animate({
            'height': '230px',
            'width': '300px',
            'top': postop,
            'left': posleft
        }, 350, function () {
            $(".sticker").css("position", "static");
            $(".sticker").not(this).fadeIn(300);
            is_open = false;
        });

    }

});
EN

回答 2

Stack Overflow用户

发布于 2013-05-29 21:51:16

当您单击其中一个时,您将希望使用.siblings来隐藏所有其他内容。我会对jQuery API文档做一些研究。这就是我要开始的地方。

票数 1
EN

Stack Overflow用户

发布于 2013-05-29 22:22:55

正如Yotam所评论的,没有jsFiddle很难调试。但是,一些突出在我身上的事情(绝不是详尽无遗的,我也不是JavaScript专家):

  1. 缓存jQuery对象,而不是继续请求DOM (var $sticker = $('.sticker'))
  2. 把你的动画链接在同一个物体上
  3. $sticker.children(".wrap")和$sticker.find(".imgspace")有什么区别?你能不使用$sticker.find(".wrap,.imgspace")吗?

您可以通过将值设置为变量对象来进一步简化代码,而不是使用不同的值两次调用相同的方法。

代码语言:javascript
运行
复制
$("body").on('click', '.sticker', function () {

    if (!is_open) {
        var $position = $(this).position(),
            $sticker = $('.sticker');

        $sticker.not(this).fadeOut(350, function () {
            $sticker.css({ 
                        position: 'absolute', 
                        left: $position.left+'px', 
                        top: $position.top+'px'
                    })
                    .animate({
                        'top': '0px',
                        'left': '300px',
                        'height': '480px',
                        'width': '750px',
                        'left': '90px'
                    }, 350);
            $sticker.find(".wrap, .imgspace").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $sticker.find(".info").animate({ 'height': '100px' }, 350);
            $('.arrow-left').animate({ 'left': '-20px' }, 450)
                            .animate({ 'left': '880px' }, 450);
            is_open = true;

        });
    }
    if (is_open) {
        $sticker.find(".wrap, .imgspace").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $sticker.find(".info").animate({
            'height': '0px'
        }, 350);
        $sticker.animate({
            'height': '230px',
            'width': '300px',
            'top': $position.top,
            'left': $position.left
        }, 350, function () {
            $sticker.css("position", "static")
                    .not(this).fadeIn(300);
            is_open = false;
        });
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16824447

复制
相关文章

相似问题

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