【问题标题】:Bootstrap Affix "Back to Top" LinkBootstrap 附加“返回顶部”链接
【发布时间】:2016-01-15 20:16:20
【问题描述】:

好的,我无法理解 Bootstrap Affix component。如果/当页面滚动到内容顶部下方时,我的目标是在屏幕左下角(边缘)出现一个“返回顶部”链接。我的页面有一个固定在顶部的 NavBar 和一个用于正文的容器。以下是我所在位置的总体思路,但我也有 setup a JS Fiddle 来演示我的设置。我也不是定位专家,所以这也是我的问题的一部分。

<div class="navbar navbar-fixed-top">...</div>
<div class="content-container" id="top">
    <p>Content that is longer than the viewport..</p>
    <span id="top-link" data-spy="affix">
        <a href="#top" class="well well-sm">Back to Top</a>
    </span>
</div>

<style>
    .navbar-fixed-top + .content-container {
        margin-top: 70px;
    }
    .content-container {
        margin: 0 125px;
    }
    #top-link.affix {
        position: absolute;
        bottom: 10px;
        left: 10px;
    }
</style>

【问题讨论】:

标签: css twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

现在我更好地理解了 Affix 组件,我想出了解决方案。指定顶部偏移量并调整 CSS 后,它运行良好。该链接将滚动到视图中,然后“固定”到底部。对于没有滚动条的页面,该链接永远不会启用。我已经用一个工作示例更新了JS Fiddle (here)。关键部分是:

HTML:

<!-- child of the body tag -->
<span id="top-link-block" class="hidden">
    <a href="#top" class="well well-sm" onclick="$('html,body').animate({scrollTop:0},'slow');return false;">
        <i class="glyphicon glyphicon-chevron-up"></i> Back to Top
    </a>
</span><!-- /top-link-block -->

JS:

<script>
// Only enable if the document has a long scroll bar
// Note the window height + offset
if ( ($(window).height() + 100) < $(document).height() ) {
    $('#top-link-block').removeClass('hidden').affix({
        // how far to scroll down before link "slides" into view
        offset: {top:100}
    });
}
</script>

CSS:

<style>
#top-link-block.affix-top {
    position: absolute; /* allows it to "slide" up into view */
    bottom: -82px;
    left: 10px;
}
#top-link-block.affix {
    position: fixed; /* keeps it on the bottom once in view */
    bottom: 18px;
    left: 10px;
}
</style>

注意:我无法使用 affix bottom offset (example) 隐藏短页面的链接,因为词缀容器高度计算存在错误(引导问题 #4647)。我确信有一种解决方法,并且会欢迎这种方法的解决方案。

【讨论】:

  • 这个($('body').outerHeight() &gt; $(window).height()) 不能代替( ($(window).height() + 100) &lt; $(document).height() ) 工作吗?
  • 添加偏移量差异的目的是在用户永远无法到达的情况下不启用 Affix 组件。我不清楚的几件事是;在文档上使用时,文档 vs 'body' 和 height() vs outerHeight()。
  • 我在使用 body 时略有错误,您可以使用 $(document).height();,但我现在了解您的需求。 document 是渲染的页面,而 body 只是 body 元素(html 元素可以有填充或固定高度,这不会影响 body 高度)。 height() 是元素高度(取决于盒子方法),可能包括也可能不包括填充,而外部高度包括填充、边框并且可以包括边距api.jquery.com/outerHeight
  • 感谢您的澄清!所以我正在做的身高比较会很好用吗?
  • 是的,如果你想变得聪明,你可以动态地将 100 更改为词缀图标的大小,以防万一你改变它
【解决方案2】:

感谢返回顶部按钮,对我也很有用 :) 一个小的改进是避免在&lt;a&gt; 标签上使用onclick="",而是使用jQuery's 事件注册器:

HTML:

...
<a href="#top" id ="backToTopBtn" class="well well-sm">
...

JS:

 $('#backToTopBtn').click(function(){
        $('html,body').animate({scrollTop:0},'slow');return false;
    });

【讨论】:

  • 好点,那肯定是更好的方法。我只是将它放在 onclick="" 中以使示例更短且不那么复杂。谢谢!
猜你喜欢
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多