简体   繁体   English

删除元素后的jQuery动画

[英]jQuery animation after removing elements

I have the following HTML and CSS: 我有以下HTML和CSS:

<div id="categories">
        <h3 id="sports">Sports</h3>

        <h3 id="videogames">Video Games</h3>

        <h3 id="music">Music</h3>

        <h3 id="web">Web</h3>
</div>

#categories {
    left: 50%;
    top: 50%;
    position: absolute;
    -webkit-transform: translate3d(-50%, -50%, 0);
    -moz-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
}

#categories > h3 {
    display: inline;
}

The h3 elements are displaying inline and centered. h3元素以内联和居中显示。 I have the following code in jQuery that when you click an h3 element, the other elements fade out. 我在jQuery中有以下代码,当您单击h3元素时,其他元素会淡出。 It works well to remove the elements, but once they disappear, the selected element just suddenly flashes into the center (which is where I want it) but is there a way to animate this? 它可以很好地删除元素,但是一旦它们消失,选定的元素就会突然闪烁到中心(这是我想要的位置),但是有没有办法对此进行动画处理? Make it a smoother transition? 使过渡更平滑吗?

$("#categories h3").click(function(){
    if(this.id == "sports"){
        $("#videogames").fadeOut(500);
        $("#music").fadeOut(500);
        $("#web").fadeOut(500);
    }
})

Use transition, much better. 使用过渡效果更好。

 $("#categories h3").click(function(){ if(this.id == "sports"){ $("#videogames, #music, #web").css({opacity: 0}); } }); 
 #categories { left: 50%; top: 50%; position: absolute; -webkit-transform: translate3d(-50%, -50%, 0); -moz-transform: translate3d(-50%, -50%, 0); transform: translate3d(-50%, -50%, 0); } #categories > h3 { display: inline; transition: opacity .3s; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="categories"> <h3 id="sports">Sports</h3> <h3 id="videogames">Video Games</h3> <h3 id="music">Music</h3> <h3 id="web">Web</h3> </div> 

May be you can use this. 也许您可以使用此功能。 correct me if i am wrongly understood. 如果我理解错误,请纠正我。

if(this.id == "sports"){
    $("#videogames").fadeOut(500);
    $("#music").fadeOut(500);
    $("#web").fadeOut(500);
    $("#sports").fadeOut(500);
    $("#sports").fadeIn(500);
  }

To answer your question about smoothly transitioning the selected element to the center try the below code. 要回答有关将所选元素平滑过渡到中心的问题,请尝试以下代码。

Your fundamental problem is that "The .fadeOut() method animates the opacity of the matched elements. Once the opacity reaches 0, the display style property is set to none, so the element no longer affects the layout of the page." 您的基本问题是“ .fadeOut()方法可对匹配元素的不透明度进行动画处理。不透明度达到0后,显示样式属性将设置为none,因此该元素不再影响页面的布局。” And so the remaining selected element jumps to the center. 因此,其余选定元素将跳到中心。 And you cannot transition to display: none. 而且您无法转换为显示:无。 So you need to find a property you can animate, like "left" which I have used here. 因此,您需要找到一个可以设置动画的属性,例如我在这里使用的“ left”。

[As an aside there is some extra code there because I was testing the ability to animate flex "order" but it doesn't work at this time on Edge, and untested on other browsers. [[顺便说一句,那里有一些额外的代码,因为我正在测试使flex“ order”动画化的功能,但是目前在Edge上还不起作用,并且在其他浏览器上也未测试。 You do not need the order properties.] 您不需要订单属性。]

Hope this helps. 希望这可以帮助。

 $("#categories h3").click( function() { var thisID = this.id; $.each( $("#categories h3"), function (index, val) { if (thisID == val.id) { var containerWidth = $("#categories").width(); var thisWidth = val.getBoundingClientRect().width; var thisComputedLeft = val.getBoundingClientRect().left; var adjustLeft = (containerWidth / 2) - thisComputedLeft - (thisWidth / 2); // to center the clicked element $(this).css({ left: adjustLeft }); } else $(val).css({opacity: 0}); }); }); 
 #categories { position: relative; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-flow: row nowrap; -ms-flex-flow: row nowrap; flex-flow: row nowrap; justify-content: space-around; align-content: center; width: 100%; height: 100%; //margin: 0 auto; } #categories > h3 { cursor: pointer; position: relative; left: 0; width: auto; transition: all 0.5s ease, opacity 0.3s; // transition: opacity 0.3s; } #sports { order: 1; } #videogames { order: 2; } #music { order: 3; } #web { order: 4; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="categories"> <h3 id="sports">Sports</h3> <h3 id="videogames">Video Games</h3> <h3 id="music">Music</h3> <h3 id="web">Web</h3> </div> 

Try the following: 请尝试以下操作:

$("#categories h3").on('click', function(){
    if(this.id == "sports"){
        $("#videogames").fadeOut(500, function(){
            $("#music").fadeOut(400, function(){
                $("#web").fadeOut(300, function(){
                     $("#sports").fadeIn(400);
                });
            });
        });
     }
 });

The callback function is called once the first animation is complete. 第一个动画完成后,将调用回调函数。 You can nest the functions for a sequence animation. 您可以嵌套序列动画的功能。

Update: 更新:

Here are better examples for chaining animations: Chaining jQuery animations that affect different elements 以下是链接动画的更好示例: 链接影响不同元素的jQuery动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM