javascript - 如何创建 `sticky` float 固定/滚动侧边栏?

标签 javascript jquery css scroll

我有一个网站,左侧有一个栏,应该与用户保持一致。 因此,当用户滚动时,侧边栏会一直滚动,直到它距页面顶部 5px。从那时起,它应该被锁在那里。

当然,视口(viewport)可能比左侧栏小,所以左侧栏不能完全适合屏幕。虽然不是很戏剧化。但是,如果用户滚动到底部,侧边栏的底部“点击”页面的页脚,然后再次与页面一起滚动,我会喜欢它。

这是我得到的代码:我的网站的基本设置和我问题第一部分的尝试(但你会发现它不起作用):jsfiddle .

我觉得问题的第一部分比较清楚,但第二部分可能有点难以理解,所以这是一个模型:when scrolled down 您可以看到没有显示任何文本,因为文本位于视口(viewport)上方。

这是我尝试第一部分的 js:

$(document).ready(function () {
    var theLoc = 5;
    var links = $('#left');
    $(window).scroll(function () {
        console.log('scroll');
        if (theLoc >= $(window).scrollTop()) {
            if (links.hasClass('fixed')) {
                links.removeClass('fixed');
            }
        } else {
            if (!links.hasClass('fixed')) {
                links.addClass('fixed');
            }
        }
    });
});

但可能更多的是 css 问题:

.fixed {
    position:fixed;
}

我尝试再次指定高度等(因为它看起来非常大),但没有任何进展。

最佳答案

我刚才做了这个,这是我为此创建的代码:View the JSFiddle .

(您可能需要稍微更改您的标记,我不确定这与您拥有的表格布局的效果如何,我建议使用 divs 并 float 它们来布局您的内容。),或者,您可以使用下面的代码/逻辑和 roll your own使用您自己的布局。

基本上,
- 获取我们的元素
- 获取侧边栏在页面加载时的位置
- 获取 #content.outerHeight()

一旦我们有了这些变量,在 window scroll 上测试我们是否是 <= (小于或等于)我们原来的sidebarTop position或检查我们是否通过了 blogHeight ,如果 2 中的任何一个为真,则删除粘性类,elseif我们的卷轴是>=我们的原创sidebar位置,然后添加 .sticky类(具有 position: fixed )。

Check the JSFiddle (Click here)


Javascript 是这样的:

// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#sidebar-nav');

// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;

// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#content').outerHeight() - 10;

// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);

// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll(){

    // Cache our scroll top position (our current scroll position)
    var windowScrollTop = $(window).scrollTop();

    // Add or remove our sticky class on these conditions
    if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop){
        // Remove when the scroll is greater than our #content.OuterHeight()
        // or when our sticky scroll is above the original position of the sidebar
        $sidebar.removeClass('sticky');
    }
    // Scroll is past the original position of sidebar
    else if (windowScrollTop >= sidebarTop){
        // Otherwise add the sticky if $sidebar doesnt have it already!
        if (!$sidebar.hasClass('sticky')){
            $sidebar.addClass('sticky');
        }
    }   
}

HTML:

<header>This is the header!</header>
<ul id="sidebar-nav" class="nav nav-list">
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
</ul>
<div id="content">Content in here, scroll down to see the sticky in action!</div>
<div class="clear"></div>
<div id="footer">This is the #footer</div>

CSS:

/* Sticky our navbar on window scroll */
#sidebar-nav.sticky {position:fixed;top:5px;}

/* Other styling for the html markup */
header {
    border:1px solid #aaa;
    background-color:yellow;
    margin-bottom:5px;
    height:50px;
}
#sidebar-nav {
    width:150px;
    border:1px solid #ddd;
    margin:0;
    padding:0;
    float:left;
}
#sidebar-nav li {
    list-style:none;
    border:1px solid #ddd;
    margin:10px;
    padding:2px;
}
#content {
    height:2000px;
    width:500px;
    padding:10px;
    border:1px solid #ddd;
    margin:0 0 10px 5px;
    float:right;
}
#footer {
    height:800px;
    border:1px solid green;
    background-color:#ddd;
}
.clear {
    clear:both;
}

:)

关于javascript - 如何创建 `sticky` float 固定/滚动侧边栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17323098/

相关文章:

javascript - 滚动时更改 Logo div 背景颜色

javascript - 使用 URL 访问正确的选项卡

javascript - 如何在每个循环中找到元素的类

javascript - 根据屏幕大小调整文本大小

jquery ui 单选按钮验证在 Firefox 中有效,但在 IE 中出错。 。

html - 如何制作没有背景图片的透明背景?

javascript - jquery 文本区域值计数长度

javascript - 当 javascript 失败时提醒 Javascript 错误?

javascript - TypeError : Object (. ..) 不是函数 react native 堆栈导航

javascript - 我如何从 Google Analytics 中提取数据并根据最流行的内容创建一个趋势话题部分