简体   繁体   English

如何为我的代码实现 window resize 事件侦听器?

[英]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.

相关问题 如何在 JavaScript 中附加窗口调整大小事件侦听器? - How can I attach a window resize event listener in JavaScript? 如何让我的matchMedia在我的resize事件监听器中工作? - How do I get my matchMedia to work inside of my resize event listener? 如何使用 Backbone 向视图中的窗口添加调整大小事件? - How do I add a resize event to the window in a view using Backbone? 如何为Firebase实现Node.Js服务器端事件侦听器? - How do I Implement a Node.Js server-side event listener for Firebase? 为什么我的窗口调整大小事件侦听器不起作用? 使用 Three.js 和 Vuetify.js - Why my window resize event listener doesn't work? Using Three.js and Vuetify.js 如何使javascript调整大小事件处理程序仅在用户停止调整窗口大小之后才发生? - How do I make the javascript resize event handler happen only after the user stops resizing the window? 如何调整WebDriverJS浏览器窗口的大小? - How do I resize a WebDriverJS browser window? 我如何知道如何在YUI中为事件侦听器设计回调函数? - How do I know how to design my callback function for my event listener in YUI? 如何添加事件监听器? - How do I add an event listener? 我如何获得一个进度元素,该元素在窗口调整大小时可以动画调整大小? - How do I get a progress element that was animated to resize on window resize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM