javascript - 计算水平图像库中的图像

标签 javascript html css image

我有这个脚本:http://jsfiddle.net/Lanti/51tqf1v9/16/

我的问题是浏览器中发生了什么: 图像未调整为浏览器的高度(我希望它没有滚动条),但在 fiddle 中工作。

我如何计算这个循环?

window.onload = resize;
window.onresize = resize;
function resize() {
  var windowHeight = window.document.documentElement.clientHeight - 17 + 'px';
  var imgList = document.getElementsByClassName('resize');

  for (i = 0; i < imgList.length; i++) {
    imgList[i].style.height = windowHeight;
  }

  alert('resize event detected!');
}

如果我使用以下内容,则只有第一张图像的大小调整为视口(viewport)的高度(并且仍然没有水平滚动条的高度)。第一个图像将被选中。我不是 javascript 程序员,我不知道如何为所有选定的 img 元素创建一个工作循环:

window.onload = resize;
window.onresize = resize;
function resize() {
  var windowHeight = window.document.documentElement.clientHeight - 17 + 'px';
  var imgList = document.getElementsByClassName('resize');

  imgList[0].style.height = windowHeight;

  alert('resize event detected!');
}

我在浏览器控制台中收到以下错误:

Uncaught ReferenceError: i is not defined

感谢您的帮助!

最佳答案

你没有在 i 之前使用 var

使用下面的代码

    window.onload = resize;
window.onresize = resize;
function resize() {
  var windowHeight = window.document.documentElement.clientHeight + 'px';
  var imgList = document.getElementsByClassName('resize');

  for (var i = 0; i < imgList.length; i++) {
    imgList[i].style.height = windowHeight;
  }

  alert('resize event detected!');
}

使用 CSS

img.resize
{
    height:97vh;
}

关于javascript - 计算水平图像库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840850/

相关文章:

javascript - ReactJS react 路由器服务器渲染问题

javascript - 使用 onClick javascript 将类添加到 span 元素

html - Chrome : "Refused to apply inline style because it violates the following Content Security Policy directive..." 中的 css 内联样式错误

html - 如何使导航按钮与标题中的 Logo 图片处于同一级别?

javascript - Javascript 搜索函数中的错误处理

javascript - 比较数组元素

javascript - Ajax 和后退按钮。哈希值发生变化,但之前的页面状态存储在哪里?

javascript - 如何通过浏览器或服务器验证ssl证书

javascript - 为什么这个JS倒计时不起作用?

css - 填充可用高度,然后在没有更多空间时滚动