admin管理员组文章数量:1304149
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.
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.
Share Improve this question edited Nov 7, 2014 at 20:49 Barlas Apaydin 7,31511 gold badges58 silver badges88 bronze badges asked Aug 16, 2012 at 8:28 MichaelMichael 5252 gold badges13 silver badges25 bronze badges1 Answer
Reset to default 9UPDATED.
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
本文标签: javascriptHow to build simple sticky navigation at the page bottumStack Overflow
版权声明:本文标题:javascript - How to build simple sticky navigation at the page bottum? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741754172a2396015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论