【问题标题】:Sticky "back to top" button showing on page load, before scrolling down向下滚动之前,在页面加载时显示粘滞的“返回顶部”按钮
【发布时间】: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; 或将您的内联样式更改为相同。
  • @WillRyan 他说的是第一行 -
  • 做到了!谢谢!奇怪...教程特别有那个带有 display:inline 的 HTML 块。

标签: javascript jquery css scroll jquery-animate


【解决方案1】:

更改您的 HTML
<a href="#" class="go-top" style="display: inline;">Back to top</a>

<a href="#" class="go-top" style="display: none;">Back to top</a>

这将在您滚动之前隐藏您的按钮。

【讨论】:

    【解决方案2】:

    它正在显示,因为您尚未触发滚动事件以使该逻辑运行以隐藏/显示它

    <script>
        $(document).ready(function() {
            function checkPosition() {
                if ($(this).scrollTop() > 200) {
                    $('.go-top').fadeIn(500);
                } else {
                    $('.go-top').fadeOut(300);
                }
            }
            // Show or hide the sticky footer button
            $(window).scroll(checkPosition);
    
            // Animate the scroll to top
            $('.go-top').click(function(event) {
                event.preventDefault();
    
                $('html, body').animate({scrollTop: 0}, 300);
            })
    
            checkPosition();
        });
    </script>
    

    这个新的重构将在页面加载时至少触发一次checkPosition,以确保按钮淡出。另一种解决方案是在元素的 CSS 中设置 display: none;,因此默认情况下它是隐藏的,然后仅由 javascript 显示

    【讨论】:

    • 我的理解是这段代码默认隐藏,然后滚动显示。
    • 是的,不是这样,因为隐藏它的部分仅在您开始滚动时运行。您需要在页面加载时至少运行一次“位置”检查。另请参阅底部的替代解决方案。
    • 这也会在你加载时变成fadeOut。所以你会看到它在加载时淡出
    • 是的,好点。 CSS/内联显示:没有更好的选择
    【解决方案3】:

    我按照用户 ntgCleaner 所说的将 html 中的“display:inline”更改为“display:none”,它似乎可以工作。谢谢!

    【讨论】:

    • 哎呀,直到现在才看到您的其他评论,只是您对我的帖子的原始评论。刚刚选择了你的作为正确答案,再次感谢!
    • 随时!很高兴我能帮上忙
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多