JS打开新窗口并居中显示

一、Js打开新窗口并居中显示

HML部分:

<button onclick="openNewWindow('https://www.baidu.com')">打开百度</button>

JS部分:

/*
    功能:打开新得浏览器窗口并居中显示
    参数:
        url:需要打开的url地址
        name:打开的窗口名称,可以为null
        customWidth:窗口的宽度
        customHeight:窗口的高度
*/

function openNewWindow(url, name, customWidth, customHeight) {
    // 设置宽高的默认值
    if(!customWidth){
        customWidth=window.screen.width/1.5;
    }
    if(!customHeight){
        customHeight=window.screen.height/1.5;
    }
    //window.screen.height获得屏幕的高,window.screen.width获得屏幕的宽
    var iTop = (window.screen.height - 30 - customHeight) / 2; //获得窗口的垂直位置;
    var iLeft = (window.screen.width - 10 - customWidth) / 2; //获得窗口的水平位置;

    window.open(url, name, 'height=' + customHeight + ',,innerHeight=' + customHeight + ',width=' + customWidth + ',innerWidth=' +
    customWidth + ',top=' + iTop + ',left=' + iLeft +
        ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}

效果:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优雅哥cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值