【问题标题】:how to animate nested divs - jquery?如何为嵌套的 div 设置动画 - jquery?
【发布时间】:2011-10-25 15:00:27
【问题描述】:

我有一些模拟谷歌图像效果的代码。 如何为div class="productBox" 中的嵌套 div 设置动画,而不仅仅是这个 div ??? 我也想在动画期间更改“imageProduct”类的高度。

这里是 jsFiddle 上的代码:http://jsfiddle.net/S2svG/57/

这里是html:

<div class="productBox">
    <div class="productImage"><a href="#"><img src="http://some_image.jpg"></a></div>
    <div class="productTitle">Product title</div>
    <div class="productDescription">Here is description of the product.</div>
    <div class="buyButton"><a href="#">Buy this!</a></div>
</div>

还有js:

$(function(){
$.fn.popOut=function(user_opts){            
    return this.each(function(){

        var opts=$.extend({
            useId:"poppedOut",
            padding:20,
            border:0,
            speed:200
        },user_opts);

        $(this).mouseover(function(){
            // kill any instance of this already
            $("#"+opts.useId).remove();

            // make a copy of the hovered guy
            var $div=$(this).clone();

            // setup for prelim stuff
            $div.css({
                "position":"absolute",
                "border":opts.border,
                "top":$(this).offset().top,
                "left":$(this).offset().left,
                "-moz-box-shadow":"0px 0px 12px black",
                "-webkit-box-shadow":"0px 0px 12px black",
                "z-index":"99"
            });

            // store all of the old props so it can be animate back
            $div.attr("id",opts.useId)
                .attr("oldWidth",$(this).width())
                .attr("oldHeight",$(this).height())
                .attr("oldTop",$(this).offset().top)
                .attr("oldLeft",$(this).offset().left)
                .attr("oldPadding",$(this).css("padding"));

            // put this guy on the page
            $("body").prepend($div);

            // animate the div outward
            $div.animate({
                "top":$(this).offset().top-Math.abs($(this).height()-opts.height),
                "left":$(this).offset().left-opts.padding,
                "height":opts.height,
                "padding":opts.padding
            },opts.speed);

            // loop through each selector and animate it to its css object
            for(var eachSelector in opts.selectors){
                var selectorObject=opts.selectors[eachSelector];
                for(var jquerySelector in selectorObject){
                    var cssObject=selectorObject[jquerySelector];
                    $div.find(jquerySelector).animate(cssObject,opts.speed);
                }
            }

            $div.mouseleave(function(){
                $("#"+opts.useId).animate({
                    width:$(this).attr("oldWidth"),
                    height:$(this).attr("oldHeight"),
                    top:$(this).attr("oldTop"),
                    left:$(this).attr("oldLeft"),
                    padding:$(this).attr("oldPadding")
                },0,function(){
                    $(this).remove();
                });
            });
        });
    });
};
$(".productBox").popOut({
    height:300,
    border:"1px solid #333"
});                       
}); 

提前致谢!!!

【问题讨论】:

    标签: javascript jquery css jquery-animate


    【解决方案1】:
    $(this).find(".productImage").animate({...});
    

    【讨论】:

    • 在哪里粘贴它以及如何在那里传输必要的参数?
    • 是这样的吗? jsfiddle.net/S2svG/76 这样做不太顺利.. :/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2012-04-04
    • 1970-01-01
    • 2020-12-27
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多