jquery实现div水平垂直居中例子

jquery统计出来

 代码如下 复制代码

if($("#cont1").css("position")!="fixed"){
$("#cont1").css("position","absolute");
var dw = $(window).width();
var ow = $("#cont1").outerWidth();
var dh = $(window).height();
var oh = $("#cont1").outerHeight();
var l = (dw - ow) / 2;
var t = (dh - oh) / 2 > 0 ? (dh - oh) / 2 : 10;
var lDiff = $("#cont1").offset().left - $("#cont1").position().left;
var tDiff = $("#cont1").offset().top - $("#cont1").position().top;
l = l + $(window).scrollLeft() - lDiff;
t = t + $(window).scrollTop() - tDiff;
$("#cont1").css("left",l + "px");
$("#cont1").css("top",t + "px");
}

jquery+css 实现居中

通过css创建一个水平居中和垂直居中的div是一件比较麻烦的事情,您必须事先知道另外一个div的尺寸:

 代码如下 复制代码

.className{
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}

通过jQuery实现水平居中和垂直居中前面已经提到过了,css的方法只适用于有固定尺寸的div,所以到jQuery发挥作用了:

 代码如下 复制代码

$(window).resize(function(){
$('.className').css({
position:'absolute',
left: ($(window).width() - $('.className').outerWidth())/2,
top: ($(window).height() - $('.className').outerHeight())/2 + $(document).scrollTop()
});
});
//初始化函数
$(window).resize();

此外在页面载入时,就需要调用resize()。

$(function(){
    $(window).resize();
});

此方法的好处就是不需要知道DIV的具体宽度和高度大小,直接用jQuery就可以实现水平和垂直居中,而且兼容各浏览器,这个方法在很多的弹出层效果中应用。

通过css实现水平居中和垂直居中

例子

 代码如下 复制代码

<body>
    <div class="pos">
        <div class="boxA">测试用的内内容测测试用的内容</div>
    </div>
</body> 

*{ margin:0; padding:0;}
body{background:#000000;}
.pos{float:left;margin-left:50%; background:#33CC33; padding:10px;}
.boxA{position:relative;right:50%;background:#096;}

总结:外层的定位:需要是自适应宽度的;(float:left 或者 position:absolute;)

内层的位定:需要是自动100%宽度的(相对于外层100%);

时间: 2024-08-01 22:07:14

jquery实现div水平垂直居中例子的相关文章

jQuery让DIV水平垂直居中的代码

通过css创建一个水平居中和垂直居中的div是一件比较麻烦的事情,您必须事先知道另外一个div的尺寸:  代码如下 复制代码 .className{  width:300px;  height:200px;  position:absolute;  left:50%;  top:50%;  margin:-100px 0 0 -150px; } 通过jQuery实现水平居中和垂直居中前面已经提到过了,css的方法只适用于有固定尺寸的div,所以到jQuery发挥作用了  代码如下 复制代码 jQ

jquery计算出left和top,让一个div水平垂直居中的简单实例_jquery

实例如下: if($("#cont1").css("position")!="fixed"){ $("#cont1").css("position","absolute"); var dw = $(window).width(); var ow = $("#cont1").outerWidth(); var dh = $(window).height(); var o

css div水平垂直居中几个例子

一.未知宽度水平居中  代码如下 复制代码 <!doctype html> <html> <head> <meta charset="utf-8"> <title>未知宽高div在页面内水平垂直居中</title> <style> *{margin:0; padding:0;} body,html{overflow:hidden; height:100%;} .box {height: 100%; ov

css实现在div水平垂直居中与图片水平居中的效果

div居中的完美解决方案! (水平垂直居中) 1,关于居中使用css为:  代码如下 复制代码 position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2; 对于ie6,只能把position:改成absolute; 图片在div中居中 利用图片的margin属性将图片水平居中,利用div的padding属性将图片垂直居中. 方法一: css代码如下:  代码如下 复制代码 div {width:300px; h

jquery实现DIV水平和垂直居中代码

经常有朋友问到有关DIV水平居中和垂直居中的问题,今天就把之前写过的一个jquery实现方法分享出来: 记住,jquery.js不要引用2.0.0版本以上的,要引用1.10.1左右版本的,不然IE9以下不兼容: 兼容各种浏览器: jquery方法  代码如下 复制代码 //页面加载完就执行这个方法 $(function(){  $(".mydiv").css({         position: "absolute",         left: ($(windo

CSS实现DIV水平 垂直居中-1

水平大家都知道,一般固定宽度给个margin:0 auto:就可以了.下面实现水平垂直都居中 HTML <div class="parent"> </div> css html,body{ width: 100%; height: 100%; } .parent{ width: 750px; height: 400px; background: orange; /*水平居中*/ margin: 0 auto; position: relative; top: 5

CSS3中DIV水平垂直居中-2(3)

用到CSS3中display的新属性. HTML <div class="parent"> </div> CSS html,body{ width: 100%; height: 100%; } /*方法二*/ body{ display: flex; align-items: center;/****水平居中****/ justify-content: center;/*垂直居中*/ } .parent{ width: 750px; height: 400px;

CSS让DIV水平垂直居中(兼容IE/FF/google浏览器)

实例  代码如下 复制代码 <style type="text/css"> body { margin: 0; padding: 0; background: #1d1d1d; font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; } h1 { font: 4em Georgia, "Times New Roman", Times, serif; color: #fff;

div css控制DIV水平垂直居中

 代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="te