简体   繁体   English

JQUERY scrollTop无法正常工作

[英]JQUERY scrollTop not working properly

So I have this jquery code 所以我有这个jQuery代码

$(document).scroll(function () {
    var y = $(this).scrollTop();
    if (y > 50) {
        $('.top-bottom').fadeIn();
    } else {
        $('.top-bottom').fadeOut();
    }
});
$('.go-top').click(function(event) {
    event.preventDefault();
    $('html, body').animate({scrollTop: 0}, 300);
});
$('.go-bottom').click(function(event) {
    event.preventDefault();
    $('html, body').animate({scrollTop: $(document).height()-$(window).height()}, 300);
});

And I have it associated to this block of HTML 而且我将其与此HTML块相关联

<div class="top-bottom">
    <a href="#" class="go-top"><img src="images/image.png" class="arrow"></a>
    <a href="#" class="go-bottom"><img src="images/image.png" class="arrow"></a>
</div>

The class arrow on the image is so I can resize it but basically I want it so when I scroll down go-top and go-bottom appear, which they are doing, but then I want it so when I click on either go-top or go-bottom it either takes me to the top, or the bottom of the page respectively, and now that's where I have the problem, whenever I click on the image it just reloads the page with the # added onto it. 图像上的类箭头是可以调整大小的,但是基本上我想要它,所以当我向下滚动go-top并出现go-bottom时,他们正在这样做,但是然后我想要这样,当我单击任一go-top时或从底开始,它要么分别带我到页面顶部或底部,现在这就是我的问题所在,每当我单击图像时,它都会重新加载带有#号的页面。 Anyone have any clue what I'm doing wrong, I've been trying to figure it out all afternoon and can't seem to get a hang of it, it's probably a silly mistake I've made or it may be very complex actually who knows, but if anyone could help me it would be greatly appreciated. 任何人都知道我在做什么错,我一直在努力弄清整个下午,似乎无法把握住它,这可能是我犯的一个愚蠢的错误,或者实际上可能很复杂谁知道,但是如果有人可以帮助我,将不胜感激。

EDIT: http://jsfiddle.net/3v6yy/ This is basically my website, I don't know what I'm doing wrong and why this won't work, the arrow images usually appear when I look at my original file but for some reason they arent appearing here, if anyone knows how to make the effect I want(Have both arrows fade in when you scroll a few pixels from the top, and maybe even have them disappear when they're at the bottom, and when you click on them have them take you to either the top or the bottom of the page, depending on what button) it would be much appreciated if you could maybe help me out, I'm not a professional, this is a school project and I really want to do it well, so apart from getting the job done it has to be simple at the same time so I can understand whats going on basically 编辑: http : //jsfiddle.net/3v6yy/这基本上是我的网站,我不知道自己在做错什么,为什么这行不通,当我查看原始文件时通常会出现箭头图像,但是由于某些原因,它们会出现在这里,如果有人知道如何实现我想要的效果(当您从顶部滚动几个像素时,两个箭头都会淡入,甚至当它们位于底部时,甚至当它们消失时,您单击它们,使它们带您到页面的顶部或底部,具体取决于哪个按钮),如果您能帮助我,我不是专业人士,这是一个学校项目,我真的很想做得好,所以除了要完成工作外,它必须同时很简单,这样我才能基本了解正在发生的事情

Remove that '#' from href attribute. 从href属性中删除该“#”。

<div class="top-bottom">
   <a href="" class="go-top"><img src="images/image.png" class="arrow"></a>
   <a href="" class="go-bottom"><img src="images/image.png" class="arrow"></a>
</div>

As your JQuery function just take the click event, here no need of the '#' tag. 由于您的JQuery函数只接受click事件,因此这里不需要'#'标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM