jQuery动画中的伪元素

问题描述 投票:1回答:2

使用animate单击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>

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

javascript jquery html css
2个回答
4
投票

你可以使用overflow: initial !important;作为div

$('a').click(function(){
$('div').animate({

width: 50 + "%"
}, 2000);
});
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%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span></span>
</div>

<a>click</a>

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%");
});

$('a').click(function() {
  $("div").css("width", "50%");
});
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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

<a>click</a>
© www.soinside.com 2019 - 2024. All rights reserved.