您的位置:首页 > Web前端 > JQuery

jQuery里面的$(window).height()和$(document).height()的区别

2015-06-22 19:46 597 查看
我们要知道,$(window).height(),获取的是窗口的可视区域的高度,而$(document).height();获取的是文档的高度。

所以当你改变窗口的时候,$(window).height()会随着窗口的改变而改变,但$(document).height()是不变的。

很多时候,我们要使元素居中,此时我们可以写一个插件命名为jQuery.center.js,jQuery.fn.center = function(){

this.css({

"position":"absolute",

"top":(($(window).height()-this.height())/2+$(window).scrollTop())+"px",

"left":(($(window).width()-this.width())/2+$(window).scrollLeft())+"px"

});

return this;

}

此时可以在文件中引入jQuery.center.js插件,然后对某个对象调用该方法,例如:$("div").center();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: