Css固定div区域

时间:2012-10-30 08:27:50

标签: css

大家好我已经修好了div区域。我将这些固定的div放在一个div中,我称之为“page”,这里是css:

.page {
    width: 964px;
    margin-top:6px;
    margin-left: auto;
    margin-right: auto;
    background-image:url(../images2/images/orta_alan_bg_GOLGE.png);
    background-repeat:repeat-y;
}

但是当我用不同的分辨率检查我的设计时,固定的div区域远离我的“page”div

这里是固定的div css:

#rocket_left
{
  width:127px;
  height:148px;
  background-image:url(../../images2/images/tapinak_resim.jpg);
  top:244px;
  left: 5.4%;
  position:fixed;
}

#rocket_left_desc
{
 background-image:url(../../images2/images/bg_sol_bslk_tpnk.png); 
  width:130px;
  height:335px;
  top:385px;
  left:70px;
  position:fixed;
}

2 个答案:

答案 0 :(得分:1)

我认为你的意思是让火箭相对于页面而不是浏览器文档定位。在这种情况下,只需更改定位说明:

.page {
    position: relative; /* Position context for the rockets */
    width: 964px;
    margin-top:6px;
    margin-left: auto;
    margin-right: auto;
    background-image:url(../images2/images/orta_alan_bg_GOLGE.png);
    background-repeat:repeat-y;
}

#rocket_left
{
  width:127px;
  height:148px;
  background-image:url(../../images2/images/tapinak_resim.jpg);
  top:244px;
  left: 5.4%;
  position: absolute; /* Absolute positionning within the page */
}

#rocket_left_desc
{
 background-image:url(../../images2/images/bg_sol_bslk_tpnk.png); 
  width:130px;
  height:335px;
  top:385px;
  left:70px;
  position: absolute; /* Absolute positionning within the page */
}

答案 1 :(得分:1)

"But when I check my design with different resolution fixed div area go far from my "page" div"

这是因为当您将元素的位置设置为固定时,其位置是相对于屏幕计算的,在您的情况下,元素将始终位于屏幕上的top:244px; left: 5.4%;top:385px; left:70px;

我的建议是绝对定位它们(​​使用position:absolute;)然后检测(使用JavaScript),如果查看器屏幕的宽度大于文档的宽度(在你的情况下,那将是964px),如果是,则将火箭的位置改为position:fixed;

这里是我的建议的jQuery代码:

<script type="text/javascript">
    $(document).ready(function(){
        if($(window).width()>=964){
            $('#rocket_left').css('position','fixed');
            $('#rocket_left').css('position','fixed');
        }
    });
</script>

这是你应该使用的CSS(由MarvinLabs发布):

.page {
    position: relative; /* Position context for the rockets */
    width: 964px;
    margin-top:6px;
    margin-left: auto;
    margin-right: auto;
    background-image:url(../images2/images/orta_alan_bg_GOLGE.png);
    background-repeat:repeat-y;
}

#rocket_left
{
  width:127px;
  height:148px;
  background-image:url(../../images2/images/tapinak_resim.jpg);
  top:244px;
  left: 5.4%;
  position: absolute; /* Absolute positionning within the page */
}

#rocket_left_desc
{
 background-image:url(../../images2/images/bg_sol_bslk_tpnk.png); 
  width:130px;
  height:335px;
  top:385px;
  left:70px;
  position: absolute; /* Absolute positionning within the page */
}