gpt4 book ai didi

html - 了解响应式设计的视口(viewport)宽度

转载 作者:行者123 更新时间:2023-11-28 11:27:18 24 4
gpt4 key购买 nike

在阅读了很多关于 browser viewport width 的内容之后问题,我决定做一个试验,看看我是否理解这个概念。

我在下面使用了 javascript:此脚本打印“您的视口(viewport)宽度为 WidthxHeight”

元素 1:在 1920 x 1080 分辨率下,HP x2301 屏幕没有任何滚动条:JS 打印:你的视口(viewport)宽度是 1920x955

元素 2:在 1920 x 1080 分辨率下,带滚动条的 HP x2301 屏幕(我增加了带有大量 lorem Ipsum 字符串段落的页面高度):JS 打印:你的视口(viewport)宽度是 1920x955

Element3:在 Chrome 中,我检查了 element1 View 和 element2 View 。对于元素 2,带滚动条,Chrome 将宽度写为 1903 像素,而不是 1920。

我的问题是:

  1. 为什么 element1 和 element2 的宽度相同?对于 element2,我期待 new width = (1920 - scroll bar width) .例如 Chrome 在其检查工具中写入了 1903 像素。
  2. 我声明了<meta name="viewport" content="initial-scale=1, width=device-width">在我的标题中作为元标记。在我的 CSS3 中,我声明了 @media screen and (max-width: 1000px) { change something for responsiveness }由于我的目标是在浏览器的视口(viewport)中做出响应,这两个组合可以吗?在这一点上我应该说我的视口(viewport)定义和目标是没有垂直滚动条的纯显示宽度。因为我的理解,max-width:1000px对我来说意味着:在纯显示宽度 <=1000px
  3. 之后在布局中响应

javascript 源链接是:http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript

    <script type="text/javascript">
<!--
 
 var viewportwidth;
 var viewportheight;
  
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
  
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
  
 // older versions of IE
  
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>

提前谢谢你,问候

最佳答案

我明白了。

我更改了 JS 并获得了真实的视口(viewport)宽度。

JS 所有者是来自 SO 家族的 Vilmantas Baranauskas。

相关SO链接:Get the height and width of the browser viewport without scrollbars using jquery?

相关脚本:

    <script type="text/javascript">     
var viewportHeight;
var viewportWidth;
if (document.compatMode === 'BackCompat') {
viewportHeight = document.body.clientHeight;
viewportWidth = document.body.clientWidth;
} else {
viewportHeight = document.documentElement.clientHeight;
viewportWidth = document.documentElement.clientWidth;
}
document.write('<p>Your viewport width without scrollbars is '+viewportHeight+'x'+viewportWidth+'</p>');
</script>

1903 像素宽度是正确的,因为我知道滚动条标准宽度是 17px。

我还建议任何人在 CSS BodyHTML 标签中使用 overflow-y:scroll; 代码来制作浏览器即使对于空白草稿网页,也始终显示滚动条。

关于html - 了解响应式设计的视口(viewport)宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21302273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com