我有这个jquery代码:
$(function(){
var $win = $(window);
var $nav = $('.subnav');
var navTop = $('.subnav').length && $('.subnav').offset().top - 38;
var isFixed = 0;
processScroll();
$win.on('scroll', processScroll);
function processScroll() {
console.log('test');
var i, scrollTop = $win.scrollTop();
if (scrollTop >= navTop && !isFixed) {
isFixed = 1;
$nav.addClass('subnav-fixed');
} else if (scrollTop <= navTop && isFixed) {
isFixed = 0;
$nav.removeClass('subnav-fixed');
}
};
})
如果我有这个网址,例如:
http://mydomain.com/posts
或http://mydomain.com
或http://mydomain.com/post?utf8=✓&search=
代码工作正常,但如果我有例如:
http://mydomain.com/post?utf8=✓&search=port
或http://mydomain.com/post?utf8=✓&search=word
代码无效......
为什么我使用搜索引擎传递url的参数代码不能正常工作?
非常感谢!
我正在使用这个太阳黑子solr作为我的搜索引擎
答案 0 :(得分:0)
我在这个问题中找到了解决方法:
Replicating Bootstraps main nav and subnav
这是代码:
$(document).scroll(function(){
// If has not activated (has no attribute "data-top"
if (!$('.subnav').attr('data-top')) {
// If already fixed, then do nothing
if ($('.subnav').hasClass('subnav-fixed')) return;
// Remember top position
var offset = $('.subnav').offset()
$('.subnav').attr('data-top', offset.top);
}
if ($('.subnav').attr('data-top') - $('.subnav').outerHeight() <= $(this).scrollTop())
$('.subnav').addClass('subnav-fixed');
else
$('.subnav').removeClass('subnav-fixed');
});
谢谢