开发者

jQuery Floating Div with Bottom Limit

开发者 https://www.devze.com 2023-02-03 06:39 出处:网络
Trying to duplicate the mashable effect with two menus. I got the scrolling effect working, but I was looking for the effect to stop at the top of the footer. I was thinking I could do a conditional s

Trying to duplicate the mashable effect with two menus. I got the scrolling effect working, but I was looking for the effect to stop at the top of the footer. I was thinking I could do a conditional statement with the limits, but I wasn't sure how to pull it off.

Here is the javascript I'm using.

var name = ".floater";
var menuYloc = null;

jQuery(document).ready(function($) { 
    menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")))
    jQuery(window).scroll(function () { 
        offset = menuYloc+jQuery(document).scrollTop()+"px";
        jQuery(name).animate({top:offset},{duration:500,queue:false});
    });
});

Here is the link to the build site. http://host开发者_运维知识库.philmadelphia2.com/~chill/about/

Thanks in advance.


use jquery's .offset to find the position of the footers location within the page, then use the menu's top position + its height to determine if its at or near the footers position and stop if it is.

#Footer, and $Menu are the id's of the footer and menu respectively change them obviously to what they are.

EDIT: previous example didnt work properly with small window heights this one should

jQuery(document).ready(function($) 
{ 
    menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")))
    jQuery(window).scroll(function () {
        var contentHeight = jQuery("#content").height();
        var menuHeight = jQuery(".floater").height();

        offset = menuYloc+jQuery(document).scrollTop();
        if( (offset+menuHeight) > (contentHeight - menuHeight) )
            offset =  (contentHeight - menuHeight);              

        jQuery(name).animate({top:offset+"px"},{duration:500,queue:false});
    }); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消