This JavaScript code detects browser window resize using an ‘optimizedResize’ event. It updates a status message. The code improves performance by preventing rapid event firing. It’s helpful for managing resize-triggered actions.
You can use this code in web development projects requiring responsive design elements. It helps manage and trigger actions during window resize events efficiently.
How to Detect Element Resize Event in JavaScript
1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document. (Optional)
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
2. In your HTML file, create a container element with a class, such as ‘box,’ that you want to monitor for resize events. Here’s an example:
<div class="box"><b>Resize ME</b><span class="status">Waiting</span></div>
3. Now, style your container element as needed using CSS. The following code includes some basic styling. Feel free to customize it to match your project’s design.
* { box-sizing: border-box; } html { height: 100%; display: flex; } body { margin: auto; } html { background: grey; } .box { width: 3em; height: 3em; border: solid 10px gold; border-width: 4vmin; font-size: 40px; font-size: 13vmin; line-height: 0.8; text-align: center; display: flex; align-items: center; justify-content: center; flex-direction: column; } .box span { font-size: 6vmin; color: gold; }
4. Finally, add the following JavaScript code to your project. This code sets up an ‘optimizedResize’ event listener to detect window resizes efficiently. It updates the status message within your container.
var body = document.querySelector("body"), statusReport = document.querySelector(".status"); (function() { var throttle = function(type, name, obj) { obj = obj || window; var running = false; var func = function() { if (running) { return; } running = true; requestAnimationFrame(function() { obj.dispatchEvent(new CustomEvent(name)); running = false; }); }; obj.addEventListener(type, func); }; /* init - you can init any event */ throttle("resize", "optimizedResize"); })(); var resizeTimer; // handle event window.addEventListener("optimizedResize", function() { console.log("firing"); statusReport.innerHTML = "firing"; clearTimeout(resizeTimer); resizeTimer = setTimeout(function() { statusReport.innerHTML = "fired"; console.log("fired"); }, 250); });
You can customize the code to perform specific actions when a resize event is detected. For instance, you can update the layout or perform other dynamic adjustments. Modify the following part of the code to suit your needs:
window.addEventListener("optimizedResize", function() { // Add your code here to handle the resize event // For example, you can update the layout, animations, or any other actions. });
That’s all! hopefully, you have successfully created Javascript Element Resize Event. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.