嗨,大家好我在jquery scrollTop中遇到了一個問題,我的問題是在導航到id #linkA後單擊它,再次添加一個不需要的滾動條。我想在單擊鏈接後阻止滾動。讓我們假設這三個段落之間有很大的差距。當鏈接被點擊時,jquery防止ScrollTop
HTML:
<ul>
<li ><a href="#linkA"> Link A </a></li>
<li ><a href="#linkB"> Link B </a></li>
</ul>
<p id="#linkA">Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit
anim id est laborum.</p>
<p id="#linkB">Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit
anim id est laborum.</p>
的Jquery:
$(document).ready(function() {
$('.a').click(function() {
$('body').animate({
scrollTop: $("#linkA").offset().top
}, 500);
return false;
});
$('.b').click(function() {
$('body').animate({
scrollTop: $("#linkB").offset().top
}, 500);
return false;
});
});
$(window).scroll(function() {
var windowTop = $(document).scrollTop();
if (windowTop > 0 && windowTop <= 200) {
//i got some parallaxing code here
}
});
或者你們都當鏈接被點擊任何想法如何阻止scrollTop的?謝謝。
使用此:http://stackoverflow.com/a/488073/961695 - 如果funciton返回true - 段落已在視圖中,並且不需要滾動 –