日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

前端开发跨浏览器计算页面大小、滚动高度

發布時間:2025/3/18 HTML 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 前端开发跨浏览器计算页面大小、滚动高度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前端開發中跨平臺。跨USER-AGENT的適配很繁瑣


想要得到瀏覽器px大小非常煩。因此以下2個函數非常實用

這兩個javascript函數很有用,用來計算瀏覽器頁面大小,窗體大小,以及滾動高度,源碼例如以下:



function getPageSize() {var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer MacxScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and SafarixScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;}var windowWidth, windowHeight;if (self.innerHeight) { // all except Explorerif (document.documentElement.clientWidth) {windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;}windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict ModewindowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) { // other ExplorerswindowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;}var pageHeight, pageWidth;// for small pages with total height less then height of the viewportpageHeight = yScroll < windowHeight ? windowHeight: yScroll;// for small pages with total width less then width of the viewportpageWidth = xScroll < windowWidth ? windowWidth: xScroll;return {pageWidth: pageWidth,pageHeight: pageHeight,windowWidth: windowWidth,windowHeight: windowHeight}; } function getPageScroll() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 StrictyScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) { // all other ExplorersyScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;}return {xScroll: xScroll,yScroll: yScroll}; }

參考 ? ? http://my.oschina.net/jockchou/blog/465220


總結

以上是生活随笔為你收集整理的前端开发跨浏览器计算页面大小、滚动高度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。