在 javascript 中设置变量中图像的高度和宽度
Setting height, and width of an image from vars in javascript
我正在尝试制作一个图像作为背景并适合浏览器的尺寸。但是,我得到:expmrt.html:15 Uncaught TypeError: Cannot set property 'width' of null
.
//Get the browser legth and height, then after the code is done, time out in 300 miliseconds.
function ScreenLoop() {
var Width = window.innerWidth;
var Height = window.innerHeight;
alert("Width: " + Width + " and also Height: " + Height) //Debugging reasons
document.getElementById("theimage").width = Width; // Where the error: expmrt.html:15 Uncaught TypeError: Cannot set property 'width' of null) occures for some reason.
document.getElementById("theimage").height = Height;
setTimeout(ScreenLoop(), 300);
};
ScreenLoop();
<img src="tl2fZkc.png" id="theimage" width="50" height="50" alt="theimage" />
从窗口调用加载事件中的 ScreenLoop()
window.onload = function() {
ScreenLoop();
}
相关文章:
- 使用JavaScript根据窗口/视口的高度动态调整图像大小
- 如果宽度是百分比,如何设置以px为单位的图像高度
- 根据图像高度添加类
- 图像宽度和高度,无拉伸或溢出
- 自动缩放图像以匹配文本高度
- 使用asp.net FileUpload时从javascript获取图像宽度和高度
- whay可以't我使用javascript同时更改图像的高度和宽度
- Cloudinary上传小部件-样式上传预览图像(宽度和高度)
- 尝试使用window.innerHeight和Javascript设置图像高度
- 如果我将宽度调整为另一个值,找出我的图像的高度
- Javascript图像宽度和高度未定义值
- 我想根据容器中包含的图像设置容器的高度
- 高图表图像渲染器自动高度
- 灯箱类型的东西,使图像成为浏览器高度的百分比
- 如何在浏览器宽度/高度变化时动态调整图像网格的大小
- 当溢出-x 设置为隐藏时处理可变高度图像的最佳方法是什么?
- 在画布HTML5中查找上传的宽度和高度图像.(带文件读取器)
- 使用JavaScript查找图像的高度-图像加载错误
- 将悬停效果应用于响应高度图像
- 垂直居中可变高度图像,同时保持最大宽度/高度