我在创建粘性标题时遇到问题 - 当我将其子位置设置为“固定”时,它基本上消失了。 这是我目前的情况:
HTML:
<div id="container" class="container-fluid">
<header class="row">
<div id="logo-container" class="col-lg-10 col-lg-push-1">
<img src="https//:placehold.it/150x30" alt="my img" id="logo"/>
</div>
<nav class="col-lg-12 no-padding">
<ul id="nav-list" class="col-lg-12">
<li>Link 0</li>
<li>link 1</li>
</ul>
</nav>
</header>
<div id="main-container" class="row">
<div id="content" class="col-lg-10 col-lg-push-1 no-padding">
// body
</div>
</div>
CSS:
header {
background: green;
}
#logo-container {
padding: 20px 15px 20px 0px;
}
nav {
background: red;
position: relative;
height: 60px;
}
nav.fixed {
position: fixed;
top: 0;
left: 0;
z-index: 10;
}
#nav-list {
text-align: right;
padding: 0 10% 0 0;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
#main-container { /* jumps when 'nav' is fixed */
background: #FFFFFF;
padding: 30px 0;
}
JS:
$(function () {
var mainContainer = $('#main-container');
var nav = $('nav');
var navHeight = nav.outerHeight();
var navPos = nav.offset().top;
var logoContainer = $('#logo-container');
$(window).on('scroll', function () {
if ($(window).scrollTop() >= navPos) {
$('header').height(navHeight); // set its height so that it stays in place, even with a fixed-positioned child
nav.addClass('fixed');
}
else {
logoContainer.show();
nav.removeClass('fixed');
$('header').removeAttr('style');
}
})
})
提前致谢!
答案 0 :(得分:0)
Fixed =元素相对于浏览器窗口定位。如果这比你的欢迎更有帮助。