简体   繁体   中英

Pseudo element in jQuery animate

After clicking div width going to 50% with animate, I appended a circle with pseudo, but when the animation is playing it is hiding. I also tried with a span inside div instead of pseudo elements, but no success. Any idea?

 $('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> 

I don't want hide the circle when the animation is playing.

You can use overflow: initial !important; for the 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> 

You could also use transition and let CSS do the animation.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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