javascript - 获取客户端屏幕尺寸并将图像调整为它

标签 javascript html css

我正在尝试获取用户的屏幕大小并调整图像大小以适合他们的屏幕大小我当前的代码可以工作,但只能在 firefox 中使用。在 IE 和 Chrome 中,图像永远不会调整大小有人可以帮助我吗?

http://jsfiddle.net/dwcribbs/ZK4tK/2/

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../style.css">
 <script type="text/javascript" src="../javascript.js"></script>
 <script>
 window.onload = function(){ 
var w =screen.availWidth - 20 + "px";
var h =screen.availHeight - 80 + "px";
document.getElementById('full').style= "height:" +  h + ";" + "width:" +  w; +";";
alert(h+w);

checkCookie();
document.getElementsByClassName('box1')[0].addEventListener('click', correct, false); 
document.getElementsByClassName('box1')[0].addEventListener('mouseover', shade, false);
document.getElementsByClassName('box1')[0].addEventListener('mouseout', unshade, false);
document.getElementsByClassName('bg')[0].addEventListener('click', wrong, false);


function shade()
{

document.getElementById('button').style= "background-color: #ADD8E6; opacity:.4;";

}

function unshade()
{

document.getElementById('button').style= " ";

}

function loc()
{
var lo = "OS2.html";

return lo;

} 
}
 </script>

</head>
<body>
<div style="color: red;" onclick=" alert('Open Microsoft Power Point without *searching* for it\nSave it in the documents library (using backstage-view, save as), with the default name');" id="help">
<center>
?
</center>
</div>
<div class="wrap">
    <img id="full" style = "height: 500px; width: 500px;"class="bg" src = "../Pic/desktop.png" >
    <div id="button" style=" " class="box box1"></div>    
</div>








</body>
</html>

谢谢!

最佳答案

代替

document.getElementById('full').style = "height:" +  h + ";" + "width:" +  w;
+";"; // Useless NaN

你应该使用[ Demo ]

var s = document.getElementById('full').style;
s.height = h;
s.width = w;

或[Demo ]

document.getElementById('full')
    .setAttribute('style', "height:" +  h + ";" + "width:" +  w);

关于javascript - 获取客户端屏幕尺寸并将图像调整为它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277407/

相关文章:

javascript - SVG 移动方向改变

html - 为什么带有换行符的 PRE 元素不会在输出中产生空行?

css - 为列表设置导航栏

html - 尝试在 VPN 上的 Lync UCWA 上运行应用程序的 POST 请求时获取 502 Bad Gateway

html - 为什么span之前的伪类导致span下移

javascript - 有没有一种简单的方法可以在 React.JS 中包含像 DataTables 这样的 jQuery UI 插件?

javascript - 连接两个字段并使用该值来更改/设置隐藏字段的值

javascript - 使用 Node JS 动态编辑 word 文档并替换文本和图像占位符

css - Magento - 显示没有任何类别的 magento 导航

css - 推特 bootstrap 3 选项卡 View