jQuery - DIV 以滚动方式移动并将位置固定到窗口的顶部和底部 [英] jQuery - DIV to move with scrolling motion and stick position to top and bottom of window

查看:15
本文介绍了jQuery - DIV 以滚动方式移动并将位置固定到窗口的顶部和底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能已经有一个 jQuery 插件可以实现这一点,但我找不到一个完全符合我要求的插件.如果有,请指点我的教程,谢谢.

我的问题是我的页面内容很长,当您滚动到页面底部附近时,我的侧边栏不可见.

所以我想让我的 #sidebar div 在您上下滚动页面时粘在浏览器窗口的顶部和底部.

我的侧边栏高度比你的典型屏幕分辨率长,所以我需要侧边栏的底部粘在浏览器窗口的底部以及浏览器的顶部.

所以当你开始向下滚动时,侧边栏会像往常一样滚动,但是当你到达侧边栏的末端时,它会粘住并且不会滚动,当你开始向上滚动时,侧边栏会跟随到顶部侧边栏到达浏览器,然后它坚持.反之亦然.

这可能吗?

我创建了一个 jsfiddle 的简单设计布局,这是中心.我在侧边栏添加了一个虚线边框,这样你现在侧边栏应该粘在哪里.

http://jsfiddle.net/motocomdigital/7ey9g/5/

任何建议,或者您知道在线教程或演示,都很棒!


更新

请参阅@Darek Rossman 的此尝试

http://jsfiddle.net/dKDJz/4/

他已经实现了基本的想法.但是向上滚动,导致它捕捉到顶部.我需要侧边栏随着向上/向下滚动运动而流畅.但坚持在窗口的顶部或底部.当页眉/页脚在视口中时,它也不应该被固定定位,因此它不会覆盖.

谢谢

解决方案

我已经用我的解决方案更新了 jsfiddle.

var $sidebar = $("#sidebar"),$window = $(window),sidebartop = $("#sidebar").position().top;$window.scroll(function() {如果 ($window.height() > $sidebar.height()) {$sidebar.removeClass('fixedBtm');if($sidebar.offset().top <= $window.scrollTop() && sidebartop <= $window.scrollTop()) {$sidebar.addClass('fixedTop');} 别的 {$sidebar.removeClass('fixedTop');}} 别的 {$sidebar.removeClass('fixedTop');if ($window.height() + $window.scrollTop() > $sidebar.offset().top + $sidebar.height()+20) {$sidebar.addClass('fixedBtm');}如果 ($sidebar.offset().top <0) {$sidebar.removeClass('fixedBtm');}}});

h1, h2 {显示:块;字体粗细:粗体;}#地平线{宽度:100%;高度:100%;最小高度:100%;背景:#cccccc;溢出:隐藏;}#页眉页脚 {宽度:480px;高度:自动;溢出:隐藏;背景:青色;填充:10px;颜色:#ffffff;}#包装{宽度:500px;高度:自动;溢出:隐藏;背景:#ffffff;边距:0 自动;}#content-wrapper {宽度:100%;高度:自动;溢出:隐藏:}#内容 {宽度:330px;高度:自动;溢出:隐藏;背景:#ffffff;边距:0 自动;向左飘浮;填充:10px;}#侧边栏{宽度:130px;高度:自动;溢出:隐藏;背景:#ffffff;边距:0 自动;向左飘浮;清楚:对;填充:8px;背景:#e5e5e5;边框:2px 红色虚线;}.fixedBtm {margin-left: 350px !important;位置:固定;底部:0;}.fixedTop {margin-left: 350px !important;位置:固定;顶部:0;}.邮政 {边距:5px;宽度:320px;背景:红色;浮动:无;溢出:隐藏;最小高度:175px}.buttons li {边距:5px;宽度:120px;背景:蓝色;浮动:无;溢出:隐藏;最小高度:20px;文本对齐:居中;颜色:#ffffff;光标:指针;}.buttons li:hover {背景:浅蓝色;}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="地平线"><div id="包装器"><div id="header">header</div><div id="内容包装器"><div id="内容"><h1>最新帖子</h1><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div><div class="post">这是一个帖子</div>

<div id="侧边栏"><h2>侧边栏</h2><ul class="buttons"><li>按钮 1</li><li>按钮 2</li><li>按钮 3</li><li>按钮 4</li><li>按钮 5</li><li>按钮 6</li><li>按钮 7</li><li>按钮 8</li><li>按钮 9</li><li>按钮 10</li><li>按钮 11</li><li>按钮 12</li><li>按钮 13</li><li>按钮 14</li><li>按钮 15</li><li>按钮 16</li><li>按钮 17</li><li>按钮 18</li>

<div id="footer">footer</div>

当窗口大于侧边栏时,侧边栏应保持在顶部,当侧边栏较大时,侧边栏应固定在底部.

There may be already a jQuery plugin which can achieve this, but I can't find one to do exactly what I'm after. If there is, just point me to the tutorial, thanks.

My problem I have is that I have very long page content, and my sidebar is not visible when you are scrolled near the bottom of the page.

So I would like to make my #sidebar div to stick to the top and bottom of my browser window as you scroll up and down the page.

My sidebar height is longer than your typical screen resolution, so I need the bottom of sidebar to sticky to the bottom of the browser window as well as the top of the browser.

So as you begin to scroll down, the side bar will scroll like normal, but when you reach the end the sidebar, it sticks and will not scroll, and as you begin to scroll up, the sidebar will follow until the top of sidebar reaches the browser, then it sticks. Vice Versa.

Is this possible?

I have created a jsfiddle of simple design layout which is central. I have added a dotted border to the sidebar so you now where the sidebar should stick.

http://jsfiddle.net/motocomdigital/7ey9g/5/

Any advice, or you know a online tutorial or demo, would most awesome!


UPDATE

Please see this attempt by @Darek Rossman

http://jsfiddle.net/dKDJz/4/

He's got the basic idea working. But the scrolling up, causes it to snap to the top. I need the sidebar to be fluid with the scrolling up/down motion. But sticking to the either the top or bottom of the window. It should also not be fixed positioned when the header/footer are in viewport, so it does not overlay.

Thanks

解决方案

I have updated the jsfiddle with my solution.

var $sidebar = $("#sidebar"),
    $window = $(window),
    sidebartop = $("#sidebar").position().top;

$window.scroll(function() {

    if ($window.height() > $sidebar.height()) {
        $sidebar.removeClass('fixedBtm');
        if($sidebar.offset().top <= $window.scrollTop() && sidebartop <= $window.scrollTop()) {
            $sidebar.addClass('fixedTop');        
        } else {
            $sidebar.removeClass('fixedTop');
        }
    } else {
        $sidebar.removeClass('fixedTop');
        if ($window.height() + $window.scrollTop() > $sidebar.offset().top + $sidebar.height()+20) {
            $sidebar.addClass('fixedBtm');    
        }
                   
        if ($sidebar.offset().top < 0) {
            $sidebar.removeClass('fixedBtm');   
        }
    }
    
});

h1, h2 {
    display: block;
    font-weight: bold;
}

#horizon {
    width: 100%;
    height: 100%;
    min-height: 100%;
    background: #cccccc;
    overflow: hidden;    
}

#header, #footer {
    width: 480px;
    height: auto;
    overflow: hidden;
    background: teal;
    padding: 10px;
    color: #ffffff;
}

#wrapper {
    width: 500px;
    height: auto;
    overflow: hidden;
    background: #ffffff;
    margin: 0 auto;
}

#content-wrapper {
    width: 100%;
    height: auto;
    overflow: hidden:
}

#content {
    width: 330px;
    height: auto;
    overflow: hidden;
    background: #ffffff;
    margin: 0 auto;
    float: left;
    padding: 10px;
}

#sidebar {
    width: 130px;
    height: auto;
    overflow: hidden;
    background: #ffffff;
    margin: 0 auto;
    float: left;
    clear: right;
    padding: 8px;
    background: #e5e5e5;
    border: 2px dashed red;
}

.fixedBtm {
    margin-left: 350px !important;
    position: fixed;
    bottom: 0;   
}

.fixedTop {
    margin-left: 350px !important;
    position: fixed;
    top: 0;   
}

.post {
    margin: 5px;
    width: 320px;
    background: red;
    float: none;
    overflow: hidden;
    min-height: 175px
}

.buttons li {
    margin: 5px;
    width: 120px;
    background: blue;
    float: none;
    overflow: hidden;
    min-height: 20px;
    text-align: center;
    color: #ffffff;
    cursor: pointer;
}

.buttons li:hover {
    background: lightblue;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="horizon">

    <div id="wrapper">
        
        <div id="header">header</div>
        
        <div id="content-wrapper">
        
            <div id="content">
            
                <h1>Latest Posts</h1>
            
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
                <div class="post">This is a post</div>
            
            </div>
        
            <div id="sidebar">
            
                <h2>Sidebar</h2>
            
                <ul class="buttons">
                    <li>Button 1</li>
                    <li>Button 2</li>
                    <li>Button 3</li>
                    <li>Button 4</li>
                    <li>Button 5</li>
                    <li>Button 6</li>
                    <li>Button 7</li>
                    <li>Button 8</li>
                    <li>Button 9</li>
                
                    <li>Button 10</li>
                    <li>Button 11</li>
                    <li>Button 12</li>
                    <li>Button 13</li>
                    <li>Button 14</li>
                    <li>Button 15</li>
                    <li>Button 16</li>
                    <li>Button 17</li>
                    <li>Button 18</li>
                </ul>
        
            </div>
                
        </div>
        
        <div id="footer">footer</div>
        
    </div>
    
</div>

The sidebar should remain in place at the top when the window is larger than the sidebar and fix to the bottom when the sidebar is larger.

这篇关于jQuery - DIV 以滚动方式移动并将位置固定到窗口的顶部和底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆