如何停止页脚浮动(滑动)div

发布于 2024-08-11 03:33:29 字数 1870 浏览 4 评论 0原文

如何才能有一个滑动菜单 div,除非页面向下滚动超过某个点,否则它不会移动

我使用此链接中的代码作为浮动菜单。它具有如何在页眉处停止停止浮动,但不在页脚处停止浮动。如何修改此代码以停止在页脚处?

    //// CONFIGURATION VARIABLES:

var name    = "#sidebar";
var menu_top_limit   = 0;
var menu_top_margin  = 0;
var menu_shift_duration = 500;
var menuYloc = null;
///////////////////////////////////

$(window).scroll(function() 
{ 
 // Calculate the top offset, adding a limit
 offset = menuYloc + $(document).scrollTop() + menu_top_margin;

 // Limit the offset to 241 pixels...
 // This keeps the menu out of our header area:
 if(offset < menu_top_limit)
  offset = menu_top_limit;

 // Give it the PX for pixels:
 offset += "px";

 // Animate:
 $(name).animate({top:offset},{duration:menu_shift_duration,queue:false});
 });

我有另一个类似的代码应该停在页脚,但它不起作用:

var name = "#sidebar";  
var menuYloc = null;  
var footer = '#footer'; //Specify the ID for your footer.

 $(document).ready(
    function()
    {  
        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))  
        $(window).scroll(
            function() 
            {   
                var offset = menuYloc + $(document).scrollTop();
                var anotherOffset = offset;

                var docTop = $(window).scrollTop();
                var footerTop = $(footer).offset().top;

                var maxOffset = footerTop - $(name).height() - 20;
                var minOffset = docTop;

                offset = offset > maxOffset ? maxOffset : offset;
                offset = offset < minOffset ? minOffset : offset;

                $(name).animate({top:offset + 'px'},{duration:500,queue:false});      
            }
        );  
    }
);

How can I have a sliding menu div that doesn't move unless the page is scrolled down past a certain point

I used the code from this link for a floating menu. it has how to stop the stop float at the header, but not at the footer. How can I modify this code to stop at the footer?

    //// CONFIGURATION VARIABLES:

var name    = "#sidebar";
var menu_top_limit   = 0;
var menu_top_margin  = 0;
var menu_shift_duration = 500;
var menuYloc = null;
///////////////////////////////////

$(window).scroll(function() 
{ 
 // Calculate the top offset, adding a limit
 offset = menuYloc + $(document).scrollTop() + menu_top_margin;

 // Limit the offset to 241 pixels...
 // This keeps the menu out of our header area:
 if(offset < menu_top_limit)
  offset = menu_top_limit;

 // Give it the PX for pixels:
 offset += "px";

 // Animate:
 $(name).animate({top:offset},{duration:menu_shift_duration,queue:false});
 });

I have another similar code that is supposed to stop at the footer, but it is not working:

var name = "#sidebar";  
var menuYloc = null;  
var footer = '#footer'; //Specify the ID for your footer.

 $(document).ready(
    function()
    {  
        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))  
        $(window).scroll(
            function() 
            {   
                var offset = menuYloc + $(document).scrollTop();
                var anotherOffset = offset;

                var docTop = $(window).scrollTop();
                var footerTop = $(footer).offset().top;

                var maxOffset = footerTop - $(name).height() - 20;
                var minOffset = docTop;

                offset = offset > maxOffset ? maxOffset : offset;
                offset = offset < minOffset ? minOffset : offset;

                $(name).animate({top:offset + 'px'},{duration:500,queue:false});      
            }
        );  
    }
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-08-18 03:33:29

如果您正在寻找如何将页脚保持在底部和滚动中间,我想我已经完成了,请参阅下面我的博客文章(在“网页上”部分下)转到 http://www.jimleo.com 查看实际效果。

http://jimleonardo.blogspot。 com/2009/02/jimleocom-is-back-up-some-how-to.html

If you are looking for how to keep the footer on the bottom and a scrolling middle, I think I've got that done see my blog post below(under the section "On to the WebPage") Go to http://www.jimleo.com to see it in action.

http://jimleonardo.blogspot.com/2009/02/jimleocom-is-back-up-some-how-to.html

回忆躺在深渊里 2024-08-18 03:33:29

听起来您想要一个始终可见的页脚。创建两个绝对元素(一个位于页面顶部,一个位于底部)不是容易得多吗?然后将所有内容添加到宽度/高度为 100% 的 div 中(必须使用 javascript 执行此操作)。

然后你就不必将东西与主体的滚动事件挂钩(这看起来不太好)。

如果您在内容之前和之后添加偏移量或一些换行符,它应该比您尝试的效果好得多。

我已经通过我自己的页面完成了此操作。

It sounds like you want a page footer that is always visible. Wouldn't it be a lot easier to create two absolute elements, one at the top of the page, and one at the bottom. And then add all of your content to a div which has 100% width/height (have to do this with javascript).

Then you don't have to hook stuff up to the body's scroll event (this never looks very good).

If you add an offset, or a few line breaks before and after your content it should work a lot better than what your attempting.

I've done it with my own page here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文