jQuery - DIV to move with scrolling motion and stick position to top and bottom of window

后端 未结 2 1288
你的背包
你的背包 2020-12-15 11:02

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.

相关标签:
2条回答
  • 2020-12-15 11:23

    You don't need any jQuery or javascript for this. All of this can be achieved in CSS with position: fixed.

    Change your sidebar selector to the following

    #sidebar {
        width: 130px;
        height: auto;
        overflow: hidden;
        background: #ffffff;
        margin: 0 auto;
        clear: right;
        padding: 8px;
        background: #e5e5e5;
        border: 2px dashed red;
        position: fixed;
        right: 35px;
    }
    
    0 讨论(0)
  • 2020-12-15 11:45

    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.

    0 讨论(0)
提交回复
热议问题