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

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

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. Coablt strike官方教程中文译版本

    安装和设置 系统要求 Cobalt Strike的最低系统要求 2 GHz +以上的cpu 2 GB RAM 500MB +可用空间 在Amazon的EC2上,至少使用较高核数的CPU(c1.medi ...

  2. A1080. Graduate Admission

    It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...

  3. A1095. Cars on Campus

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

  4. 【模板】Splay

    Splay 均摊复杂度证明见此处 \(\rightarrow\) 链接 代码如下 #include <bits/stdc++.h> using namespace std; const i ...

  5. 关于:HTTP Header -> Content-Type: text/plain Cache-Control: no-cache IE浏览器弹出错误下载对话

    下午遇到一个很奇怪的现象,一个网址: http://192.168.1.3/login?action=a&fr=b.com 注意网址后面的参数形式,action参数在前,最后一个参数值的尾部含 ...

  6. PHP自动加载(__autoload和spl_autoload_register)

    一:什么是自动加载 我们在new出一个class的时候,不需要手动去require或include来导入这个class文件,而是程序自动帮你导入这个文件不需要手动的require那么多class文件了 ...

  7. 学习windows编程 day4 之 设置画刷

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  8. FASTREPORT COM/ActiveX报表如何保存到C++项目中?

    可以的. VC++ : ... IStream * pStream;CreateStreamOnHGlobal(NULL, true, &pStream);pStream->AddRef ...

  9. android 简单文件操作

    1.布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  10. js 运动框架及实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...