简体   繁体   English

位置:固定导航停止在特定div的末尾 - 视差滚动和javascript

[英]position:fixed navigation stop at end of specific div - parallax scrolling & javascript

I have a vertically oriented vertical navigation bar, that I would like to make stop at the end of #contact . 我有一个垂直方向的垂直导航栏,我想在#contact结束时#contact It will need to resume scrolling again if the user scrolls back up. 如果用户向上滚动,则需要再次继续滚动。 What is the best way to achieve this? 实现这一目标的最佳方法是什么?

javascript being used: 正在使用的javascript:

$(function() {
            $.fn.scrollBottom = function() {
                return $(document).height() - this.scrollTop() - this.height();
            };

            var $el = $('#nav>div');
            var $window = $(window);
            var top = $el.parent().position().top;

            $window.bind("scroll resize", function() {
                var gap = $window.height() - $el.height() - 10;
                var visibleFoot = 340 - $window.scrollBottom();
                var scrollTop = $window.scrollTop()

                if (scrollTop < top + 10) {
                    $el.css({
                        top: (top - scrollTop) + "px",
                        bottom: "auto"
                    });
                } else if (visibleFoot > gap) {
                    $el.css({
                        top: "auto",
                        bottom: visibleFoot + "px"
                    });
                } else {
                    $el.css({
                        top: 0,
                        bottom: "auto"
                    });
                }
            }).scroll();
        });

jsfiddle 的jsfiddle

I believe this is the code you are looking for: 我相信这是您正在寻找的代码:

$(function() {
    var $Nav = $('#Nav');
    var $window = $(window);
    var $contact = $('#contact');
    var maxTop = $contact.offset().top + $contact.height() - $Nav.height();
    window.navFixed = 1;

    $window.bind("scroll resize", function() {
        var currentTop = $window.scrollTop();
        if (currentTop <= maxTop && window.navFixed == 0) {
            $Nav.css({
                position: 'fixed',
                top: '5%'
            });
            window.navFixed = 1;
        } else if (currentTop > maxTop && window.navFixed == 1) {
            $Nav.css({
                position: 'absolute',
                top: maxTop
            });
            window.navFixed = 0;
        }
    }).scroll();
});

The #Nav element contains the CSS you had originally specified: position: fixed; top: (...) #Nav元素包含您最初指定的CSS: position: fixed; top: (...) position: fixed; top: (...) . position: fixed; top: (...) When the document is ready, the variable maxTop is calculated based on the #contact element's top and height. 文档准备就绪后,变量maxTop将根据#contact元素的top和height计算。

On the scroll and resize event, the variable currentTop is calculated as the current scroll position. scrollresize事件中,变量currentTop被计算为当前滚动位置。 If this value is lower than maxTop , then #Nav is set to the original CSS; 如果此值低于maxTop ,则#Nav设置为原始CSS; if the value is higher, new CSS styles are applied: position: absolute; top: maxTop; 如果值更高,则应用新的CSS样式: position: absolute; top: maxTop; position: absolute; top: maxTop;

window.navFixed is used to prevent the CSS to be constantly updated while scrolling. window.navFixed用于防止在滚动时不断更新CSS。 I'm sure that bit can be improved, however, it demonstrates its purpose. 我确信这一点可以改进,但它证明了它的目的。

Check out the JSFiddle for the full HTML.. 查看JSFiddle以获取完整的HTML ..

PS. PS。 There's a minor bug in your code, where #Nav refers to the <ul> element, rather than the <nav> element. 您的代码中存在一个小错误,其中#Nav引用<ul>元素,而不是<nav>元素。 However, the moving element is the <ul> , when it should be <nav> . 但是,移动元素是<ul> ,它应该是<nav>

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

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