jQuery制作元素在屏幕中水平垂直居中效果
jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px"); this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px"); return this; } //Use the above function as: $(element).center();
另外还有一些其他的实现方法
< !-- html --> <div id="outer"> <div id="middle"> <div id="inner"> any text <br> <b>any height </b><br> any content, for example generated from DB <br> everything is vertically centered <br> </div> </div> </div> < !-- CSS --> html{height: 100%;} body {height: 100%;} #outer {height: 600px; overflow: visible;width: 100%;position: relative;} /* or without overflow */ #outer[id] {display: table; position: static;} #middle {position: absolute; top: 50%;} /* for explorer only*/ #middle[id] {display: table-cell; vertical-align: middle; width: 100%;position:static;} #inner {position: relative; top: -50%} /* for explorer only */ /* optional: #inner[id] {position: static;} */
相关推荐
冰蝶 2020-01-10
小仙儿 2020-06-25
lcqin 2020-08-11
yaodilu 2020-08-03
lanzhusiyu 2020-07-18
xiaohuli 2020-06-11
CaiKanXP 2020-06-09
songfens 2020-06-08
CSSEIKOCS 2020-05-19
aSuncat 2020-05-10
Phoebe的学习天地 2020-05-09
nicepainkiller 2020-05-05
amwayy 2020-05-01
AlisaClass 2020-04-14
coulder 2020-04-08
wangjie 2020-02-26
xiaohuli 2020-02-23