动画添加和删除块中的类

时间:2017-10-12 11:27:40

标签: javascript jquery css animation

我想要动画添加一个类及其删除。

Google在

中找到了有关转换的信息,但由于某些原因它无效。

在块上单击按钮时,会添加具有css样式的类。在样式中,如果属性:afterter,它必须与动画一起出现和消失。 怎么还能实现呢?



$('button.add').click(function() {
  $('div.required').addClass('required-empty');
  setTimeout(function () {
    $('div.required').removeClass('required-empty');
  }, 5000);
});

.required-empty {
  position: relative;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required-empty:after {
  position: absolute;
  width: 11px;
  height: 11px;
  line-height: 12px;
  font-size: 30px;
  text-align: center;
  right: -18px;
  top: 50%;
  color: #fa6464;
  content: "s";
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required {
  position: relative;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
  width: 100px;
  height: 50px;
  background: #333;
  margin-left: 100px;
}

.required:before {
  position: absolute;
  width: 11px;
  height: 11px;
  line-height: 12px;
  font-size: 20px;
  text-align: center;
  left: -18px;
  top: 50%;
  color: #e0e0e3;
  content: "R";
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required.required-red:before {
  color: #fa6464;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="required required-red">

</div>
<button class="add">addClass</button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$('button.add').click(function() {
  $('div.required').addClass('required-empty');
  setTimeout(function () {
    $('div.required').removeClass('required-empty');
  }, 5000);
});
&#13;
.required {
  position: relative;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
  width: 100px;
  height: 50px;
  background: #333;
  margin-left: 100px;
}

.required:before {
  position: absolute;
  width: 11px;
  height: 11px;
  line-height: 12px;
  font-size: 20px;
  text-align: center;
  left: -18px;
  top: 50%;
  color: #e0e0e3;
  content: "R";
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required.required-red:before {
  color: #fa6464;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required-empty {
  position: relative;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}

.required-empty:before {
  position: absolute;
  width: 11px;
  height: 11px;
  line-height: 12px;
  font-size: 30px;
  text-align: center;
  right: -18px;
  top: 50%;
  color: #fa6464;
  content: "s";
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}
&#13;
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="required required-red">

</div>
<button class="add">addClass</button>
&#13;
&#13;
&#13;

课程订单,您的default州重叠empty班级