【发布时间】:2014-04-17 04:48:23
【问题描述】:
我按照教程获得了一个粘性“返回顶部”按钮,一旦您向下滚动就会出现该按钮。出于某种原因,当您在页面首次加载后位于页面顶部时,它会显示。如果您向下滚动,然后一直向上滚动,它就会消失(应该如此)。但最初它的行为不正常。有什么想法吗?
这是我使用它的实时页面,你可以在右下角看到它:http://willryan.us
HTML
<a href="#" class="go-top" style="display: inline;">Back to top</a>
<script>
$(document).ready(function() {
// Show or hide the sticky footer button
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$('.go-top').fadeIn(500);
} else {
$('.go-top').fadeOut(300);
}
});
// Animate the scroll to top
$('.go-top').click(function(event) {
event.preventDefault();
$('html, body').animate({scrollTop: 0}, 300);
})
});
</script>
CSS
.go-top {
position: fixed;
bottom: 0.75em;
right: 0.5em;
text-decoration: none;
color: white;
background-color: rgba(0, 0, 0, 0.25);
font-size: 12px;
padding: 10px;
display: none;
margin: 0;
}
.go-top:hover {
background-color: rgba(0, 0, 0, 0.6);
color: white;
text-decoration: none;
}
【问题讨论】:
-
您正在使用
display:inline加载元素,将其更改为display:none,当您第一次加载时它不会出现 -
你说的是“.go-top”类吗?那确实有显示:无。编辑:啊,你说的是html。我现在试试,谢谢。
-
您的 HTML 有一个显示为
display:inline的内联样式 - 让您的样式表显示为display:none !important;或将您的内联样式更改为相同。 -
做到了!谢谢!奇怪...教程特别有那个带有 display:inline 的 HTML 块。
标签: javascript jquery css scroll jquery-animate