jq 获取各个元素的宽度高度的方法

JS获取各种宽度、高度的简单介绍:
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
document.documentElement.scrollTop 垂直方向滚动的值
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量
以上主要指IE之中,FireFox差异如下:
IE6.0、FF1.06+:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
offsetWidth (width+padding+border)
假设 obj 为某个 HTML 控件。
obj.offsetTop 指 obj 距离上方或上层控件的位置,整型,单位像素。
obj.offsetLeft 指 obj 距离左方或上层控件的位置,整型,单位像素。
obj.offsetWidth 指 obj 控件自身的宽度,整型,单位像素。获取对象可见内容的宽度,不包括滚动条,不包括边框;
obj.offsetHeight 指 obj 控件自身的高度,整型,单位像素。
offsetWidth 与 style.width 的区别
一、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
二、offsetTop 只读,而 style.top 可读写。
三、如果没有给 HTML 元素指定过 top 样式,则 style.top 返回的是空字符串。
jQuery获取各种宽度、高度的简单介绍
- alert($(window).height()); //浏览器时下窗口可视区域高度
- alert($(document).height()); //浏览器时下窗口文档的高度
- alert($(document.body).height());//浏览器时下窗口文档body的高度
- alert($(document.body).outerHeight(true));//浏览器时下窗口文档body的总高度 包括border padding margin
- alert($(window).width()); //浏览器时下窗口可视区域宽度
- alert($(document).width());//浏览器时下窗口文档对于象宽度
- alert($(document.body).width());//浏览器时下窗口文档body的高度
- alert($(document.body).outerWidth(true));//浏览器时下窗口文档body的总宽度 包括border padding margin
- alert($(document).scrollTop()); //获取滚动条到顶部的垂直高度
- $('#outer-div')[0].scrollHeight
- //获取div的实际高度
- alert($(document).scrollLeft()); //获取滚动条到左边的垂直宽度
scrollTop为滚动条在Y轴上的滚动距离。
clientHeight为内容可视区域的高度。
scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。
从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。
jquery实现判断滚动条滚动到底部:
- <!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>
- <meta http-equiv="content-type" content="text/html;charset=utf-8">
- <title>下拉滚动条滚到底部了吗?</title>
- <script language="javascript" src="jQuery.js"></script>
- <script language="javascript">
- $(document).ready(function (){
- $('#scroll-top-msg').html($("#outer-div")[0].scrollTop);
- $('#scroll-height-msg').html($("#outer-div")[0].scrollHeight);
- $("#outer-div").scroll(function(){
- $('#scroll-to-bottom-msg').html('');
- $('#scroll-top-msg').html($("#outer-div")[0].scrollTop);
- $('#scroll-height-msg').html($("#outer-div")[0].scrollHeight);
- if($("#outer-div")[0].scrollTop >= ($("#outer-div")[0].scrollHeight - $("#outer-div").height()))
- $('#scroll-to-bottom-msg').html('滚动到DIV底部了');
- });
- });
- </script>
- <body>
- Scroll Top : <span id="scroll-top-msg">0</span> |
- Scroll Height : <span id="scroll-height-msg">0</span> <br/>
- <span id="scroll-to-bottom-msg"></span>
- <div id="outer-div" style="overflow-y:auto; overflow-x:hidden; width:800px;height:500px;background:gray;">
- <div style="height:750px;background:#aea;width:600px;margin:0 auto;">
- </div>
- </div>
- </body>
- </html>
当然也可以使用百分比判断滚动条离底部距离
- <!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>
- <meta http-equiv="content-type" content="text/html;charset=utf-8">
- <title>下拉滚动条滚到底部了吗?</title>
- <script language="javascript" src="jQuery.js"></script>
- <script language="javascript">
- $(document).ready(function (){
- $('#scroll-top-msg').html($("#outer-div")[0].scrollTop);
- $('#scroll-height-msg').html($("#outer-div")[0].scrollHeight);
- $("#outer-div").scroll(function(){
- $('#scroll-to-bottom-msg').html('');
- $('#scroll-top-msg').html($("#outer-div")[0].scrollTop);
- $('#scroll-height-msg').html($("#outer-div")[0].scrollHeight);
- if($("#outer-div")[0].scrollTop / (($("#outer-div")[0].scrollHeight - $("#outer-div").height()))== 0.95 ) //在滚动条距离底端5%以内
- $('#scroll-to-bottom-msg').html('滚动到DIV底部了');
- });
- });
- </script>
- <body>
- Scroll Top : <span id="scroll-top-msg">0</span> |
- Scroll Height : <span id="scroll-height-msg">0</span> <br/>
- <span id="scroll-to-bottom-msg"></span>
- <div id="outer-div" style="overflow-y:auto; overflow-x:hidden; width:800px;height:500px;background:gray;">
- <div style="height:750px;background:#aea;width:600px;margin:0 auto;">
- </div>
- </div>
- </body>
- </html>
jq 获取各个元素的宽度高度的方法的更多相关文章
- Knockout获取数组元素索引的2种方法,在MVC中实现
原文:Knockout获取数组元素索引的2种方法,在MVC中实现 在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespa ...
- Jq_Js_Js、Jq获取浏览器和屏幕各种高度宽度
$(document).ready(function() {alert($(window).height()); //浏览器当前窗口可视区域高度alert($(document).he ...
- jquery outerHeight方法 outerWidth方法 获取元素实际宽度高度
曾经写代码中,每当须要获取元素的实际"宽度"(这里的宽度是指元素宽度加上其边距)时,都须要用元素宽度加上margin值才行,今天发现一个叫outerWidth(options)的方 ...
- 获取dom元素的宽度和高度
一.获取css的大小 1.第一种通过内联样式 var box = document.getElementById('box'); var w = box.style.width; var h = bo ...
- JS与JQ 获取页面元素值的方法和差异对比
获取浏览器高度和宽度 document.documentElement.clientWidth ==> 浏览器可见区域宽度 document.documentElement.clientHeig ...
- 使用jquery获取父元素或父节点的方法
今天面试题问到了,没答上,jq要继续学习啊 jquery获取父元素方法比较多,比如parent(),parents(),closest()这些都能帮你实现查找父元素或节点,下面我们来一一讲解: 先举个 ...
- jquery获取父元素或父节点的方法
jquery获取父元素方法比较多,比如parent(),parents(),closest()这些都能帮你实现查找父元素或节点,下面我们来一一讲解: 先举个例子: <ul class=" ...
- js jq 获取网页元素宽度
Javascript: IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度d ...
- JS 和 Jq 获取客户端各种屏幕宽度和高度
//javascript 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: documen ...
随机推荐
- LoadRunner之关联
一.什么是关联 关联就是将服务器动态返回变化的值保存为一个参数以供后面需要用到的地方使用. 二.什么时候需要关联 1.服务器返回中存在动态变化的值,一般是类似session.token这样的无规则数据 ...
- case when then else end 累加
表结构 id(pk) satisfy(满意,一般,不满意) 一条语句查询 总数 各satisfy总数 SELECT count(*) AS 总数, count( CASE satisfy WHEN ' ...
- node-webkit-updater——NW.js自动更新
NW.js自动更新三种方案: 1)node-webkit-updater(推荐) 2)nwjs-autoupdater 3)nw-autoupdater NW.js自动更新三种方案:[http://d ...
- charles抓包教程
百度搜索下载charles 默认安装即可完成 1.双击charles.exe启动,我的是4.2.7版本.最好下载原版的不要去破解中文,会有不兼容 1.搜索该软件许可证书并输入即可长期使用 2.设置代理 ...
- 【ABAP系列】SAP 如何用ABAP实现自动发送外部邮件
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 如何用ABAP实现自动发 ...
- 【Linux开发】【Qt开发】交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
一. 什么是ABI和EABI1) ABI: 二进制应用程序接口(Application Binary Interface (ABI) for the ARM Architecture)在计算机中,应用 ...
- Ubuntu 安装nodejs最新版本
sudo apt update -y sudo apt install -y npm sudo npm config set registry https://registry.npm.tao ...
- ESXi导出的CentOS7 ovf文件导入到workstation 无法打开GUI登录界面的问题解决方案
1. 前几天将centos的机器导出为ovf 文件 然后使用 workstation 引入之后发现总是黑屏 然后有一个 横杠在闪 2. 解决办法是 修改centos的虚拟机配置文件,将显示器修改为这样 ...
- 使用idea搭建SSH
一.新建项目 选中Spring strust2 hibernate 二.见项目根路径下的lib下的jar移动到WEB-INF下 移动 修改路径 在lib目录下导入[c3p0-0.9.5.2.jar]. ...
- Hibernate 日期映射 条件查询
1. hql: ...and accopt_time > ?" 2. query.setDate Query query = session.createQuery(hql); int ...