hi folks!
i have been coding on this vertical sliding navigation quite a while. its tested on all relevant browsers and works like a dream.
id loved to show you an example of it, but the site is under development, and i, as a little coder, am not allowed to grant access to it, additionally i do not have enough time to create a testcase for everybody to see.
to give you a short insight:
the site is quite long (vertically) and we wanted a small navigation (very subtle) to follow the user when scrolling. the problem was: the footer is quite long too, and we didnt want the sliding navigation to reach into it, when scrolling down the footer. in other words: i had to stop the animation when the sliding nav reached the end of the main content.
so far so good, i had NO idea how to tell the animation to stop at certain conditions, other than telling it that from the start.
my solution is totally workaround and completely selfmade. but i am SURE that there is some kind of "stop animation when condition is met"-command i was totally overlooking. so please dear jquery community: tell me what i am doing wrong, and how i could improve this function.
here is the code:
- function slide_nav() {
- var scrollingDiv = jQuery("div#slide_nav");
- if(scrollingDiv.length > 0)
- {
- // check for main div lower border to set the navigation so, that it
- // doesnt exceeds this value when scrolling
-
- // save the main divs lower border position from top
- var main_div = jQuery('div#main');
- var main_div_position = main_div.position();
- var main_div_lower_reach = main_div_position.top + main_div.height();
-
- var scrollingDiv_offset = scrollingDiv.offset();
- var initial_offset = scrollingDiv_offset.top;
-
- // Create small helper dog to look up position when finished animating
- var wrapper = jQuery('div#wrapper');
- wrapper.before('<div id="watchdog"></div>');
-
- var watchdog = jQuery('div#watchdog');
-
- watchdog.css({
- 'background': 'transparent',
- 'height': scrollingDiv.height(),
- 'width': scrollingDiv.width(),
- 'position': 'absolute',
- 'top': scrollingDiv_offset.top,
- 'left': scrollingDiv_offset.left
- });
-
- jQuery(window).scroll(function() {
- setTimeout(function() {
- scroll_this_dog(scrollingDiv)
- }, 500);
- });
-
- function scroll_this_dog(element)
- {
- watchdog.css({
- "marginTop" : (jQuery(window).scrollTop() + 1) + "px"
- });
-
- var watchdog_offset = watchdog.offset();
- var watchdog_lowerend = watchdog_offset.top+watchdog.height();
-
- //console.log("BARK! thats my lower end: "+watchdog_lowerend);
- //console.log("maindiv lower end: "+main_div_lower_reach);
-
-
- if((watchdog.height()+watchdog_offset.top) > main_div_lower_reach)
- {
- //console.log("BARK! im lower!");
- element.stop().animate({
- "marginTop" : main_div_lower_reach - element.height() - initial_offset + "px"
- }, 1000, "easeOutExpo");
- }
- else
- {
- //console.log("BARK! im above!");
- element.stop().animate({
- "marginTop" : (jQuery(window).scrollTop() + 1) + "px"
- }, 1000, "easeOutExpo");
- }
- }
- }
- }
thanks to everyone willing to have a look into this, its really appreciated!
thanks and gbye