Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to make it so when you load a page a div is sticking to the bottom of it. Then when a user scrolls down it sticks to the top.

I can do the stick to the top part using a sticky element.

<div id="container">
<div id="menu">
? ??<ul>
? ? ? ??<li><a href='#test'>Line 1</a></li>
? ? ? ??<li><a href='#'>Line 2</a></li>
? ? ? ??<li><a href='#'>Line 3</a></li>
? ??</ul>
</div>
</div>

<script type="text/javascript">
? ??$(document).ready(function(){
? ? ? ??$(window).scroll(function(){
? ? ? ? ? ?if($(this).scrollTop()>=660)
? ? ? ? ? ?{
? ? ? ? ? ?$('#menu').addClass('fixed');
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?$('#menu').removeClass('fixed');
? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?});
? ??});
? ??</script>

I just can't do it so it sticks to the bottom on load. I've attached a little mockup if it's unclear.

Sticky

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

UPDATED.
Here is working jsFiddle to examine.

jQuery:

$(document).ready(function() {
    var windowH = $(window).height();
    var stickToBot = windowH - $('#menu').outerHeight(true);
    //outherHeight(true) will calculate with borders, paddings and margins.
    $('#menu').css({'top': stickToBot + 'px'});

    $(window).scroll(function() {
       var scrollVal = $(this).scrollTop();
        if ( scrollVal > stickToBot ) {
            $('#menu').css({'position':'fixed','top' :'0px'});
        } else {
            $('#menu').css({'position':'absolute','top': stickToBot +'px'});
        }
    });
});?

Note: if you want to go further, i suggest to inspect this answer too:

Setting CSS value limits of the window scrolling animation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...