固定位置的绝对效果

时间:2014-11-09 07:12:59

标签: javascript html css

基于此代码:

HTML:

<div style="width:1000px;height:1000px;">
    <div id="box1" class="box" style="left:20px;top:20px;">
        My position-x is fixed but position-y is absolute.
    </div>
    <div id="box2" class="box" style="left:20px;top:120px;">
        My position-x is absolute but position-y is fixed.
    </div>
    <div id="box3" class="box" style="left:20px;top:220px;">
        Im positioned fixed on both axis.
    </div>
</div>

CSS:

.box 
{
    width:400px;
    height:80px;
    background:gray;
    position:fixed;  
}

JS:

$(window).scroll(function(){
//box one
    var $win = $(window);
    $('#box1').css('top', 20 -$win.scrollTop());
    $('#box2').css('left', 20 -$win.scrollLeft());
});

如果我直接在css中给css而不是js,我怎么能以同样的方式使它工作?

Fiddle

1 个答案:

答案 0 :(得分:0)

讨厌说,但在纯CSS2,3中不可能。