我有一个导航菜单,它位于单页滚动网站的顶部。它使用" sticky" jquery插件,以便在页面滚动时保持可见。它完美地工作,直到我为下拉菜单添加嵌套的无序列表。 这是下拉网站:
http://creativelaunch.net/clients/mediafactory/option-b/
这是粘性插件的代码:
(function($) {
var defaults = {
topSpacing: 0,
bottomSpacing: 0,
className: 'is-sticky',
wrapperClassName: 'sticky-wrapper',
center: false,
getWidthFrom: ''
},
$window = $(window),
$document = $(document),
sticked = [],
windowHeight = $window.height(),
scroller = function() {
var scrollTop = $window.scrollTop(),
documentHeight = $document.height(),
dwh = documentHeight - windowHeight,
extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
for (var i = 0; i < sticked.length; i++) {
var s = sticked[i],
elementTop = s.stickyWrapper.offset().top,
etse = elementTop - s.topSpacing - extra;
//update height in case of dynamic content
s.stickyWrapper.css('height', s.stickyElement.outerHeight());
if (scrollTop <= etse) {
if (s.currentTop !== null) {
s.stickyElement
.css('position', '')
.css('top', '');
s.stickyElement.parent().removeClass(s.className);
s.currentTop = null;
}
}
else {
var newTop = documentHeight - s.stickyElement.outerHeight()
- s.topSpacing - s.bottomSpacing - scrollTop - extra;
if (newTop < 0) {
newTop = newTop + s.topSpacing;
} else {
newTop = s.topSpacing;
}
if (s.currentTop != newTop) {
s.stickyElement
.css('position', 'fixed')
.css('top', newTop);
if (typeof s.getWidthFrom !== 'undefined') {
s.stickyElement.css('width', $(s.getWidthFrom).width());
}
s.stickyElement.parent().addClass(s.className);
s.currentTop = newTop;
}
}
}
},
resizer = function() {
windowHeight = $window.height();
},
methods = {
init: function(options) {
var o = $.extend(defaults, options);
return this.each(function() {
var stickyElement = $(this);
var stickyId = stickyElement.attr('id');
var wrapper = $('<div></div>')
.attr('id', stickyId + '-sticky-wrapper')
.addClass(o.wrapperClassName);
stickyElement.wrapAll(wrapper);
if (o.center) {
stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"});
}
if (stickyElement.css("float") == "right") {
stickyElement.css({"float":"none"}).parent().css({"float":"right"});
}
var stickyWrapper = stickyElement.parent();
stickyWrapper.css('height', stickyElement.outerHeight());
sticked.push({
topSpacing: o.topSpacing,
bottomSpacing: o.bottomSpacing,
stickyElement: stickyElement,
currentTop: null,
stickyWrapper: stickyWrapper,
className: o.className,
getWidthFrom: o.getWidthFrom
});
});
},
update: scroller
};
// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
if (window.addEventListener) {
window.addEventListener('scroll', scroller, false);
window.addEventListener('resize', resizer, false);
} else if (window.attachEvent) {
window.attachEvent('onscroll', scroller);
window.attachEvent('onresize', resizer);
}
$.fn.sticky = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method ) {
return methods.init.apply( this, arguments );
} else {
$.error('Method ' + method + ' does not exist on jQuery.sticky');
}
};
$(function() {
setTimeout(scroller, 0);
});
})(jQuery);
以下是下拉菜单的代码:
<!--HEADER START-->
<nav class="header home-section clearfix">
<div class="for-sticky">
<div class="container nav-box">
<div class="logo">
<a href="index.html">
<img class="logo1" alt="Media Factory" src="images/mf-logo-white-main.png">
</a>
</div>
<div class="menu-box hidden-xs hidden-sm">
<ul class="navigation">
<li class="current"><a href="#home">Home</a></li>
<li><a href="#about">About Us</a>
<ul>
<li><a href="#">Our Mission</a></li>
<li><a href="#">The Factory</a></li>
<li><a href="#">Meet The Team</a></li>
<li><a href="#">Who We Serve</a></li>
</ul>
</li>
<li><a href="#works">Our Work</a>
<ul>
<li><a href="#">Printing</a></li>
<li><a href="#">Signs, Banners +</a></li>
<li><a href="#">Mailing</a></li>
<li><a href="#">Design</a></li>
<li><a href="#">Vehicle Graphics</a></li>
<li><a href="#">Promotional Items</a></li>
</ul>
</li>
<li><a class="external" href="#">Blog</a></li>
<li><a href="#resources">Resources</a>
<ul>
<li><a href="#">Upload Files</a></li>
<li><a href="#">Case Studies</a></li>
<li><a href="#">Estimate Request</a></li>
<li><a href="#">Place An Order</a></li>
<li><a href="#">File Specs</a></li>
<li><a href="#">Mailing Specs</a></li>
<li><a href="#">FAQ</a></li>
</ul>
</li>
<li><a class="external" href="#">Our Store</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.menu-box-->
<div class="box-mobile hidden-lg hidden-md">
<div class="menu-btn" data-toggle="collapse" data-target=".nav-collapse">
<span class="fa fa-bars"></span>
</div>
<ul class="nav-collapse mobile-menu hidden-lg hidden-md"></ul>
</div><!--/.box-mobile-->
</div><!--/.container-->
</div><!--/.for-sticky-->
</nav><!--/.header-->
<!--HEADER END-->
最后,这里是下拉列表的CSS代码。它使用与父菜单相同的样式。如果我删除下拉列表及其CSS代码,粘滞剂将再次开始工作。
/*-----------------------------HEADER SECTION-----------------------------------*/
.home-section.header {
top: 0;
left: 0;
position: absolute;
width: 100%;
z-index:999;
}
.logo {
float: left;
max-width: 20%;
}
.menu-box {
float: right;
max-width: 80%;
text-align: right;
}
/*----------------------------------------------*/
.navigation {
display: block;
list-style: outside none none;
margin: 0;
padding: 0;
}
.navigation li {
display: inline-block;
}
.navigation li a {
color: #fff;
display: block;
font-family: Montserrat;
font-size: 12px;
font-weight: bold;
letter-spacing: 2.5px;
padding: 15px 17px;
text-transform: uppercase;
}
.navigation li a:hover{color:#cc227b;}
.navigation li ul {
display: none;
position: absolute;
text-align: left;
left: 10%;
}
.navigation li:hover ul {
display: block;
}
.navigation .current a{color:#cc227b;}
.for-sticky {
top: 0;
left: 0;
padding: 10px 0;
width: 100%;
z-index:9999;
transition: ease .3s;
-webkit-transition: ease .3s;
-moz-transition: ease .3s;
-o-transition: ease .3s;
-ms-transition: ease .3s;
}
.is-sticky .for-sticky {
background: #000 none repeat scroll 0 0;
}
感谢您的帮助!!