简体   繁体   English

CSS过渡时间不适用于转换

[英]CSS transition time not applied on transform

I have a div that should slide to the left smoothly over .5s. 我有一个div,应该在.5s内平稳地向左滑动。 Right now it's instantly popping to its new position, not sliding. 现在,它立即弹出到新位置,而不是滑动。

I'm using jQuery to apply a tranlsateX to the div after a button is pushed. 按下按钮后,我正在使用jQuery将tranlsateX应用于div。 It triggers some ajax which adds a transform to the div's css (at bottom): 它会触发一些ajax,从而向div的css添加一个转换(在底部):

 $.ajax({
         method: 'POST',
         url: '/cities/popup',
         data: { latitude: latitude, longitude: longitude },
         dataType: 'html',
         success: function(data){
          $(".popup").css("display", "block");
          $(".popup").empty();
          $(".popup").append(data);
          $(".popup").css("transform", "translateX(-100%)");
 });

Here's all of the styling on the popup class, notice that I'm defining transform time should be 3s at the end of the code block. 这是popup类的所有样式,请注意,我定义的转换时间应在代码块末尾为3s。

.popup{
  background-color: #FFFFFF;
  display: none;
  position: absolute;
  overflow: scroll;
  z-index: 5;
  right: -40%;
  width: 40%;
  height: 100%;
  -webkit-box-shadow: -6px 5px 35px 0px rgba(133,133,133,0.53);
  -moz-box-shadow: -6px 5px 35px 0px rgba(133,133,133,0.53);
  box-shadow: -6px 5px 35px 0px rgba(133,133,133,0.53);
  transition: transform 3s;
}

The transform is working, the div moves, but it's not an animated movement over .5s, simply appears in the new spot instantly. 转换有效,div移动,但不是超过0.5s的动画移动,只是立即出现在新位置。 I'm really not sure how to begin troubleshooting this. 我真的不确定如何开始对此进行故障排除。

The cause 原因

The transition doesn't work because of display: none . 过渡由于display: none而无法正常工作display: none display: none to display: block can't be animated, so it breaks the transition. display: none display: block无法设置动画,因此会中断过渡。

Solution

One solution is to hide the element without using display: none . 一种解决方案是不使用display: none隐藏元素display: none The following example works when the div is hidden with opacity: 0 and shown with opacity: 1 . 以下示例在div以opacity: 0隐藏并以opacity: 1显示时起作用。

Note : where display: none removes the element completely, opacity: 0 simply hides it and it can still be interacted with. 注意display: none可以完全删除元素, opacity: 0只是将其隐藏,并且仍然可以与之交互。 For example, if it is positioned over a link you will not be able to click the link because of the invisible div. 例如,如果它位于链接上,则由于div不可见,您将无法单击该链接。

To overcome this, you could consider pointer-events: none (see browser compatiblity ) so that click events pass beneath it. 为了克服这个问题,您可以考虑使用pointer-events: none (请参阅浏览器兼容性 ),以便单击事件在其下方传递。 You would then change it back to pointer-events: auto when shown, so it can be clicked. 然后,您可以将其更改回pointer-events: auto显示时为pointer-events: auto ,因此可以单击它。

Basic example 基本例子

 body { height: 100vh; } div { height: 100px; width: 100px; background: #F00; transition: transform 1s; } body:hover div { transform: translateX(100px); } /*Will work*/ .willTransition { opacity: 0; } body:hover .willTransition { opacity: 1; } /*Doesn't work*/ .wontTransition { display: none; } body:hover .wontTransition { display: block; } 
 <h1> Hover anywhere here to show!</h1> <h2>Will transition with opacity</h2> <div class="willTransition"></div> <h2>Wont transition with display none to block</h2> <div class="wontTransition"></div> 

Try using 尝试使用

transition: all 3s ease;

Probably its not taking the transform property in particular. 可能它没有特别重视transform属性。

transition: all 3s ease 0s;

解决了!

I think the better idea is add class on popup. 我认为更好的主意是在弹出窗口中添加类。 It's easiest way 这是最简单的方法

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

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