我正在开发一个单页网站。到目前为止,我成功地实现了“垂直平滑滚动”功能(对于我的主页、关于、画廊......导航链接)并且它工作得很好。
现在,我想向同一个网站添加另一个 jquery - 一个小的“向上滚动”按钮,它应该显示在右下角以便于导航。问题是它永远不会出现???
这是我到目前为止所做的:
HTML:
<a class="scrollup" href="#">Scroll</a>
CSS:
.scrollup {
height: 38px;
width: 38px;
opacity: 1.0;
position:fixed;
bottom: 35px;
right: 35px;
background: url(images/top.png) no-repeat;
}
JQUERY(我打开了scrollup.js文件并在里面添加了这段代码):
// scroll-to-top button show and hide
jQuery(document).ready(function(){
jQuery(window).scroll(function(){
if (jQuery(this).scrollTop() > 100) {
jQuery('.scrollup').fadeIn();
} else {
jQuery('.scrollup').fadeOut();
}
});
// scroll-to-top animate
jQuery('.scrollup').click(function(){
jQuery("html, body").animate({ scrollTop: 0 }, 600);
return false;
});
});
我还将这些脚本添加到我的 HTML header 中:
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
<script type='text/javascript' src='scrollup.js'></script>
<script type='text/javascript' src='verticalsmoothscrolling.js'></script>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link href='http://fonts.googleapis.com/css?family=Roboto+Slab:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Noto+Sans' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Fauna+One' rel='stylesheet' type='text/css'>
尽管如此,我的垂直平滑滚动条工作正常,但“向上滚动”按钮从未出现???这个问题底部的脚本顺序重要吗?或者是别的东西?希望你们能帮我解决这个问题。谢谢 :)
最佳答案
如果文件被正确加载并且滚动事件被正确触发,这个脚本就可以工作(jsFiddle)。这意味着三件事之一:
脚本未正确加载。您应该会在开发者控制台中看到与 404 错误相关的错误。
此脚本的资源未正确加载。情况似乎并非如此,因为您在脚本之前正确地包含了 jQuery。这通常会导致在开发人员控制台中弹出错误:referenceError (
$ is not defined
orjQuery is not defined
) or ablahblah is not a function
其中 blahblah 是您调用的函数。滚动事件未触发。可能是这种情况,因为您有另一个脚本可以更改滚动的“外观”。
如果此脚本将处理程序附加到
wheel
事件或DOMMouseScroll
事件并阻止默认操作,则永远不会触发滚动事件。这是如何工作的 can be found in this question .要检查是否调用了滚动事件,请将处理程序附加到滚动事件并在控制台中记录一些内容。检查控制台以查看是否记录了某些内容:
$(window).on( 'scroll', function() { console.log( 'The scroll event fired!' ); } );
如果滚动事件没有触发,您可以通过在
wheel
( docs ) 和/或DOMMouseScroll
事件中触发滚动事件来“修复”这个问题( docs ):function triggerScroll() { $( window ).trigger( 'scroll' ); } jQuery.on( 'wheel', triggerScroll ); jQuery.on( 'DOMMouseScroll', triggerScroll );
关于javascript - 向上滚动按钮不显示/不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429976/