Scrolling with fixed div layout and min-widths

前端 未结 7 1494
情歌与酒
情歌与酒 2021-01-04 19:03

\"Desired

I\'ve been trying to layout this page, but for the life of me, can\'t seem to get it to wor

7条回答
  •  爱一瞬间的悲伤
    2021-01-04 19:24

    I think this may work for you...

    Working Example

    JS:

    $(window).scroll(function () { // on scroll
        var win = $(window);
        var title = $('.title');
        var winWidth = $(window).innerWidth();
        var titleWidth = title.width();
        if (win.scrollLeft() >= titleWidth - winWidth) { // if scrolled to the right further than .title is wide minus the window's width
            title.addClass('fixed'); // add the fixed class
            title.offset({ //keep the title bar at the top 
                top: win.scrollTop(),
            });
        } else { // if not
            title.removeClass('fixed'); // removed class fixed
            title.offset({ // keep the title bar at the top anyway
                top: win.scrollTop(),
            });
        }
    });
    

    CSS:

    * {
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }
    html, body {
        width: 1024px;
        height:100%
    }
    .title {
        background:red;
        position: absolute;
        z-index:2;
        min-width: 100%;
    }
    #content {
        background: blue;
        padding-top:50px;
        min-width:1024px;
        min-height:100%;
    }
    .fixed {
        position: fixed;
        right: 0;
    }
    

    API documentation:

    .scroll()
    .innerWidth()
    .width()
    .scrollLeft()
    .offset()
    .addClass()
    .removeClass()

提交回复
热议问题