jQuery制作元素在屏幕中水平垂直居中效果
2016-03-31 22:35
656 查看
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;} */
转载自:http://www.9958.pw/post/jquery_center
相关文章推荐
- jquery中生成一个带cha的tag
- 引jquery入库
- jquery部分笔记
- 锋利的jQuery第2版学习笔记1~3章
- jQuery+css3侧边栏导航菜单
- Jquery ajax提交表单几种方法详解
- jQueryMobile(二)
- 【JQuery】:password表单密码选择器
- 【JQuery】:text表单文本选择器
- 获取浏览器的名称与版本信息
- 分享一个JQuery写的点击上下滚动的小例子
- 高效使用jQuery之选择器
- javascript中jQuery对象和dom对象
- 用jQuery—ui对表格进行修改数据
- 用Jquery做图片轮播
- JQuery选择器收藏
- jQuery.trigger() 函数详解
- jquery判断元素是否有某个属性
- jQuery的ajax()方法提交数组问题
- 从零开始学_JavaScript_系列(四)——jquery(基础,选择器,触发条件,动画,回调函数)