I am adding a class to a div after a user scrolls. This works fine, but for some reason it won't remove this class when the user scrolls back again. There are no errors in the console. Where am I going wrong?
var scrolled = $('body').offset().top - 800;
$(window).on('resize scroll', function() {
if ($(window).scrollTop() > scrolled) {
$('#one').addClass('in');
} else {
$('#one').removeClass('in');
}
});
section.bg-red {
background: red;
}
section.bg-blue {
background: blue;
}
section {
min-height: 100vh;
}
section p {
color: red;
text-align: center;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
section.in p {
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="bg-red" id="one">
<p>Well done you scrolled</p>
</section>
<section class="bg-blue">
ddd
</section>
console.log(variable)is a simple statement you should be aware of which allows you to do basic debugging to solve problems yourself. If you are unaware of this method, I get to tell you about it. If you already know about it, then it should be simple for you to use it to help yourself figure out the issue. People who answer questions on S.O. are not all just about providing answers, but helping you find your own answers in the future.