[英]How do I implement the window resize event listener to my code?
I have an automatic carousel on my website.我的网站上有一个自动轮播。 When I resize the browser, the functionality of the carousel still works but the images become distorted (the carousel shows some of both pictures)当我调整浏览器的大小时,轮播的功能仍然有效,但图像变得扭曲(轮播显示了两张图片中的一些)
Here is the codepen: https://codepen.io/Harrison17/pen/VweamoL这是代码笔: https://codepen.io/Harrison17/pen/VweamoL
const slideWidth = slides[index].clientWidth;
slide.style.transform = `translateX(${-slideWidth * index}px)`;
I think I need to add an event listener for a resizing of the window but I'm not sure how to implement it into my code.我想我需要添加一个事件侦听器来调整 window 的大小,但我不确定如何在我的代码中实现它。
The code is as follows:代码如下:
function onResize() {
var windowWidth = window.innerWidth;
// change the width of images or the carousel based on window width
}
window.onresize = onResize;
Hope this helps!希望这可以帮助!
https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event
window.onresize = function() {
var height = window.innerHeight;
var width = window.innerWidth;
}
You would probably want to use the width attribute and "slide" accordingly.您可能希望使用宽度属性并相应地“滑动”。 If you can't get it working, I advise using one of my JavaScript carousel libraries such as slick如果你不能让它工作,我建议使用我的 JavaScript 轮播库之一,比如slick
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.