在一些页面中,我们总会遇到一些弹窗不居中的时候,还要根据浏览器的大小来调整弹窗的弹出位置,

之前我也遇到这样的问题,现在我把我知道的呈现给大家

css样式

.windowBox{

width:500px;

}

.mid-tanBox{

position: fixed;  top: 50%;  left: 50%;    margin-left: -250px;  background: #fff; border:1px solid red; display:none

}

html代码

<div class='windowBox'>

<div class ="mid-tanBox"></div>

<div class="jclick">弹窗</div>

</div>

jQuery代码自动:

$(".jclick").click(function(i){

var height = $(".mid-tanBox").height();

var eheight = height/2;

$(".mid-tanBox").eq(i).css("margin-top","-eheight");

})

额外:-JS

<style>
*{margin:0px;padding:0px}
.all-tanch{ background:blue; position: fixed;  display:none; color: #fff; padding:50px;}
.wraper{ width:600px; background:#333; color: #fff; position:relative; margin:30px;}
.sonone{background:red; display:none;position:absolute;}
</style>
<Script type="text/javascript" src="jquery.js"></script>
<!--针对浏览器窗口垂直居中-->
<Script type="text/javascript">
function popup(poName){
    var scrollHeight = $(document).scrollTop();//获取当前窗口距离页面顶部高度
    var windowHeight = $(window).height();//获取当前窗口高度
    var windowWidth = $(window).width();//获取当前窗口宽度
    var popupHeight = popupName.height();//获取弹出层高度
    var popupWeight = popupName.width();//获取弹出层宽度
    alert(windowHeight);
    posiTop = (windowHeight)/2;
    posiLeft = (windowWidth)/2;
    poName.css({"left": posiLeft + "px","top":posiTop + "px","display":"block"});//设置position
}
$(function(){
    $(".clickme01").click(function(){
        popup($(".all-tanch"));
        $(".wraper").hide();
    });

});
</script>

<!--针对当前层 窗口垂直居中-->
<Script type="text/javascript">
function middleModle(selectorName){   //参数: 定义的class或ID,,其他
    var fatherHeight = $(".wraper").height();//获取当前窗口高度
    var fatherWidth = $(".wraper").width();//获取当前窗口宽度
    var tanHeight = selectorName.height();//获取弹出层高度
    var tanWeight = selectorName.width();//获取弹出层宽度
    var poTop = (fatherHeight - tanHeight)/2;
    var poLeft = (fatherWidth - tanWeight)/2;
    selectorName.css({"left": poLeft + "px", "top":poTop + "px", "display":"block"});//设置position位置
}
$(function(){
    $(".clickme02").click(function(){
        $(".wraper").show();
        middleModle($(".sonone"));
    });
});
</script>

<input class="clickme01" type="button" value="针对浏览器窗口垂直居中"/>
<input class="clickme02" type="button" value="针对当前窗口垂直居中"/>
<div class="all-tanch">
     整个弹窗
</div>
<div class="wraper">
    我是父级层--
    <div class="sonone">大家好,我是弹窗,没有样式自己凑合着看吧!!!!</div>
</div>
 

js实现弹窗居中的更多相关文章

  1. Js控制弹窗实现在任意分辨率下居中显示

    弹窗居中比较烦人的是怎么才能在任意分辨率下实现居中显示.1,html部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  2. js解决弹窗问题实现班级跳转DIV示例

    js解决弹窗问题实现班级跳转DIV 1.js代码如下: <%--实现班级跳转DIV--%>  <div id="displayClassDiv" style=&q ...

  3. js写弹窗

    1.先来看弹窗的模样 点击“弹出窗口”后会弹出下面窗口 2.下面是实现弹出窗口的代码,其中引入的jquery一般自己有,没有的话可以从网上下载.tanchuang.js和tanchuang.css写在 ...

  4. js右下角弹窗代码(实测好用)

    实测好用的js右下角弹窗代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. js实现div居中

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. js广告弹窗

    生活中我们经常遇到一些烦人的广告页面,比方说弹窗,悬浮等等各种广告.有的同事甚至都下一个屏蔽广告插件到浏览器上.这样就防止了广告的干扰. 但是我们学前端的必须是要知道广告弹窗这个做的过程,甚至是它的原 ...

  7. js自定义弹窗

    <一>confirm弹窗 页面操作中常见需要确认操作. 例如:删除某条消息前需要确认是否删除. 页面中弹窗确认操作用到confirm消息对话框. JS代码 function del(){ ...

  8. python js 处理弹窗图片

    内置函数 : driver.execute_script() 2.自定义弹窗 由于alert弹窗不美观,现在大多数网站都会使用自定义弹窗,使用Selenium自带的方法就驾驭不了了,此时就要搬出JS大 ...

  9. jquery弹窗居中-类似alert()

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. A1039. Course List for Student

    Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists o ...

  2. Flash:使用FileReference上传在Firefox上遇到的问题终于解决了

    以前使用的是这样的一句话:var uploadURL:URLRequest = new URLRequest();uploadURL.url = "upload.asp"; 测试发 ...

  3. 走进JVM之一 自己编译openjdk源码

    想要深入了解JVM,就必须了解其实现机制.了解JVM实现的最好方法便是自己动手编译JDK.好了,让我们开始吧! 1.  准备工作 获取OpenJDK源码 本次编译选择的是OpenJDK7u,官方源码包 ...

  4. 【推荐】使用Ultrapico Expresso学习正则表达式

    Ultrapico Expresso是我工作中经常使用的一个非常强大的正则表达式构建.测试以及代码生成工具.它能够对你构建的正则表达式进行解析.验证,并输出解析结果,提供性能测试工具,支持C#.VB等 ...

  5. 怎样解决Myeclipse内存溢出?

    打开myeclipse 10安装目录下的myeclipse.ini文件 打开文件,将文件圈圈中的内容设置如下图: 上面是其中一种解决方案,下面介绍第二种解决方案 设置Default VM Argume ...

  6. HDFS集群PB级数据迁移方案-DistCp生产环境实操篇

    HDFS集群PB级数据迁移方案-DistCp生产环境实操篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 用了接近2个星期的时间,终于把公司的需要的大数据组建部署完毕了,当然,在部 ...

  7. secure CRT the remote system refused the connection 解决办法

    1.安装ssh服务器和客户端 apt-get install openssh-server apt-get install openssh-client 2.重启ssh /etc/init.d/ssh ...

  8. POJ - 1836 Alignment (动态规划)

    https://vjudge.net/problem/POJ-1836 题意 求最少删除的数,使序列中任意一个位置的数的某一边都是递减的. 分析 任意一个位置的数的某一边都是递减的,就是说对于数h[i ...

  9. QT_校园导航Update

    //MainWidget.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include " ...

  10. python 面试题--你能做出多少?

    python3中__get__,getattr,__getattribute__的区别 什么是 GIL 详细博客 GIL = Global Intercept Lock 全局解释器锁,任意时刻在解释器 ...