简体   繁体   English

用户将页面滚动到底部时页脚向上滑动

[英]Footer slide up when user scrolls page to bottom

I have a footer fixed on the bottom of my webpage but I don't want it to be visible until the user reaches the bottom of the page, then I want it to slide up. 我的网页底部固定有页脚,但我不希望在用户到达页面底部之前不显示页脚,然后才向上滑动。

The footer is 200px height and fixed to the bottom and the body has a 200px padding a the bottom. 页脚高度为200px,固定在底部,主体底部填充200px。

CSS: CSS:

footer {
    min-height: 200px;
    background-color: #bdf207;
    position: fixed;
    width: 100%;
    left: 0;
    right: 0;
    bottom: 0;
 }

body {
    padding-bottom: 200px;
}

The jQuery I'm using is as follows but it is not working: 我正在使用的jQuery如下,但它不起作用:

$(window).scroll(function() {
    // when user starts scrolling trigger function
    var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
    // if the bottom of the page reaches 250px remaining of the document
    if (scrollBottom > 250){
        // slide the footer up
        $('footer').slideUp('slow');
    }
});

I would approach it a little differently. 我会有所不同。 I would take advantage of the absolute positioning and just hide it below the document until you scroll to the appropriate position. 我会利用绝对位置的优势,将其隐藏在文档下方,直到滚动到适当的位置为止。

 $(window).scroll(function() { var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop(), currentPos = parseInt($('footer').css('bottom')); if(currentPos === -200 || currentPos === 0) if (scrollBottom < 250){ $('footer').stop().animate({bottom: 0}); } else { $('footer').stop().animate({bottom: -200}); } }); 
 footer { min-height: 200px; background-color: #bdf207; position: fixed; left: 0; right: 0; bottom: -200px; } body { padding-bottom: 200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <footer>Footer</footer> 

The slideUp() function hides the element by reducing its height to 0 or whatever the min-height is set to and then setting display: none; slideUp()函数通过将元素的高度减小到0或将min-height设置为任意值,然后设置display: none;来隐藏元素display: none; .

Calling it on an element that is 200px in height with a min-height of 200px will do nothing to the element height-wise and then set display: none; 在高度为200px且最小高度为200px的元素上调用它不会对元素的高度产生任何影响,然后将display: none;设置为display: none;

.footer {

  position: absolute;

  right: 0;

  bottom: 0;

  left: 0;

  padding: 1rem;

  background-color: #efefef;

  text-align: center;

}

Then use jQuery to slide up 然后使用jQuery向上滑动

You can refer below link for help 您可以参考下面的链接寻求帮助

http://jsfiddle.net/nathanbweb/zMsYq/ ` http://jsfiddle.net/nathanbweb/zMsYq/ `

jQuery(function($) {

        var slide = false;

        var height = $('#footer_content').height();

        $('#footer_button').click(function() {

            var docHeight = $(document).height();

            var windowHeight = $(window).height();

            var scrollPos = docHeight - windowHeight + height;

            $('#footer_content').animate({ height: "toggle"}, 1000);

            if(slide == false) {

                if($.browser.opera) {

                    $('html').animate({scrollTop: scrollPos+'px'}, 1000);

                } else {

                    $('html, body').animate({scrollTop: scrollPos+'px'}, 1000);
                }

                slide = true;
            } else {
                slide = false;
            }
        });
    });

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

相关问题 用户滚动到页面底部时,向上滑动页脚-闪烁 - Slide up footer once user scrolls to bottom of page - flickering 当用户滚动到页面底部时显示页脚 - Show footer when user scrolls to the bottom of the page 用户滚动到页面底部时的性能问题 - Performance problems when user scrolls to bottom of the page 当用户滚动到页面底部时无法检测 - Cannot Detect When User Scrolls to the Bottom of Page 如何创建固定在屏幕底部的页脚,但当用户向下滚动时,它不再是固定页脚? - How do I create a footer that is fixed at the bottom of the screen but when the user scrolls down it is no longer fixed footer? 当用户向上滚动时暂停 javascript setInterval,然后当用户滚动回底部时重新启动 setInterval - Pause javascript setInterval when user scrolls up, then restart setInterval when user scrolls back to bottom 用户滚动到页面底部时超时后的addClass - addClass after timeout when user scrolls to bottom of page 当用户滚动时,我希望div在页面底部滚动 - I want a div to scroll at the bottom of the page when the user scrolls 当用户滚动到页面底部时,如何启动jQuery事件? - How to start a jQuery event when the user scrolls to the bottom of the page? 当用户滚动到页面底部附近时调用函数 - call a function when user scrolls near bottom of page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM