I have added a horizonal line after a section using CSS:
.section:after {
content: "";
position: absolute;
bottom: -20px;
left: 0;
width: 0;
height: 2px;
background-color: #000;
animation: line-appear 1.5s forwards;
visibility: hidden;
}
@keyframes line-appear {
to {
width: 100%;
visibility: visible;
}
}
I also added javascript into the html:
$(document).ready(function() {
$(window).scroll(function() {
$(".section").each(function() {
var position = $(this).offset().top;
var scrollPosition = $(window).scrollTop() + $(window).height();
if (scrollPosition > position) {
$(this).find(".section:after").css("animation-play-state", "running");
}
});
});
});
However, the line animated before I scrolled to it.
Does anyone know how to make the line starts animated just when I scroll to it?
Does anyone know how to make the line starts animated just when I scroll to it?
2
Answers
animation-play-state
already has the valuerunning
, before your JavaScript code even sets it. (Because that is its initial value, and by using theanimation
shorthand property in your CSS, you set all the "sub-properties" that you don’t explicitly specify, to their initial default.)You need to specify it as
paused
in the initial state:Try this:
css:
JavaScript:
Let me know if this can be helpful to you