Fix header to top of page with horizontal scroll

僤鯓⒐⒋嵵緔 提交于 2019-12-12 00:29:34

问题


I have a navigation header at the top of every page. I'd like the user to be able to scroll the horizontal overflow with a scrollbar. I understand this isn't possible with just CSS, but can any help me with the JavaScript portion?

If this is too general, I'll post my project's code, but it's pretty extensive.

Ex:

| Example Header | About | Contact | Bl | (Cut off)

<------------------> (Scroll bar)


回答1:


You can use a version of my code:

function reposition(){
    var el = document.getElementById('header');
    var ScrollLeft = document.body.scrollLeft;
    if (ScrollLeft == 0)
    {
        if (window.pageXOffset)
            ScrollLeft = window.pageXOffset;
        else
            ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
    }
    el.style.left = "-" + ScrollLeft + "px";
}

Replace 'header' with your header's id.

Hope this helps!




回答2:


It's possible with CSS. Set your header to use overflow: auto;.

Example: http://jsbin.com/edopor/4/edit



来源:https://stackoverflow.com/questions/12291226/fix-header-to-top-of-page-with-horizontal-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!