This is because when your element reaches the top of the window, its distance from the top becomes equal $(this).scrollTop();
, so you have to include cases when they are equal, otherwise the other part if
will be true, so it fixedClass
will be deleted and then added again when scrolling, then everything will start again, hence flicker.
, >
, (>
), (>=
) if
:
if(scrollTop >= fence) {
$(o.tofixedClass).addClass(o.fixedClass).css({"margin-top":o.top,"margin-bottom":o.bottom});
} else {
$(o.tofixedClass).removeClass(o.fixedClass);
}
: http://jsfiddle.net/1wxggpv5/6/
: , , , , , , , .
, #box
.pagar
fence
on.scroll
:
jQuery ( ):
return this.each(function() {
var $window = $(window),
$this = $(this),
fence = $(o.tofixedClass).offset().top;
$window.on("scroll", function() {
var scrollTop = $(this).scrollTop();
if(scrollTop >= fence) {
$(o.tofixedClass).addClass(o.fixedClass).css({"margin-top":o.top,"margin-bottom":o.bottom});
} else {
$(o.tofixedClass).removeClass(o.fixedClass);
}
});
});
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "pagar">
If you scroll past me, then you'll *NOT* see such as flashing lights flickering, and we now know why :)
<div id = "box">
Oh hello! :D
</div>
</div>
<div class="scroll"></div>
: http://jsfiddle.net/1wxggpv5/19/