jQuery制作元素在屏幕中水平垂直居中效果
2016-07-09 00:00
253 查看
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
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- JavaScript 各种遍历方式详解
- 数组方法汇总
- jQuery Html控件基本操作(日常收集整理)
- jQuery插件实现文字无缝向上滚动效果代码
- jQuery菜单插件用法实例
- JQuery 初体验(建议学习jquery)
- 基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
- Jquery实现的table最后一行添加样式的代码
- jQuery实现向下滑出的平滑下拉菜单效果
- jQuery 练习[一] 学习jquery的准备工作
- jquery获得页面元素的坐标值实现思路及代码