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

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

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. PendingIntent的使用

    1, 构造intent Intent mIntent = new Intent("android.intent.action.MAIN"); ComponentName comp ...

  2. A1069. The Black Hole of Numbers

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  3. 【洛谷P1379】八数码难题 状压bfs

    对于这道题来说,每个时刻的状态是整个棋盘所有棋子的位置,即:任何一个棋子位置发生了移动,都会使得状态转移. 因此,需要采取将整个状态作为广搜的搜索对象,进行状态压缩.采用哈希得到每个状态的对应的数值, ...

  4. configParse模块(二十七)

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...

  5. zookeeper配置

    原文链接:https://www.cnblogs.com/yuyijq/p/3438829.html 前面两篇文章介绍了Zookeeper是什么和可以干什么,那么接下来我们就实际的接触一下Zookee ...

  6. Log4j 2X 日志文件路径问题

    关于路径问题网上说啥的都有,但是也不能说人家错,只能说不适合你这个. 一开始,我用的   ${webapp.root} <RollingFile name="rollingFileSy ...

  7. 窗体焦点监听事件WindowFocusListener

      public class Demo extends JFrame { JLabel label;//定义变量,以便在自定义方法类中调用 public Demo() { setBounds(100, ...

  8. HDU - 4901 The Romantic Hero(dp)

    https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标. ...

  9. 第三节:工厂+反射+配置文件(手写IOC)对缓存进行管理。

    一. 章前小节 在前面的两个章节,我们运用依赖倒置原则,分别对 System.Web.Caching.Cache和 System.Runtime.Cacheing两类缓存进行了封装,并形成了ICach ...

  10. 去除inline-block出现间距的几种方法

    display:inline-block,简单来说就是将对象呈现为inline对象,但是对象的内容作为block对象呈现,之后的内联对象会排列在同一行 比如两个input,默认中间会产生一些间距 &l ...