如何将粘性菜单包含在浮动侧边栏中
How to keep a sticky menu contained inside a floating sidebar?
我制作了这个jsfiddle:https://jsfiddle.net/0yd516er/这告诉大部分问题。
如果菜单位于浮动块内,如何阻止粘性菜单离开包装器?为此,我尝试了各种js插件,但我想这些插件只适用于不浮动的相对div。包装器使用overflow包装浮动div:hidden;
项目的高度未知。
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<body>
<div class="header"></div>
<div class="wrap">
<div class="content">
<div class="item">Height of these elements isn't known</div>
<div class="item">Test test</div>
<div class="item">Test test</div>
<div class="item">Test test</div>
</div>
<div class="sidebar">
<div class="menu">
<div class="menu-top">Top of the menu should stay here</div>
<div class="menu-bottom">This part of the menu needs to be sticky, but stop at the end of .wrap</div>
</div>
</div>
</div>
<div class="footer"></div>
</body>
JS:
$(document).ready(function(){
// to set the width right even when position is fixed
var divWidth = $('.sidebar').width();
$('.menu-bottom').css('width', divWidth+'px');
var fixmeTop2 = $('.menu-bottom').offset().top;
$(window).scroll(function() {
var currentScroll2 = $(window).scrollTop();
if (currentScroll2 >= fixmeTop2) {
$('.menu-bottom').css({
position: 'fixed',
top: '20px'
});
} else {
$('.menu-bottom').css({
position: 'relative'
});
}
});
});
CSS:
body {
margin: 0;
padding: 0;
font-size: 16px;
}
.wrap, .footer, .header {
position: relative;
width: 75%;
margin: auto;
overflow: hidden;
background-color: #EBEBEB;
}
.footer, .header {
height: 200px;
background-color: #999;
}
.footer {
height: 2000px;
}
.content {
float: left;
position: relative;
width: 70%;
}
.sidebar {
float: left;
position: relative;
width: 30%;
background-color: #0CF;
}
.item {
height: 300px;
position: relative;
width: 100%;
background-color: #0F6;
margin-bottom: 20px;
box-sizing: border-box;
padding: 20px;
}
.menu-top {
height: 120px;
box-sizing: border-box;
padding: 20px;
}
.menu-bottom {
z-index: 99;
position: relative;
height: 300px;
background-color: red;
box-sizing: border-box;
padding: 20px;
}
进行了一些优化,切换了一个额外的sticky
类。在Firefox上检查了平滑滚动和各种填充边距,似乎对确切的位置做出了响应:
演示
.sticky {
position: fixed;
}
$(function() {
var wrap = $('.wrap'),
lowmenu = $('.menu-bottom'),
range = $('.sidebar').width(),
fromtop = 60,
edge = parseInt($('.item:last').css('margin-bottom'));
lowmenu.css('width', range);
var fixtop = lowmenu.offset().top-fromtop,
lowpoint = wrap.offset().top+wrap.outerHeight()-lowmenu.outerHeight()-edge-fromtop,
placetop = wrap.outerHeight()-lowmenu.outerHeight()-$('.menu-top').outerHeight()-edge;
$(window).scroll(function() {
var current = $(window).scrollTop();
if (current >= fixtop && current <= lowpoint && !stuck()) lowmenu.addClass('sticky').css('top', fromtop);
else if (current > lowpoint && stuck()) lowmenu.removeClass('sticky').css('top', placetop);
else if (current < fixtop && stuck()) lowmenu.removeClass('sticky').css('top', 0);;
});
function stuck() {
return lowmenu.hasClass('sticky');
}
});
您可以将fromtop
设置为任何数字。最后一次.item
的保证金也应计入。。。
相关文章:
- 当侧边栏很长时,滚动/固定侧边栏会被切断
- 创建带有侧边栏的谷歌地图
- 如何在显示侧边栏时禁用背景,单击除侧边栏之外的任何位置都会关闭侧边栏
- 在页脚处停止固定侧边栏-防止重叠
- 如何将javascript添加到Wordpress侧边栏中
- 如何正确地将数据发送到打开的Firefox侧边栏
- 如何制作侧边栏以增加其完整长度
- 固定位置侧边栏不滚动-设置内容的高度
- Firefox插件SDK:在侧边栏和主脚本之间通信对象
- 在上下文菜单项单击上显示侧边栏/弹出窗口的最佳方法是什么
- 如何将数组(Google应用程序脚本)返回到HTML侧边栏
- 切换传单侧边栏 V2
- 粘性侧边栏停止在页脚上方 30px
- 使用什么js函数来切换侧边栏
- 指定的侧边栏广播器无效 -- 有效的侧边栏广播器是什么样子的
- 向下滚动页面时在侧边栏中显示部分标题(也许使用 BS scrollspy?
- 火狐侧边栏获取标签网址
- 滚动时固定了侧边栏的计算
- 如何使用 php 包含侧边栏,并在侧边栏中为当前页面使用不同的颜色
- 如何将粘性菜单包含在浮动侧边栏中