【问题标题】:Animating Prepend and Append with floats动画前置和附加浮动
【发布时间】:2014-07-16 23:18:12
【问题描述】:

这看起来很简单,但我正处于最糟糕的时期。我有以下代码在以下 div 上工作。很想为滑块设置动画,使其看起来逐渐向前推进,但是当我在 $(".person") 上使用 .animate() 时,项目动画,然后附加了额外的 div,一切最终都超出了我的预期。我的目标是让滑块逐渐推进其中一张图片的宽度,即 12.5%

HTML:

<div id="people-reel">
    <div class="person" id="alex"></div><!-- end #alex -->
    <div class="person" id="jenna"></div><!-- end #jenna -->
    <div class="person" id="martha"></div><!-- end #martha -->
    <div class="person" id="matt"></div><!-- end #matt -->
    <div class="person" id="mia"></div><!-- end #mia -->
    <div class="person" id="rich"></div><!-- end #rich -->
    <div class="person" id="ryan"></div><!-- end #ryan -->
    <div class="person" id="silvia"></div><!-- end #silvia -->
    <div class="person" id="tony"></div><!-- end #tony -->
</div><!-- end #people-reel -->
<div class="controller" id="left-controller"></div><!-- end #left-controller -->
<div class="controller" id="right-controller"></div><!-- end #left-controller -->

CSS:

#people-reel {
    width: 120%;
    left: -10%;
    background: rgba(255,255,255,0.3);
    height: 200px;
    position: absolute;
    bottom: 15%;
    overflow: hidden;
    -webkit-transition: all 0.5s ease-in;
    -moz-transition: all 0.5s ease-in;
    -o-transition: all 0.5s ease-in;
    transition: all 0.5s ease-in;
}

#alex {background-image: url('img/our-team/alex.jpg');}
#jenna {background-image: url('img/our-team/jenna.jpg');}
#martha {background-image: url('img/our-team/martha.jpg');}
#matt {background-image: url('img/our-team/matt.jpg');}
#mia {background-image: url('img/our-team/mia.jpg');}
#rich {background-image: url('img/our-team/rich.jpg');}
#ryan {background-image: url('img/our-team/ryan.jpg');}
#silvia {background-image: url('img/our-team/silvia.jpg');}
#tony {background-image: url('img/our-team/tony.jpg');}

.person {
    height: 200px;
    width: 12.5%;
    background-image: url('img/our-team/mia.jpg');
    background-size: cover;
    background-position: center;
    float: left;
    display: inline-block;
    cursor: pointer;
    -webkit-transition: all 0.5s ease-in;
    -moz-transition: all 0.5s ease-in;
    -o-transition: all 0.5s ease-in;
    transition: all 0.5s ease-in;
}

JavaScript:

function moveToLeft(){
    $("#left-controller").on("click", function(){       
        var firstElement = $(".person").first();
        $("#people-reel").append(firstElement);
    });
}
function moveToRight(){
    $("#right-controller").on("click", function(){      
        var firstElement = $(".person").last();
        $("#people-reel").prepend(firstElement);
    });
}

moveToLeft();
moveToRight();

【问题讨论】:

  • 当您附加或添加一个项目时,您需要在动画中补偿它的宽度。首先我建议使用偏移量为容器制作动画,而不是在动画完成后为每个 .person 制作动画。同时进行宽度的附加和补偿..
  • 如果你需要帮助编码这个..把它放在jsfiddle.net,我可以帮助你。
  • 非常感谢您的指点!我刚刚尝试了你的概念,它奏效了!

标签: javascript jquery html css animation


【解决方案1】:

非常感谢 Webkit 的指点!以下代码有效:

function moveToLeft(){
    $("#left-controller").on("click", function(){       
        var firstElement = $(".person").first();
        var inc = $("#people-reel").width() * 0.125;
        firstElement.animate({'width': "0"}, "fast");
        setTimeout(function(){$("#people-reel").append(firstElement);}, 1000);
        setTimeout(function(){firstElement.animate({'width': inc}, 0);}, 1000);
    });
}
function moveToRight(){
    $("#right-controller").on("click", function(){      
        var lastElement = $(".person").last();
        var inc = $("#people-reel").width() * 0.125;
        lastElement.animate({'width': "0"}, "fast");
        setTimeout(function(){$("#people-reel").prepend(lastElement);}, 500);
        setTimeout(function(){lastElement.animate({'width': inc}, 0);}, 500);
    });
}

moveToLeft();
moveToRight();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多