简单js小效果——有版心贴靠栏固定定位怎么不随窗口大小的改变而改变位置


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>fix</title>
    <style>
        *{ margin: 0; padding: 0; }
        .main{ width: 1200px; margin: 0 auto; height: 500px; border: 1px solid #ccc; }
        .hh{ width: 15px; height: 60px; border: 1px solid #ccc; position: fixed;box-sizing: border-box; }
    </style>
</head>
<body>
    <div class="main"></div>
    <div class="hh" style='right:342px;top:20px;'></div>
</body>
<script>
    var hh = document.querySelector('.hh');
    window.onload = function(){
        var win = window.innerWidth;
        hh.style.right=(win-1200)/2-18 +'px'
    }
    window.onresize=function() {
        var win = window.innerWidth;
        hh.style.right=(win-1200)/2-18 +'px'
    }
</script>
</html>
窗口宽度+版心宽度 除以 2 减去 侧边栏的宽度


作者:Vam的金豆之路

主要领域:前端开发

我的微信:maomin9761

微信公众号:前端历劫之路