html - 禁用 CSS 过渡延迟

标签 html css css-transitions css-animations

我试图通过添加一个将延迟值设置为 0 秒的类来禁用转换延迟。

我现在不明白为什么它不起作用。

唯一对我有用的是添加 .no-animtransition: none; 但是根本没有动画。

我还想在单击添加类的按钮后保留动画,因此使用 transition: none; 的解决方案在我的情况下不够好...

有什么想法吗?

$('.button').click(function(){
	$this = $('.box');
	$this.addClass('no-anim');
  setTimeout(function() {
  	$this.removeClass('no-anim');
  }, 3000);
});
.box {
  width: 200px;
  height: 200px;
  background: #333;
  transition: width .3s ease-in-out;
  position: relative;  
}

.box:hover {
    width: 300px;
    transition-delay: 2.5s;
  }
  .box.no-anim {
    transition-delay: .3s;
  }

.button {
    display: inline-block;
    height: 50px;
    width: 30px;
    background: #ff3434;
    position: absolute;
    right: 0;
    top: 50%;
    margin-top: -25px;
    cursor: pointer;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<div class="box">
  <span class="button"></span>
</div>

最佳答案

选择器 .box.no-anim 与普通的 .box 选择器具有相同的优先级(它们都只是类)。这意味着 .box.no-anim 添加的 :hover 伪选择器赋予它更高的优先级,使其覆盖你的 no-anim 转换延迟。

也将 :hover 添加到您的 .no-anim 选择器,它将正常工作。

$('.button').click(function(){
	$this = $('.box');
	$this.addClass('no-anim');
  setTimeout(function() {
  	$this.removeClass('no-anim');
  }, 3000);
});
.box {
  width: 200px;
  height: 200px;
  background: #333;
  transition: width .3s ease-in-out;
  position: relative;  
}

.box:hover {
    width: 300px;
    transition-delay: 2.5s;
  }
  .box.no-anim:hover {
    transition-delay: .3s;
  }

.button {
    display: inline-block;
    height: 50px;
    width: 30px;
    background: #ff3434;
    position: absolute;
    right: 0;
    top: 50%;
    margin-top: -25px;
    cursor: pointer;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<div class="box">
  <span class="button"></span>
</div>

关于html - 禁用 CSS 过渡延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38385843/

相关文章:

html - 仅模糊输入内的文本,而不是整个 &lt;input&gt; 元素

javascript - 管理 LTE 子菜单不会在单击时保持打开状态

javascript - 动态注释掉 CSS 样式表?

javascript - 在 webkit 浏览器中通过 Javascript 使用 CSS 重新绘制减速

html - 所有版本的所有浏览器都可以识别这些样式吗?

使用drawImage和imageSmoothing禁用Javascript Canvas不完美像素

javascript - 自定义html5音频标签,如何添加音量按钮?

css - 是否有适当的方法以编程方式动态更新部分转换和转换?例如变换原点或持续时间

javascript - 自定义 jQuery 轮播过渡错误

html - 如何在类中使用 css 属性背景拉伸(stretch)或适合背景图像