简体   繁体   English

滚动浮动/滑动侧栏问题

[英]Scrolling floating/sliding sidebar issue

I've looked through all stuff in here related to what I'm facing, but still can't get it fixed. 我已经浏览了与我所面临的内容相关的所有内容,但仍无法解决。

What I'm trying to do: 我正在尝试做的是:

  1. Get a sidebar navigation to float as the user scrolls up/down the page. 当用户向上/向下滚动页面时,获取侧边栏导航浮动。
  2. To have the sidebar centrally stopping so that all elements can be seen and clicked. 使边栏居中停止,以便可以看到和单击所有元素。

What I'm getting: 我得到的是:

  1. Sidebar follows scrolling just fine when scrolling down (centralised view), but when scrolling up, only half of the sidebar is displayed when the page is scrolled up too fast. 向下滚动(集中视图)时,边栏跟随滚动就很好,但是当向上滚动时,如果页面向上滚动得太快,则只显示一半的边栏。
  2. When scrolling down the page, the sidebar will push the footer further below without end. 向下滚动页面时,边栏将无尾地将页脚推向下方。
  3. The sidebar will not lock back into its original position when the page is scrolled all the way up from the bottom. 从底部一直向上滚动页面时,侧栏不会锁定回到其原始位置。 There seems to be a slight gap. 似乎有一点差距。

Demo Link 演示链接

Here is the script (modded from the generous Jordon Mears): 这是脚本(改编自慷慨的乔登·米尔斯):

<script type="text/javascript">
function animate_box() {  
var offset = -15; /* set this to the starting margin-top in the css */  
var element = document.getElementById('animate_box'); 

if(element) {  
    var top = Number(String(element.style.marginTop).substring(0,String(element.style.marginTop).indexOf('px')));

    try {  
        if(document.body.scrollTop > 500) {  
            var difference = (document.body.scrollTop + offset);
        } else if(document.documentElement.scrollTop > 0) {  
            var difference = (document.documentElement.scrollTop + offset);

        } else {  
            var difference = offset;  
        }  
    } catch(e) {  
        var difference = offset;  
    }  

    difference = difference - top;  

    if(difference > 200) {  
        element.style.marginTop = (top + Math.abs(Math.ceil(difference / 30))) + 'px';  
    } else if(difference < 190) {  
        element.style.marginTop = (top - Math.abs(Math.ceil(difference / 30))) + 'px';  
    }  
}  
}  
window.setInterval(animate_box, 50);
</script>

I suggest a different approach: 我建议使用另一种方法:

  • save the starting position of the element (.offset().top) 保存元素的起始位置(.offset()。top)
  • when scroll happens: 滚动发生时:
  • if the window scroll offset (.scrollTop()) is more than the starting position, then change the position of the sidebar to "fixed" with "top: 0" 如果窗口滚动偏移量(.scrollTop())大于起始位置,则将侧边栏的位置更改为“固定”,并使用“顶部:0”
  • if it goes under the starting position change it back to static (default position). 如果它位于起始位置以下,则将其更改回静态(默认位置)。

Something like this: 像这样:

$(function() {
    var backup_position_toolbar = $('#sidebar').offset().top;
    $(window).scroll(function() {
        if ( $('#sidebar').offset().top - $(window).scrollTop() &lt; 0) $('#sidebar').addClass('fixed');
        if ( $(window).scrollTop() &lt; backup_position_toolbar ) $('#sidebar').removeClass('fixed');
    });
});

Note that I use a "fixed" class defined as follows: .fixed { position: fixed; 请注意,我使用的“固定”类定义如下:.fixed {position:fixed; top: 0; 最高:0; } }

This makes the menu more usable. 这使菜单更加可用。 If you want to block the sidebar at a certain point you can add a little more logic (ie when bottom is too close). 如果要在某个点上阻止侧栏,则可以添加更多的逻辑(即,当底部太近时)。 In this way you don't need to set numeric values (500, 200 etc). 这样,您无需设置数值(500、200等)。

HOWEVER if you want something more with no effort try bootstrap with "affix" (look at the left menu, is what you want) http://twitter.github.com/bootstrap/javascript.html#affix 但是,如果您不费吹灰之力就尝试使用带“缀”的引导程序(请看左侧菜单,这就是您想要的) http://twitter.github.com/bootstrap/javascript.html#affix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM