首页 > 解决方案 > 如何为我的代码实现窗口调整大小事件侦听器?

问题描述

我的网站上有一个自动轮播。当我调整浏览器的大小时,轮播的功能仍然有效,但图像变得扭曲(轮播显示了两张图片中的一些)

这是代码笔: https ://codepen.io/Harrison17/pen/VweamoL

const slideWidth = slides[index].clientWidth;
slide.style.transform = `translateX(${-slideWidth * index}px)`;

我想我需要为调整窗口大小添加一个事件侦听器,但我不确定如何在我的代码中实现它。

标签: javascript

解决方案


代码如下:

function onResize() {
  var windowWidth = window.innerWidth;

  // change the width of images or the carousel based on window width
}
window.onresize = onResize;

希望这可以帮助!


推荐阅读