Javascript获取浏览器视口大小并输出到主体CSS中
我正在使用 Andy Langton javascript 来获取我的 veiwport 大小。如果您还没有听说过,请参阅下面的链接。
http://andylangton.co.uk/articles/javascript/get- viewport-size-javascript/
该脚本有效,但我不确定如何让脚本将“宽度”和“高度”输出到我的正文 CSS 或任何其他 CSS 选择器中。
请参阅下面我蹩脚的尝试,尽管我认为混合 jQuery 会出错,
<script>
<!--
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).ready(function() {
$("body").css({
width : "+viewportwidth+",
height : "+viewportheight+"
});
});
//-->
</script>
有人可以帮助我解决这个难题吗?
如果有一种方法可以通过 PHP 标签将其添加到正文 html 中,那也很酷,请参见下面我的示例...
<div style="width: <?php veiwportwidth(); ?>px; height: <?php veiwportheight(); ?>px;"> </div>
如果这是有道理的,如果没有也不必担心,只是 javascript 位就会非常有帮助。
提前致谢。
I'm using Andy Langton javascript to get my veiwport size. See link below if you haven't heard of it.
http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
The script works but I'm not sure hows the best way to get the script to output the 'width' and 'height' into my body CSS, or any other CSS selector for that matter.
See my lame attempt below, though I think I'm going wrong by mixing in jQuery
<script>
<!--
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).ready(function() {
$("body").css({
width : "+viewportwidth+",
height : "+viewportheight+"
});
});
//-->
</script>
Can any one help me with this conundrum?
If there's a way to add it into body html via PHP tag, that would be cool too, see below my example...
<div style="width: <?php veiwportwidth(); ?>px; height: <?php veiwportheight(); ?>px;"> </div>
If that makes sense, no worry if not, just the javascript bit would be hugely helpful.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经熟悉了 jQuery:
您还可以向 DOM 添加样式部分,这对于类重用很有帮助(但在本例中没有那么多):
演示:http://jsfiddle.net/ThinkingStiff/Pa9k3/
脚本:
You were close with your jQuery:
You can also add a style section to the DOM, which is helpful with class reuse (but not so much in this case):
Demo: http://jsfiddle.net/ThinkingStiff/Pa9k3/
Script:
以下是您需要修复的内容:
至:
Here is what you need to fix:
To: