I want to be able to emulate the effect that google has used on their search results screen whereby the results are shown on the left and the user can click a button on each result to show a preview of that result on the right.
The thing is as the user scrolls the page the right hand result preview panel is constantly anchored to the top and bottom of the browser window. Also when the window resizes the panel stays anchored and resizes too. Obviously this cannot be done with css alone as the page itself needs to be scrollable so the user can scroll the results whilst this preview panel needs to remain anchored to the page height so I assumed javascript/jquery was required.
I found somewhat of a solution in jquery format:-
"The trick is that you have to set it as position:fixed, but only after the user has scrolled past it.
This is done with something like this, attaching a handler to the window.scroll event
This simply adds a sticky CSS class when the page has scrolled past it, and removes the class when it's back up.Code:// Cache selectors outside callback for performance. var $window = $(window), $stickyEl = $('#the-sticky-div'); var elTop = $stickyEl.offset().top; $window.scroll(function() { var windowTop = $window.scrollTop(); $stickyEl.toggleClass('sticky', windowTop > elTop); });
And the CSS class looks like this
"Code:#the-sticky-div.sticky { position: fixed; top: 0; }
However I want to achieve this in javascript alone. Can anyone help me with this please?


Reply With Quote
Bookmarks