简体   繁体   English

固定位置的绝对效果

[英]Absolute effect in fixed position

Based on this code: 基于此代码:

HTML: 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: CSS:

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

JS: JS:

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

If I give the css directly in css not in the js, how can I still make it work the same way ? 如果我直接在CSS中而不是在js中给css提供服务,我如何仍可以使其以相同的方式工作?

Fiddle 小提琴

讨厌这么说,但是在普通CSS2,3中是不可能的。

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

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