jQuery动画中的伪元素

时间:2016-12-09 10:38:29

标签: javascript jquery html css

使用动画点击div 宽度 50%后,我添加了一个带伪的圆圈,但是当动画播放时它隐藏了。我也尝试使用div而不是伪元素,但没有成功。有什么想法吗?



$('a').click(function() {
    $('div').animate({
    width: 50 + "%"
    }, 2000);
});

div {
  width: 0%;
  height: 2px;
  background: red;
  position: relative;
}

div span {
    content: "";
    position: absolute;
    right: 0;
    top: -2px;
    width: 10px;
    height: 10px;
    background: red;
    border-radius: 50%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <span></span>
</div>

<a>click</a>
&#13;
&#13;
&#13;

我不想在动画播放时隐藏圆圈。

2 个答案:

答案 0 :(得分:4)

您可以将overflow: initial !important;用于div

&#13;
&#13;
$('a').click(function(){
$('div').animate({

width: 50 + "%"
}, 2000);
});
&#13;
div {
  width: 0%;
  height: 2px;
  background: red;
  position: relative;
  overflow: initial !important;
}

div span {
    display:inline-block;
    content: "";
    position: absolute;
    right: 0;
    top: -2px;
    width: 10px;
    height: 10px;
    background: red;
    border-radius: 50%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span></span>
</div>

<a>click</a>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

你也可以使用transition让CSS做动画。

<强> HTML

<div></div>
<a>click</a>

<强> CSS

div {
  width: 0%;
  height: 2px;
  background: red;
  position: relative;
  transition: 2s;
}

div:after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 10px;
  background: red;
  border-radius: 50%;
  z-index: 1000;
}

<强>的jQuery

$('a').click(function() {
  $("div").css("width", "50%");
});

&#13;
&#13;
$('a').click(function() {
  $("div").css("width", "50%");
});
&#13;
div {
  width: 0%;
  height: 2px;
  background: red;
  position: relative;
   transition: 2s;
}

div:after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 10px;
  background: red;
  border-radius: 50%;
  z-index: 1000;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

<a>click</a>
&#13;
&#13;
&#13;