简体   繁体   English

通过CSS3过渡将元素移到新位置

[英]Move element into new position with CSS3 transitions

I am trying to get a gallery working and I am trying to limit the usage of jQuery in this case. 我试图使图库正常工作,并且在这种情况下试图限制jQuery的使用。 As shown in the following screenshot, I always have 2 items beside each other: http://prntscr.com/2gpi01 . 如以下屏幕快照所示,我总是彼此旁边有2个项目: http : //prntscr.com/2gpi01 They are set to float left and whenever one is removed or set to display none, another item simply takes its place. 它们被设置为向左浮动,并且每当一个被删除或不显示时,另一项就被取代。 This working fine, however I want to add a transition, in order for the new item to move into the old item's place. 这项工作正常,但是我想添加一个过渡,以便将新项目移到旧项目的位置。 I have seen this quite often online, that by simply removing an item, another item slides smoothly into its new place. 我在网上经常看到这种情况,只需移除一个项目,另一个项目就可以顺利滑入新位置。 Is this somehow possible with CSS3 transitions? CSS3转换是否可能以某种方式实现?

You can animate max-width using transition . 您可以使用transition max-width设置动画。

On the click event, you need do add class hidden to the clicked element. 在click事件上,您需要将hidden的类添加到clicked元素中。

If you have a markup like this: 如果您有这样的标记:

<ul class="gallery">
    <li><img src="image1" alt=""></li>
    <li><img src="image2" alt=""></li>
</ul>

You can do the animation with the following CSS: 您可以使用以下CSS制作动画:

ul.gallery li {
  float: left;
  list-style-type: none;
  transition: max-width 2s;
  max-width: 250px;
  overflow: hidden;
}

ul.gallery li.hidden {
  max-width: 0;
}

ul.gallery li img {
  height: 150px;
  margin: 8px;
}

Here is a demo: http://cssdeck.com/labs/pqbkirfl 这是一个演示: http : //cssdeck.com/labs/pqbkirfl

That is not possible using CSS, since CSS won't check if any child element is removed or not! 使用CSS是不可能的,因为CSS不会检查是否删除了任何子元素!

You can try out the animation using 您可以尝试使用以下动画

transition: margin-top 2s;

This way, the margin-top change for the element will take place in 2 seconds in an animating way. 这样,该元素的页边距顶部更改将以动画方式在2秒内发生。

But to detect the removal of the element you are required to use jQuery! 但是要检测元素的删除,您需要使用jQuery!

Since you want the animation to take place only if the element is removed, you can try out adding class or removing a class for the other elements. 由于您希望动画仅在元素被删除时才发生,因此您可以尝试添加类或为其他元素删除类。 This way, when the class is added the animation (CSS) will take place and it will animate its property (margin-top). 这样,在添加类时,将发生动画(CSS)并将其属性设置为动画(margin-top)。

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

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