JS获取页面内容宽度各浏览器不一

JS获取页面内容宽度各浏览器不一,第1张

你可以用documentbodyscrollWidth 来获取,但兼容性本人没有做过测试。 给你个参考:网页可见区域宽:documentbodyclientWidth

网页可见区域高:documentbodyclientHeight

网页可见区域宽:documentbodyoffsetWidth(包括边线的宽)

网页可见区域高:documentbodyoffsetHeight(包括边线的宽)

网页正文全文宽:documentbodyscrollWidth

网页正文全文高:documentbodyscrollHeight

网页被卷去的高:documentbodyscrollTop(IE7无效)

网页被卷去的左:documentbodyscrollLeft(IE7无效)网页被卷去的高:documentdocumentElementscrollTop(IE7有效)网页被卷去的左:documentdocumentElementscrollLeft(IE7有效)

网页正文部分上:windowscreenTop

网页正文部分左:windowscreenLeft

屏幕分辨率的高:windowscreenheight

屏幕分辨率的宽:windowscreenwidth

屏幕可用工作区高度:windowscreenavailHeight

屏幕可用工作区宽度:windowscreenavailWidth 相对于窗口左上角的X:windoweventclientX相对于窗口左上角的Y:windoweventclientY

相对于整个页面的X:windoweventX

相对于整个页面的Y:windoweventY

目前来讲html不具备获取浏览器宽度的能力。可以采用js的方式来获取,具体示例JS代码如下:

// 获取窗口宽度

if (windowinnerWidth)

winWidth = windowinnerWidth;

else if ((documentbody) && (documentbodyclientWidth))

winWidth = documentbodyclientWidth;

// 获取窗口高度

if (windowinnerHeight)

winHeight = windowinnerHeight;

else if ((documentbody) && (documentbodyclientHeight))

winHeight = documentbodyclientHeight;

// 通过深入 Document 内部对 body 进行检测,获取窗口大小

if (documentdocumentElement && documentdocumentElementclientHeight && documentdocumentElementclientWidth)

{

winHeight = documentdocumentElementclientHeight;

winWidth = documentdocumentElementclientWidth;

}

一、途径:

第一种情况就是宽高都写在样式表里,就比如#div1{width:120px;}。这中情况通过#div1stylewidth拿不到宽度,而通过#div1offsetWidth才可以获取到宽度。

第二种情况就是宽和高是写在行内中,比如style="width:120px;",这中情况通过上述2个方法都能拿到宽度。

二、小结:

因为idoffsetWidth和idoffsetHeight无视样式写在样式表还是行内,所以我们获取元素宽和高的时候最好用这2个属性。注意如果不是写在行内style中的属性都不能通过idstyleatrr来获取。

三、代码:

var o = documentgetElementById("view");

var h = ooffsetHeight; //高度

var w = ooffsetWidth; //宽度

参考资料

js获取Html元素的实际宽度高度的方法脚本之家[引用时间2017-12-29]

1、使用document对象获取WebView容器元素,例如:var container=documentgetElementById('webview-container');其中,'webview-container'是WebView容器元素的ID,您需要将其替换为实际的ID值。

2、使用JavaScript的offsetWidth属性获取WebView容器的宽度,例如:var width = containeroffsetWidth。

IE中: 

documentbodyclientWidth ==> BODY对象宽度 

documentbodyclientHeight ==> BODY对象高度 

documentdocumentElementclientWidth ==> 可见区域宽度 

documentdocumentElementclientHeight ==> 可见区域高度

FireFox中: 

documentbodyclientWidth ==> BODY对象宽度 

documentbodyclientHeight ==> BODY对象高度 

documentdocumentElementclientWidth ==> 可见区域宽度 

documentdocumentElementclientHeight ==> 可见区域高度

Opera中: 

documentbodyclientWidth ==> 可见区域宽度 

documentbodyclientHeight ==> 可见区域高度 

documentdocumentElementclientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽) 

documentdocumentElementclientHeight ==> 页面对象高度(即BODY对象高度加上Margin高) 

没有定义W3C的标准,则 

IE为: 

documentdocumentElementclientWidth ==> 0 

documentdocumentElementclientHeight ==> 0 

FireFox为: 

documentdocumentElementclientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽) 

documentdocumentElementclientHeight ==> 页面对象高度(即BODY对象高度加上Margin高) 

Opera为: 

documentdocumentElementclientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽) 

documentdocumentElementclientHeight ==> 页面对象高度(即BODY对象高度加上Margin高) 

网页可见区域宽: documentbodyclientWidth 

网页可见区域高: documentbodyclientHeight 

网页可见区域宽: documentbodyoffsetWidth (包括边线的宽) 

网页可见区域高: documentbodyoffsetHeight (包括边线的高) 

网页正文全文宽: documentbodyscrollWidth 

网页正文全文高: documentbodyscrollHeight 

网页被卷去的高: documentbodyscrollTop 

网页被卷去的左: documentbodyscrollLeft 

网页正文部分上: windowscreenTop 

网页正文部分左: windowscreenLeft 

屏幕分辨率的高: windowscreenheight 

屏幕分辨率的宽: windowscreenwidth 

屏幕可用工作区高度: windowscreenavailHeight 

屏幕可用工作区宽度: windowscreenavailWidth 

HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth 

scrollHeight: 获取对象的滚动高度。 

scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 

scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 

scrollWidth:获取对象的滚动宽度 

offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度 

offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置 

offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置 

eventclientX 相对文档的水平座标 

eventclientY 相对文档的垂直座标 

eventoffsetX 相对容器的水平坐标 

eventoffsetY 相对容器的垂直坐标 

documentdocumentElementscrollTop 垂直方向滚动的值 

eventclientX+documentdocumentElementscrollTop 相对文档的水平座标+垂直方向滚动的量

示例:

var winWidth = 0; 

var winHeight = 0; 

function findDimensions() //函数:获取尺寸 

    //获取窗口宽度 

    if (windowinnerWidth) 

        winWidth = windowinnerWidth; 

    else if ((documentbody) && (documentbodyclientWidth)) 

        winWidth = documentbodyclientWidth; 

    //获取窗口高度 

    if (windowinnerHeight) 

        winHeight = windowinnerHeight; 

    else if ((documentbody) && (documentbodyclientHeight)) 

        winHeight = documentbodyclientHeight; 

    //通过深入Document内部对body进行检测,获取窗口大小 

    if (documentdocumentElement && documentdocumentElementclientHeight && documentdocumentElementclientWidth) 

    { 

        winHeight = documentdocumentElementclientHeight; 

        winWidth = documentdocumentElementclientWidth; 

    } 

    //结果输出至两个文本框 

    documentform1availHeightvalue= winHeight; 

    documentform1availWidthvalue= winWidth; 

findDimensions(); 

//调用函数,获取数值 

windowonresize=findDimensions;

以上就是关于JS获取页面内容宽度各浏览器不一全部的内容,包括:JS获取页面内容宽度各浏览器不一、HTML如何获取浏览器的宽度、jQuery获取HTML元素“div”的宽度:$("div").width()等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9510375.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存