首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >简单js小效果——有版心贴靠栏固定定位怎么不随窗口大小的改变而改变位置

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

作者头像
马克社区
发布2022-07-13 17:47:34
发布2022-07-13 17:47:34
3.8K0
举报
文章被收录于专栏:高端IT高端IT
代码语言:javascript
复制
<!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 减去 侧边栏的宽度

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120055510

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档