I'm having trouble setting 2 positions on a scroll function using offset.
I have created a fiddle so you can see... http://jsfiddle.net/motocomdigital/SGCHt/12/
When you open this fiddle, reduce the fiddle preview viewport down to the similar size of the screenshot below.
MY PROBLEM
You can see I'm using conditional statements to control the positioning of the blue tabs, depending on what scroll point they are at.
The yellow columns represent the tab containers, and I'm tyring to use a if else
statement to control the bottom positioning so the blue tabs never go outside the yellow containers.
But I can't get this to work. My bottom positon offset does not work.
var $tab = $('.tab-button');
$(window).bind("resize.browsersize", function() {
var windowHalf = $(window).height() / 2,
content = $("#content"),
pos = content.offset();
$(window).scroll(function(){
if ($(window).scrollTop() >= pos.top + windowHalf ){
$tab.removeAttr('style').css({ position:'fixed', top:'50%'});
} else if ($(window).scrollTop() >= pos.bottom + windowHalf ){
$tab.removeAttr('style').css({ position:'absolute', bottom:'0px'});
} else {
$tab.removeAttr('style').css({ top: $(window).height() + 'px' });
}
});
}).trigger("resize.browsersize");
Can anyone please help me understand where I'm going wrong.
Thanks Josh
If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.
The jQuery offset() function is a built-in function in jQuery, by which we can return the offset of the bottom coordinate. The jQuery offset() function only specifies the top and left properties position, but with the help of top property and outerHeight() function, we can get the bottom position for elements.
You find the height of the scrolling container, then compare that to the scroll position. If they are the same, then you have reached the bottom.
jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.
So it appears you must calculate the bottom offset of an element manually. I tried to find something to make your life easier in the jQuery documentation but turned up with nothing. Supposedly offset only supports top/left
http://api.jquery.com/offset/
If we find the height of the wrapper, subtract the height of the tabs, we can figure out exactly where to limit the tab scroll.
Also I included a jQuery css definition of the tab top. Without defining it onload, there was a weird document height change before the first scroll.
http://jsfiddle.net/SGCHt/16/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With