Answer the question
In order to leave comments, you need to log in
What book to read on algorithms in this case?
In the example, the code for the sticky block. I read somewhere that programming is algorithms + language features, or something similar to that. I basically know what these jquery methods do in this example. But first you need to understand what algorithm, and then apply the capabilities of the library. I understand it partially.
Here we get the height from the top of the sticky block
var stickyTop = $('.sticky').offset().top;
. Inside the scroll method, we store the scroll value in windowTop . I do not understand the condition, at least not completely.
stickyTop < windowTop the first part, if the indent of the sticky block from the top is less than the scroll itself, then we assign it ('position', 'fixed');
otherwise ('position', 'relative');
I still can’t fully understand it, the rest of the condition is not logically clear to me.
$(window).scroll(function() {
var windowTop = $(window).scrollTop();
if (stickyTop < windowTop && $(".blue").height() + $(".blue").offset().top - $(".sticky").height() > windowTop) {
$('.sticky').css('position', 'fixed');
} else {
$('.sticky').css('position', 'relative');
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question