我在jQuery平滑滚动时遇到了问题。我有一个onepager,我想在它上滚动到不同的DIVs,通过锚点。不允许手动从一个DIV滚动到另一个DIV。为了达到这个目的,我禁用了整个页面上的滚动,但在单击菜单按钮的同时将其设置回正常状态一段时间。
它的工作原理是这样的:一旦你进入网站,你就会看到我的第一个DIV内容。当您点击菜单中的一个链接时,您会看到一个简短的动画,其中一个图像离开DIV,然后页面滚动到下一个DIV。在第二个DIV中,图像在动画中显示,文本在其中滚动。第一次它工作,但当我想点击另一个链接时,滚动工作,但一秒钟后,在右边的DIV,页面跳回到顶部(到第一个DIV)。
我已经尝试过用event.preventDefault();和return false来解决这个问题,但是它还是跳到了顶端。
有谁能帮帮我吗?
var scrollPosition = [
self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
];
var html = jQuery('html, body'); // it would make more sense to apply this to body, but IE7 won't have that
html.data('scroll-position', scrollPosition);
html.data('previous-overflow', html.css('overflow'));
html.css('overflow', 'hidden');
window.scrollTo(scrollPosition[0], scrollPosition[1]);
$(function() {
$('a[href*=#]:not([href=#])').on('click', function(event) {
event.preventDefault();
var html = jQuery('html, body');
var scrollPosition = html.data('scroll-position');
html.css('overflow', html.data('previous-overflow'));
window.scrollTo(scrollPosition[0], scrollPosition[1])
var href = $(this).attr('href');
setTimeout(function() {
$('html, body').stop().animate({
scrollTop: $(href).offset().top
}, 1500, 'easeInExpo');
}, 900);
setTimeout(function() {
$('body').css('overflow', 'hidden');
$('html').css('overflow', 'hidden');
}, 4000);
});
});<div id="page-one"></div>
<div id="pageone">
<!-- some content -->
</div>
<div id="page-two"></div>
<div id="pagetwo">
<!-- some content -->
</div>
<div id="page-three"></div>
<div id="pagethree">
<!-- some content -->
</div>
发布于 2015-11-04 22:13:54
没有小提琴的麻烦,但请尝试以下。
$(document).on('click', 'a[href*=#]:not([href=#])', function(event) {
// code here
});我希望这是有意义的。.on()函数的第二个参数是选择器。
https://stackoverflow.com/questions/28477801
复制相似问题