我正在寻找一个直接浮动在我的内容列旁边的粘性侧边栏。它将用于保存页面上的跳转链接,类似于此页面,但导航按钮将直接位于内容列旁边。
需要注意的是,这个侧边栏的 HTML 将通过 javascript 添加(这是一个更大的 JS 插件的一部分),所以我不能依赖在内容旁边浮动侧边栏。
如果可能,我只想使用 CSS。如果没有,JS的数量越少越好。
这是我到目前为止所得到的:
HTML
<!-- Main Section -->
<section class="main green">
<div class="inner">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, nesciunt, dolores eveniet quia commodi velit alias consequatur asperiores possimus ipsum quasi dicta sit maxime. Voluptatum provident a voluptate sequi impedit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum, temporibus, molestiae id non quidem vitae at placeat consequuntur in aspernatur incidunt officia facere ad odit harum itaque aperiam fugiat quaerat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid, obcaecati, maiores, harum porro aut quidem ullam voluptatibus molestias perspiciatis fugit voluptas adipisci repudiandae accusamus cum deserunt? Quas, praesentium ipsam neque!</p>
</div>
</section>
<!-- /Main Section -->
<!-- Main Section -->
<section class="main">
<div class="inner">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, nesciunt, dolores eveniet quia commodi velit alias consequatur asperiores possimus ipsum quasi dicta sit maxime. Voluptatum provident a voluptate sequi impedit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum, temporibus, molestiae id non quidem vitae at placeat consequuntur in aspernatur incidunt officia facere ad odit harum itaque aperiam fugiat quaerat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid, obcaecati, maiores, harum porro aut quidem ullam voluptatibus molestias perspiciatis fugit voluptas adipisci repudiandae accusamus cum deserunt? Quas, praesentium ipsam neque!</p>
</div>
</section>
<!-- /Main Section -->
<!-- Main Section -->
<section class="main green">
<div class="inner">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, nesciunt, dolores eveniet quia commodi velit alias consequatur asperiores possimus ipsum quasi dicta sit maxime. Voluptatum provident a voluptate sequi impedit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum, temporibus, molestiae id non quidem vitae at placeat consequuntur in aspernatur incidunt officia facere ad odit harum itaque aperiam fugiat quaerat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid, obcaecati, maiores, harum porro aut quidem ullam voluptatibus molestias perspiciatis fugit voluptas adipisci repudiandae accusamus cum deserunt? Quas, praesentium ipsam neque!</p>
</div>
</section>
<!-- /Main Section -->
<!-- Main Section -->
<section class="main">
<div class="inner">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, nesciunt, dolores eveniet quia commodi velit alias consequatur asperiores possimus ipsum quasi dicta sit maxime. Voluptatum provident a voluptate sequi impedit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum, temporibus, molestiae id non quidem vitae at placeat consequuntur in aspernatur incidunt officia facere ad odit harum itaque aperiam fugiat quaerat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid, obcaecati, maiores, harum porro aut quidem ullam voluptatibus molestias perspiciatis fugit voluptas adipisci repudiandae accusamus cum deserunt? Quas, praesentium ipsam neque!</p>
</div>
</section>
<!-- /Main Section -->
CSS
.main{ background:gray; }
.green{ background: green; }
.inner{
width:300px;
margin:0 auto;
padding: 10px;
}
.pageNavigation{
position: fixed;
width: 25px;
top: 50%;
right: 5%;
}
.pageNavigation li{
list-style:none;
margin: 5px 0;
background: #ddd;
}
.pageNavigation a{
display: block;
padding: 5px;
text-align: center;
}
JS
(function sidebarNav(){
sidebarHTML = '<nav class="pageNavigation">';
sidebarHTML += '<ul>';
sidebarHTML += '<li><a href="#">1</a></li>';
sidebarHTML += '<li><a href="#">2</a></li>';
sidebarHTML += '<li><a href="#">3</a></li>';
sidebarHTML += '</ul>';
sidebarHTML += '</nav>';
$('body').append(sidebarHTML);
})();
JS小提琴
任何想法将不胜感激!