设置靠近 水平居中的主体内容Div 的 左侧位置固定的Div

示例效果:

1.主体内容的divMain 水平居中;

2.divLeft 靠近divMain ,位置固定,不随垂直滚动条而动;

 

 

相关代码:

<html>
<head runat="server">
    <title>设置靠近主体内容的左侧固定位置的Div</title>

<style>
#divLeft
{
    position:fixed;/* 固定位置 */
    top:200px;
    border:solid 1px #ccc; 
    width:150px;
    height:500px;
}    

#divMain{
     width:918px;
     height:2000px;
     border:solid 1px #ccc;
     margin:0 auto;/* 水平居中 */

}

</style>



</head>
<body  onresize="setDivLeftPosition();" >

<div id="divMain">

</div>

<div id="divLeft">
    左侧固定位置
</div>


</body>
</html>

<script  type="text/javascript">
    //动态调整左侧Div的位置
    function setDivLeftPosition() {
        var divMain = document.getElementById("divMain");
        document.getElementById("divLeft").style.left = (divMain.offsetLeft - 155) + "px";
    }
    setDivLeftPosition();


    //        var top, left;
    //        if (div.currentStyle) { left = div.currentStyle.left; top = div.currentStyle.top; }
    //        else if (window.getComputedStyle) { left = window.getComputedStyle(div, null).left; top = window.getComputedStyle(div, null).top; }
    //        alert(left + "," + top);


</script>

 

转载于:https://www.cnblogs.com/freeliver54/p/3625099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值