Jquery 滚动到id脚本,如果我使用粘性菜单,将窗口滚动到错误位置
我有一个网页,其中插入了“滚动到id”和“粘性菜单”jQuery脚本。我遇到的问题是,当我在页面顶部时,第一次点击菜单。它与顶部的偏移与其他单击后的偏移不同(当我不在顶部时)。 我编写了一个示例代码。如果我不能说出我的意思,我希望你们能在样品中找到。Jquery 滚动到id脚本,如果我使用粘性菜单,将窗口滚动到错误位置,jquery,scroll,menu,offset,sticky,Jquery,Scroll,Menu,Offset,Sticky,我有一个网页,其中插入了“滚动到id”和“粘性菜单”jQuery脚本。我遇到的问题是,当我在页面顶部时,第一次点击菜单。它与顶部的偏移与其他单击后的偏移不同(当我不在顶部时)。 我编写了一个示例代码。如果我不能说出我的意思,我希望你们能在样品中找到。 HTML: <header> <a href="#part1">Part1</a> <a href="#part2">Part2</a> <a href="#
HTML:
<header>
<a href="#part1">Part1</a>
<a href="#part2">Part2</a>
<a href="#part3">Part3</a>
<a href="#part4">Part4</a>
<a href="#part5">Part5</a>
<a href="#part6">Part6</a>
</header>
<section id="part1" class="parts"><h1>Part 1</h1></section>
<section id="part2" class="parts silver"><h1>Part 2</h1></section>
<section id="part3" class="parts"><h1>Part 3</h1></section>
<section id="part4" class="parts silver"><h1>Part 4</h1></section>
<section id="part5" class="parts"><h1>Part 5</h1></section>
<section id="part6" class="parts silver"><h1>Part 6</h1></section>
body { margin: 0 0 0 0 }
.parts{ height: 200px; border: 1px gray solid }
.silver { background : silver }
header { background: yellow; height:50px; line-height: 50px; width:100% }
.sticky { position: fixed; left: 0; top: 0; z-index: 2000; width:100% }
h1 { text-align:right }
// This section is given from http://stackoverflow.com/questions/5284814/jquery-scroll-to-div
// Scroll to id script
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 50
}, 1000);
return false;
}
}
});
});
// Sticky menu script
var stickyNavTop = $('header').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('header').addClass('sticky');
} else {
$('header').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
jQuery:
<header>
<a href="#part1">Part1</a>
<a href="#part2">Part2</a>
<a href="#part3">Part3</a>
<a href="#part4">Part4</a>
<a href="#part5">Part5</a>
<a href="#part6">Part6</a>
</header>
<section id="part1" class="parts"><h1>Part 1</h1></section>
<section id="part2" class="parts silver"><h1>Part 2</h1></section>
<section id="part3" class="parts"><h1>Part 3</h1></section>
<section id="part4" class="parts silver"><h1>Part 4</h1></section>
<section id="part5" class="parts"><h1>Part 5</h1></section>
<section id="part6" class="parts silver"><h1>Part 6</h1></section>
body { margin: 0 0 0 0 }
.parts{ height: 200px; border: 1px gray solid }
.silver { background : silver }
header { background: yellow; height:50px; line-height: 50px; width:100% }
.sticky { position: fixed; left: 0; top: 0; z-index: 2000; width:100% }
h1 { text-align:right }
// This section is given from http://stackoverflow.com/questions/5284814/jquery-scroll-to-div
// Scroll to id script
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 50
}, 1000);
return false;
}
}
});
});
// Sticky menu script
var stickyNavTop = $('header').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('header').addClass('sticky');
} else {
$('header').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
请帮我解决这个问题。更换您的状况
if (scrollTop > stickyNavTop)
与
将第一个零件包括在条件中,并在此零件上添加边距顶部
#part1 {margin-top:50px}
更换您的状况
if (scrollTop > stickyNavTop)
与
将第一个零件包括在条件中,并在此零件上添加边距顶部
#part1 {margin-top:50px}