Sidebar position “semi” fixed css

我与影子孤独终老i 提交于 2019-12-21 18:30:03

问题


How do i get a fixed sidebar like the one containing the social buttons on this site:

http://www.tripwiremagazine.com/2012/11/social-media-buttons-and-icon-sets.html

I want my sidebar to be fixed to the top of my screen when i scroll down but on the top of the page there must be an absolute position it so that it stops following the browser as i scrool.

Currently I am just using:

#sidebar { position:fixed; }

But this does not give it an absolute position when reaching the top of the page.

Thank you


回答1:


html

<div class="wrapper"><div class="abs" id="sidebar"></div></div>

CSS

.abs { position: absolute; }

.fixed {
position: fixed;
top: 30px !important;}

#sidebar {
top: 150px;
left: 0;
height: 100px;
width: 20px;
background-color: #ccc;}

.wrapper {
height: 1500px;
padding: 20px;}

jQuery

 $(document).scroll(function() {
    var scrollPosition = $(document).scrollTop();
    var scrollReference = 10;
    if (scrollPosition >= scrollReference) {      
        $("#sidebar").addClass('fixed');   
    } else {
        $("#sidebar").removeClass('fixed');
        $("#sidebar").addClass('abs');
    };
});

DEMO of this code:

http://jsfiddle.net/B3jZ5/6/

<div class="wrapper">

is the example of content.




回答2:


You can also try this plugin:

https://code.google.com/p/sticky-panel/



来源:https://stackoverflow.com/questions/16089202/sidebar-position-semi-fixed-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!