各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight

本文主要是介绍各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、.position()和.offset()

jquery的.position()获取相对于最近的position为relative或absolute的父元素的偏移,返回.position().left和.position().top,不算上自己的margin-left;

jquery的.offset()获取相对于视口左上角的偏移,返回.offset().left和.offset().top, 算上自己的margin-left,,还可以设置.offset({left:,top:});这个很有用,比如编写日期控件时,append到body上,然后就可以根据input的.offset()和高度设置日期控件的.offset(),最终这些值自动会被换算为position为relative(相对于body)的top和left。

jquery的.offset()与js的obj.offsetLeft和.obj.offsetTop一样,只是这个不能赋值。

2、.outerHeight()、.innerHeight()和.height()

jquery的.outerHeight()=border+padding+height,等同于js的.offsetHeight(.offsetWidth同理)

.outerHeight(true)=margin+border+padding+height

.innerHeight()=padding+height

.height()设置和获取高度

3、.scrollTop和.scrollHeight

.scrollTop:已经滚动过的高度;
.scrollHeight:整个滚动的高度,从开始到滚动结束滚动过的高度,包括滚动元素自身的高度。

用图来解释:如下图,contentContainer为父元素,content为它的子元素,由于它的高度设置得比父元素高度高,所以父元素出现了滚动条。

假设现在滑动条已划过了一段距离,

则contentContainer的scrollTop为a,scrollHeight为b。

4、监听整个网页的滚动事件:

测试ie11、chorme、firefox,发现body上不支持监听整个网页的滚动事件scroll,window上都支持:window.οnscrοll=function(){};

5、获取/设置网页已滚动的高度:

 chrome

document.documentElement.scrollTop无效为0,

document.body.scrollTop有效与$('body').scrollTop()+','+$(document).scrollTop()等值

 safari与chrome表现相同 
 ie11(包括其模拟的IE10-7)

document.documentElement.scrollTop有效与$(document).scrollTop()等值,

document.body.scrollTop无效为0,$('body').scrollTop()无效为0

 firefox

document.documentElement.scrollTop有效与$(document).scrollTop()等值,

document.body.scrollTop无效为0,$('body').scrollTop()无效为0

 

所以获取网页已滚动的高度时:Math.max(document.documentElement.scrollTop,document.body.scrollTop)或$(document).scrollTop()
jquery和js的scrollTop都可以赋值
所以如果要设置‘返回顶部’的效果应该:

$('#totop').click(function(){document.body.scrollTop?document.body.scrollTop=0:document.documentElement.scrollTop=0;//或$(document).scrollTop(0);
})

另外测试同一个页面,用鼠标滚轮把页面滚动到底,ie11和chrome都只触发了两次,大概是scrollTop从166到230,而firefox则触发了15次,看来ie11和chrome对触发滚动事件做了优化。可以手动添加判断两次滚动间隔超过多少px才允许运行onscroll函数:

var stop=Math.max(document.documentElement.scrollTop,document.body.scrollTop);
window.onscroll=function(){if(Math.max(document.documentElement.scrollTop,document.body.scrollTop)-stop>100){//do something}
}

6、获取body视口高度:

有<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">时:

 chrome

document.documentElement.clientHeight有效,window.innerHeigh有效,

document.body.clientHeight无效与$('body').height()等值

 ie11(包括其模拟的IE10-9)

document.documentElement.clientHeight有效,window.innerHeigh有效,

document.body.clientHeight无效与$('body').height()等值, 

 用ie11模拟ie8、ie7window.innerHeight为undefined,其他相同
 firefox

document.documentElement.clientHeight有效,window.innerHeigh有效,

document.body.clientHeight无效与$('body').height()等值

无<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">时:
chrome:document.documentElement.clientHeight无效与$('body').outerHeight(true)等值,window.innerHeigh有效,document.body.clientHeight有效,即使是有<!DOCTYPE>也是如此。
ie11:document.documentElement.clientHeight有效,document.body.clientHeight有效(ie7,8,9:无效与$('body').outerHeight(true)等值),window.innerHeigh有效,(用ie11模拟ie8、ie7时,window.innerHeight为undefined),即使是有<!DOCTYPE>也是如此。

所以在保证有完整<!DOCTYPE...声明的前提下,获取body的视口高度为:document.documentElement.clientHeight;

7、判断div的滚动条滚到底部

只要判断这个div已滚动的距离scrollTop+自己本身的高度offsetHeight>这个div的scrollHeight就表示滚动到底了。
如:一个id='contentContainer'的div里面包含一个高度查过自己 id='content'的子div。

则只要

复制代码

document.getElementById('contentContainer').onscroll=function(){if(this.scrollTop+this.offsetHeight>=this.scrollHeight){$(this).append('bottom');//不要用==因为很多浏览器并不是每滚动1px就触发scroll事件的}
}    

复制代码

<!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>example</title> </head> <body > <div id='show'></div><div id='contentContainer' style="margin:10px 0 0 10px;overflow:auto; padding:30px;height:300px;width:80%;border:2px solid black;"> <div id='content' style="margin:10px 0 0 10px; padding:10px;height:700px;width:80px;border:1px solid black;"></div> </div> <script type="text/javascript"> document.getElementById('contentContainer').οnscrοll=function(){ if(this.scrollTop+this.offsetHeight>=this.scrollHeight) document.getElementById('show').innerHTML=document.getElementById('show').innerHTML+'tobottom,'; } </script> </body> </html>

8、判断body的滚动条滚到底部:
获取页面的scrollHeight:document.documentElement.scrollHeight在各浏览器中表现一致。
监听页面滚动条滚到底部: 

window.onscroll=function(){    var scrolltop=Math.max(document.documentElement.scrollTop,document.body.scrollTop);     if(scrolltop+document.documentElement.clientHeight>=document.documentElement.scrollHeight)alert( 'nowbottom' );
};

这篇关于各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1084744

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

POJ1269 判断2条直线的位置关系

题目大意:给两个点能够确定一条直线,题目给出两条直线(由4个点确定),要求判断出这两条直线的关系:平行,同线,相交。如果相交还要求出交点坐标。 解题思路: 先判断两条直线p1p2, q1q2是否共线, 如果不是,再判断 直线 是否平行, 如果还不是, 则两直线相交。  判断共线:  p1p2q1 共线 且 p1p2q2 共线 ,共线用叉乘为 0  来判断,  判断 平行:  p1p

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou