简体   繁体   English

如何使滚动页脚固定然后返回滚动

[英]How to make a scroll footer to fixed then back to scroll

在此处输入图片说明

Hello all, I have seen all of these sticky footer script and sticky sidebar scripts! 大家好,我已经看过所有这些粘性页脚脚本和粘性侧栏脚本! what I want is something similar, I have a long page with a footer and then more content below the footer, I would like the page to only show the footer when the content reaches the footer, once the footer shows on the screen, it gets fixed to the bottom so I can keep on scrolling the page and view the "more content" and when scrolling back up, the footer detaches from fixed at the bottom back to normal! 我想要的是类似的东西,我有一个长页脚的页面,然后在页脚下面有更多内容,我希望页面仅在内容到达页脚时显示页脚,一旦页脚在屏幕上显示,它就会固定在底部,因此我可以继续滚动页面并查看“更多内容”,并且在向上滚动时,页脚会从固定在底部的位置分离回正常!

I have attached a screenshot to maybe help explaining it better! 我附上了屏幕截图,可能有助于更好地解释它!

Well it requires some steps... 好吧,这需要一些步骤...

  • Get how much window has scrolled. 获取滚动了多少窗口。
  • check if it has scrolled beyond the point we want our footer to start acting as fixed. 检查它是否已经滚动到我们希望页脚开始固定的点之外。
  • That point is a sum of window height - footer height. 这就是窗口高度-页脚高度的总和。
  • if that point is again gone back to then make it as it was. 如果该点又回到原来的位置,则保持原样。
  • Add function to scroll event. 添加功能滚动事件。 So which check everything and does the work if needed. 因此,检查所有内容并在需要时进行工作。

See demo here : http://jsfiddle.net/techsin/MgQQm/2/embedded/result/ 在此处查看演示http : //jsfiddle.net/techsin/MgQQm/2/embedded/result/

See code here: http://jsfiddle.net/techsin/MgQQm/2/ 在此处查看代码: http : //jsfiddle.net/techsin/MgQQm/2/

$footer = $('#footer');
$win = $(window);
var ipos = $footer.offset().top;
var wpos, space, width;
width = $footer.width();

function h(e) {
    wpos = $win.scrollTop();
    space = $win.height() - $footer.height();
    if ((wpos + space) > ipos) {
        $footer.addClass('fixed');
        $footer.width(width);
    } else {
        $footer.removeClass('fixed');

    }
}


$(window).scroll(h);
 <div id="content"></div>
 <div id="moreContent"></div>
 <div id="footer"></div>

css 的CSS

 #content {
   height:1000px;
   width:400px;
   position: relative;
   z-index: 2;
   background-color:red;
}
#moreContent{
   height:500px;
   width:450px;
   margin:0px 0px 100px 0px;
   position: relative;
   z-index: 0;
   background-color:yellow;
 }
#footer {
   position: fixed;
   bottom: 0;
   width:400px;
   left: 8px;
   right: 0;
   height: 100px;
   z-index: 1;
   background-color:blue;
}

demo 演示

In css itself before, you may keep it to position:fixed; 在CSS之前,您可以将其保持在position:fixed; .

Or use JQuery for that, 或为此使用JQuery,

$('footer').css({position:'fixed'});

Or pure js, 还是纯js,

document.getElementByTagName('footer').style.position = 'fixed';

To make it scrollable later use jQuery, 要使其在以后滚动,请使用jQuery,

 $('footer').css({position:'relative'});

Or use javascript, 或使用javascript,

 document.getElementByTagName('footer').style.position = 'relative';

Your css for footer element 您的CSS页脚元素

.sticky {
    width: 100%;
    position:fixed;
    background: #F6D565;
    padding: 25px 0;
    top:700px;
    text-transform: uppercase;
  }

For demo chk this JSFIDDLE 对于演示chk,此JSFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM