我刚刚使用导航栏固定左侧创建了一组复选框。我希望这个菜单像粘性一样。怎么可能。
var acc = document.getElementsByClassName("accord");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
var panel = this.nextElementSibling;
if (panel.style.display === "none") {
panel.style.display = "block";
} else {
panel.style.display = "none";
}
}
}
*{
padding: 0;
margin: 0;
}
.navbar-fixed-left{
background-color: #fff;
border-radius: 0;
border-color: transparent;
position: absolute;
left: 0;
top: 0;
width: 100%;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.14);
}
.panel-heading{
background-color: transparent !important;
padding: 10px 15px 10px 25px;
}
.panel-group{
margin-top: 10px;
}
.panel{
border: none;
border-radius: 0;
}
.panel-body{
border: none !important;
padding: 15px 15px 15px 25px;
}
.catg-lists{
padding-left: 10px;
padding-bottom: 18px;
}
.catg-lists li{
list-style-type: none;
margin-bottom: 4px;
color: #838383;
font-size: 16px;
text-transform: capitalize;
}
.catg-lists li input[type=checkbox]{
margin-right: 6px;
}
.cat-menu{
padding: 8px 15px 8px 4px;
border-bottom: 2px solid #359443;
}
.cat-menu span{
text-transform: capitalize;
font-size: 15px;
font-weight: 600;
color: #292929;
padding: 2px 20px 6px 5px;
border-bottom: 2px solid #458b53;
}
.cat-menu:hover, .cat-menu:active, .cat-menu:visited, .cat-menu:focus{
text-decoration: none;
}
.accord {
display: block;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
position: relative;
}
.accord i{
position: absolute;
right: 30px;
top: 22px;
color: #349342;
}
div.panel {
padding: 0 18px;
background-color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<section id="photo-gall">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="navbar navbar-inverse navbar-fixed-left">
<div class="panel-group" id="accordion">
<a class="cat-menu accord">
<span>category</span><i class="fa fa-caret-down"></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">fruits & vegetables</li>
<li><input type="checkbox">fresh fruits <span>(87)</span></li>
<li><input type="checkbox">fresh vegetables <span>(191)</span></li>
<li><input type="checkbox">international fruits & vegetables <span>(128)</span></li>
<li><input type="checkbox">organic fruits & vegetables <span>(50)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>price</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">Less than rs 20 <span>(53)</span></li>
<li><input type="checkbox">rs 21 to rs 50 <span>(164)</span></li>
<li><input type="checkbox">rs 51 to rs 100<span>(126)</span></li>
<li><input type="checkbox">rs 101 to rs 200 <span>(77)</span></li>
<li><input type="checkbox">rs 201 to rs 500 <span>(62)</span></li>
<li><input type="checkbox">more than rs 501 <span>(27)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>pack size</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">100 grams <span>(53)</span></li>
<li><input type="checkbox">200 grams <span>(164)</span></li>
<li><input type="checkbox">300 grams<span>(126)</span></li>
<li><input type="checkbox">500 grams <span>(77)</span></li>
<li><input type="checkbox">750 grams <span>(62)</span></li>
<li><input type="checkbox">1 Kg <span>(27)</span></li>
<li><input type="checkbox">2 Kg <span>(43)</span></li>
<li><input type="checkbox">combo - 2 items<span>(0)</span></li>
<li><input type="checkbox">combo - 3 items<span>(50)</span></li>
<li><input type="checkbox">combo - 4 items<span>(10)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>discounts</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">10%-20%</li>
<li><input type="checkbox">30%-40%</li>
<li><input type="checkbox">50%</li>
<li><input type="checkbox">60%-70%</li>
<li><input type="checkbox">80%-90%</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-8">
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
</div>
</div>
</div>
</section>
当我滚动页面时,菜单应该是粘性的。我试过但它没有用。
注意:在宽屏幕上检查此代码可获得更好的效果。可能会在代码段中混淆。
这是此代码的结果。
如何实现粘性菜单?
提前致谢。
答案 0 :(得分:1)
您需要使用固定位置并将其添加到滚动:
添加jQuery:
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
为固定位置和最大宽度添加CSS类,否则使用宽度:100%+位置:固定元素将采用窗口全宽。注意左边:固定时自动,否则左边:0,它将一直向左移动:
.navbar-fixed-left{
background-color: #fff;
border-radius: 0;
border-color: transparent;
position: absolute;
left: 0;
top: 0;
width: 100%;
max-width: 350px;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.14);
}
.navbar-fixed-left.fixed{
position: fixed;
top:0;
left: auto;
}
添加JS以检测滚动页面并添加类:
var $sidebar = jQuery('.navbar-fixed-left'),
$window = jQuery(window),
offset = $sidebar.offset();
$window.scroll(function() {
if ($window.scrollTop() > offset.top) {
$sidebar.addClass('fixed');
} else {
$sidebar.removeClass('fixed');
}
});
答案 1 :(得分:0)
添加css:
div#accordion {
position: fixed;
background: black;
}
删除课程:
navbar navbar-inverse navbar-fixed-left
在删除位置:修复时添加媒体查询(我没有添加媒体查询,但您必须添加)
var acc = document.getElementsByClassName("accord");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
var panel = this.nextElementSibling;
if (panel.style.display === "none") {
panel.style.display = "block";
} else {
panel.style.display = "none";
}
}
}
*{
padding: 0;
margin: 0;
}
.navbar-fixed-left{
background-color: #fff;
border-radius: 0;
border-color: transparent;
position: absolute;
left: 0;
top: 0;
width: 100%;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.14);
}
.panel-heading{
background-color: transparent !important;
padding: 10px 15px 10px 25px;
}
.panel-group{
margin-top: 10px;
}
.panel{
border: none;
border-radius: 0;
}
.panel-body{
border: none !important;
padding: 15px 15px 15px 25px;
}
.catg-lists{
padding-left: 10px;
padding-bottom: 18px;
}
.catg-lists li{
list-style-type: none;
margin-bottom: 4px;
color: #838383;
font-size: 16px;
text-transform: capitalize;
}
.catg-lists li input[type=checkbox]{
margin-right: 6px;
}
.cat-menu{
padding: 8px 15px 8px 4px;
border-bottom: 2px solid #359443;
}
.cat-menu span{
text-transform: capitalize;
font-size: 15px;
font-weight: 600;
color: #292929;
padding: 2px 20px 6px 5px;
border-bottom: 2px solid #458b53;
}
.cat-menu:hover, .cat-menu:active, .cat-menu:visited, .cat-menu:focus{
text-decoration: none;
}
.accord {
display: block;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
position: relative;
}
.accord i{
position: absolute;
right: 30px;
top: 22px;
color: #349342;
}
div.panel {
padding: 0 18px;
background-color: white;
}
div#accordion {
position: fixed;
background: black;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<section id="photo-gall">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="">
<div class="panel-group" id="accordion">
<a class="cat-menu accord">
<span>category</span><i class="fa fa-caret-down"></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">fruits & vegetables</li>
<li><input type="checkbox">fresh fruits <span>(87)</span></li>
<li><input type="checkbox">fresh vegetables <span>(191)</span></li>
<li><input type="checkbox">international fruits & vegetables <span>(128)</span></li>
<li><input type="checkbox">organic fruits & vegetables <span>(50)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>price</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">Less than rs 20 <span>(53)</span></li>
<li><input type="checkbox">rs 21 to rs 50 <span>(164)</span></li>
<li><input type="checkbox">rs 51 to rs 100<span>(126)</span></li>
<li><input type="checkbox">rs 101 to rs 200 <span>(77)</span></li>
<li><input type="checkbox">rs 201 to rs 500 <span>(62)</span></li>
<li><input type="checkbox">more than rs 501 <span>(27)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>pack size</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">100 grams <span>(53)</span></li>
<li><input type="checkbox">200 grams <span>(164)</span></li>
<li><input type="checkbox">300 grams<span>(126)</span></li>
<li><input type="checkbox">500 grams <span>(77)</span></li>
<li><input type="checkbox">750 grams <span>(62)</span></li>
<li><input type="checkbox">1 Kg <span>(27)</span></li>
<li><input type="checkbox">2 Kg <span>(43)</span></li>
<li><input type="checkbox">combo - 2 items<span>(0)</span></li>
<li><input type="checkbox">combo - 3 items<span>(50)</span></li>
<li><input type="checkbox">combo - 4 items<span>(10)</span></li>
</ul>
</div>
<a class="cat-menu accord">
<span>discounts</span><i class="fa fa-caret-down" ></i></a>
<div class="panel">
<ul class="catg-lists">
<li><input type="checkbox">10%-20%</li>
<li><input type="checkbox">30%-40%</li>
<li><input type="checkbox">50%</li>
<li><input type="checkbox">60%-70%</li>
<li><input type="checkbox">80%-90%</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-8">
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
<h1>Some content</h1>
</div>
</div>
</div>
</section>
答案 2 :(得分:0)
由于Listener
有.navbar
,我会添加几个属性:
position: relative;