Use full width excluding overflow scrollbar with “position: absolute”

前端 未结 2 1047
轮回少年
轮回少年 2021-01-18 06:03

I would like to have a small red div with full width at a fixed top position, inside another div that has overflow: scroll. I hope the jsFiddle makes it clear:

2条回答
  •  感动是毒
    2021-01-18 06:38

    HTML

    foo
    foo
    foo
    foo

    CSS

    * { margin: 0; padding: 0 }
    body {
        padding-top: 30px;
    }
    
    div.main {
        height: 200px;
        width: 100%;
        overflow-y: scroll;
        position: absolute;
        z-index: 50;
        background: -webkit-gradient(linear, center top, center bottom, from(white), to(rgba(255,255,255,0)));
    }
    
    div.item {
        border: 1px solid black;
        margin: 20px;
        padding: 20px;
    }
    
    div.scroller {
        height: 20px;
        width: 100%;
        position: absolute;
        z-index: 100;
        overflow: hidden;
    }
    
    div.banner-wrapper {
        background: transparent;
        position: relative;
        height: 20px;
        overflow-y: scroll;
        left: 0;
        margin-right: -20px;
    }
    div.banner {
        height: 20px;
        background: -webkit-gradient(linear, center top, center bottom, from(white), to(rgba(255,255,255,0)));;
        margin-left: 20px;
        margin-right: 40px;
    }
    

    Development version: http://jsfiddle.net/mCYLm/13/
    Final version: http://jsfiddle.net/mCYLm/14/

    Works with zooming and variable viewport width.
    ! BUG: Scrollbar button from the right top is not accessable/clickable.

    Tested in:

    • IE6,7,8,9 (windows)
    • FF11 (Windows)
    • Google Chrome 18 (ubuntu)
    • Safari 5.1 (OSX)

提交回复
热议问题