【问题标题】:Show footer when user scrolls to the bottom of the page当用户滚动到页面底部时显示页脚
【发布时间】:2015-12-22 14:40:48
【问题描述】:

这是我的页脚代码。

<div class="row">
    <div class="col-md-12">
        <div> the part that always showing at the bottom  </div>
    </div>
    <div class="col-md-12">
        <div> show only if the user reaching the bottom of the page </div>
    </div>
</div>

我的问题是我希望页脚贴在页面底部,直到用户到达底部,然后显示其他内容。

【问题讨论】:

  • 纯css是做不到的。您必须使用 javascript 或 jquery。
  • 您是在使用 jquery 还是更喜欢纯 javascript 解决方案?
  • 我现在使用 javascript。我在想怎么做。你能给我一个链接或建议吗?
  • 我现在使用 javascript。我在想怎么做。你能给我一个链接或建议吗?
  • 为什么在一行中有两个col-md-12s?

标签: javascript html css twitter-bootstrap


【解决方案1】:

仅在 CSS 的帮助下,您可以将其重新考虑为两个页脚,一个弹出,另一个无聊;)

[id^=foo]{
  background:orange;
  padding:5px;
  font-size:25px;
}

#foo-boring{
  position:fixed;
  bottom:0;
  right:0;
  left:0;
}
#foo-pop{
  position:absolute;
  height:70px;
  right:0; left:0;
}
<div>SCROLL ME DOWN<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br>much contents there.<br> END.</div>
<div id="foo-pop"><b>POP!1!!!1!!1!11!</b></div>
<div id="foo-boring">The boring footer.</div>

【讨论】:

  • 这只有在内容大于视口高度时才有效,对吧?
  • @Beren 如果内容大于视口,它已经像“滚动”到底部了,对吧? :) 任何粘贴的页脚都会发生这种情况。
  • @nicael 但它没有缝合到底部页脚。
  • @Berend 哦,对不起!很快就会做(现在有点忙)。
  • @Berend 嗯,我已经提供了获取弹出页脚百分比高度的解决方案,尽管无论如何都不需要这样做,因为如果你打算使用这个模型,你肯定会制作一个更大的页面(例如,我的页面太小了)。
【解决方案2】:

这里需要一点 Javascript。下面的代码应该可以工作。

$(document).ready(function() {
    $('#footer-final').hide()
});

$(window).scroll(function(){
    if ($(window).scrollTop() == $(document).height() - $(window).height()) {
        $('#footer-inter').hide()
        $('#footer-final').show()
    }
});

我假设您已经有了 CSS 来使页脚粘在页面底部 (position:fixed; bottom=0;) 在这种情况下,您可以替换任何代码来隐藏中间页脚并显示您想要的任何其他内容当用户滚动到底部时显示。

【讨论】:

    【解决方案3】:

    这是一个简单的脚本,用于跟踪滚动位置并将其与高度进行比较。滚动到底部时满足条件。到那时你可以随心所欲:)。

    window.addEventListener('scroll', function () {
        console.log('scroll: ' + (window.innerHeight + window.scrollY));
        console.log('height: ' + document.body.offsetHeight);
        if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight)      {
            console.log('here!');
        }
    });
    

    https://jsfiddle.net/jzrgmeqg/1

    【讨论】:

    • @McGyverBasaya 当然,您只需要在满足条件时更改您希望显示/隐藏的任何内容的显示属性。也可能想添加一个 else 条件以在它们向上滚动时将其更改回来
    • 感谢 aw04 我需要的某些部分,但是是否可以根据上面的代码修复第一列的位置?正如我上面的问题,我只想显示第一列,然后在用户到达页面底部时显示第二列。这可能吗?
    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多