4
I have a div
that I would like you to hold to the top when the div has to touch the top. I tried to do this, resulted but the div
continuous blinking. What’s wrong?
SCRIPT
$(window).on('scroll', function() {
var ell = $('.menu').offset().top;
var ill = $(document).scrollTop();
var screen = $(window).height();
var distance = ell - ill;
if(ell < ill ) {
$('.menu').css("position", "fixed");
$('.menu').css("top", "0");
}else{
$('.menu').css("position", "relative");
$('.menu').css("top", "initial");
}
});
UPDATE
I discovered the problem since the div
stays fixed
, will be the same distance from the top as the scroll, so it will always be stuck to the top. There is how to save the old position?
Thank you for your reply! Perfect and functional example.
– I_like_trains
@I_like_trains Thanks my dear! Success!
– Sam