弹出框居中的三种写法

1、通过(浏览器显示区域的宽高—弹出框的宽高)/ 2计算出弹出框定位的top和left

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>box-center-1</title>
</head>
<body>
<button id="open" onclick="openModal()">openModal</button>
<script type="text/javascript">
  function openModal(){
    var modalDom = document.createElement("div");
    //计算出弹出框定位的top值
    var iTop = (window.innerHeight - 400) / 2 + "px";
    //计算出弹出框定位的left值
    var iLeft = (window.innerWidth - 400) / 2 + "px";
    modalDom.style.cssText = "height:400px;width:400px;z-index:2;background-color:#7fbc39;color:#fff;text-align:center;line-height:400px;position:fixed;";
    modalDom.style.cssText += "top:" + iTop + ";left:" + iLeft;
    modalDom.innerText = "这是一个居中屏幕的弹框";
    document.body.appendChild(modalDom);
  }
</script>
</body>
</html>复制代码

2、这种方法在元素的宽高不固定时会全屏拉伸元素,在高度不固定靠子元素撑开自适应的情况下不适用。但是在宽高固定的情况下可以用,而且没有兼容性问题

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>box-center-2</title>
  <style>
    .box-center {
      height: 400px;
      width: 400px;
      position: fixed;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      margin: auto;
      background-color: #00b4cf;
      color: #fff;
      text-align: center;
      line-height: 400px;
    }
  </style>
</head>
<body>
<button id="open" onclick="openModal()">openModal</button>
<script type="text/javascript">
  function openModal(){
    var modalDom = document.createElement("div");
    modalDom.innerText = "这是一个居中屏幕的弹框";
    modalDom.className = "box-center";
    document.body.appendChild(modalDom);
  }
</script>
</body>
</html>复制代码

3、用top:50% left:50%定位后元素的左上角会处于页面中间,然通过transform:translateX(-50%) translateY(-50%)是CSS3的新写法,让元素相对于他自身的宽高反向移动。配合上面的代码就能实现居中定位,而且适用于不确定宽高的元素,但是在低版本IE不兼容

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>box-center-3</title>
  <style>
    .box-center {
      position:fixed;
      top:50%;
      left:50%;
      transform:translateX(-50%) translateY(-50%);
      background-color: #435466;
      color: #fff;
      text-align: center;
      line-height: 20px;
    }
  </style>
</head>
<body>
<button id="open" onclick="openModal()">openModal</button>
<script type="text/javascript">
  function openModal(){
    var modalDom = document.createElement("div");
    modalDom.innerText = "这是一个居中屏幕的弹框";
    modalDom.className = "box-center";
    document.body.appendChild(modalDom);
  }
</script>
</body>
</html>复制代码

基础知识点:

document.body.style.width 返回当前元素的宽度(元素宽度)

document.body.offsetWidth 返回当前元素的宽度(元素宽度+内边距)

document.body.clientWidth 返回当前元素的宽度(元素宽度+内边距+边框)

window.innerWidth 返回当前浏览器屏幕的可见区域的宽度(包含滚动条)

window.screen.availWidth 返回当前浏览器屏幕的可用宽度(通常与屏幕宽高一致)

window.screen.width 返回当前屏幕宽度(分辨率值) 

window.document.body.offsetHeight 返回当前网页高度

window.document.body.offsetWidth 返回当前网页宽度


转载于:https://juejin.im/post/5b8429296fb9a01a10595cc4

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值