I'm making a div appear with input animation when the scroll reaches a point on the page, the problem is that I can not do the output animation when the scroll returns.
The <div class="box"></div>
has the following CSS:
.box {
right: -384px;
visibility: hidden;
-webkit-transition: right 500ms cubic-bezier(0.265, 0.365, 0.26, 0.865);
-moz-transition: right 500ms cubic-bezier(0.265, 0.365, 0.26, 0.865);
-o-transition: right 500ms cubic-bezier(0.265, 0.365, 0.26, 0.865);
transition: right 500ms cubic-bezier(0.265, 0.365, 0.26, 0.865);
}
And when the scroll reaches a certain point, I add the class display-block
that executes the animation:
.display-block {
right: 0px;
visibility: visible;
}
This is jQuery:
$(window).scroll(function (e) {
var height = $(window).scrollTop();
var available = $(document).height();
var percentage_of_page = 0.5;
var half_screen = available * percentage_of_page;
if ( height > half_screen ) {
$('.box').addClass('display-block');
} else {
$('.box').removeClass('display-block');
}
});
I tried to create an extra class to hide the element with a keyframes animation , but did not work very well. I know it's possible to use a library like Waypoints to do this easily, but how to do with pure jQuery and CSS?
I did a JSFiddle pretty basic, because maybe Scroll is not even important. And a more complete JSFiddle where you can see the effect.