For example, let’s add an event to the object window to get its width and height and show it on the web page. This function is used to specify what will happen if the size of a window is being changed. To add a resize event to the window, we can use the onresize() function in JavaScript. Add a Resize Event Using the onresize() Function in JavaScript ![]() You can also use the addEventListener() function to add an event to any object, like a checkbox. The easiest way is to check element size in some time intervals. It means we need to have some own solution to do it. By default, there are no built-in events that monitor element resize events. You can save the above code into an HTML file and open it with any browser and change its size to see if the code works or not. In this short article, we would like to show how to monitor size changes of div (or any element) in pure JavaScript. ![]() The width and height of the window will be shown on the page, and it will change as the size of the window is changed. ![]() After the start() function, we added the resize event, which will call the start() function when a user resizes the window. In the script tag, we have a function start(), which is used to change the text of the two spans with the width and height of the window. ![]() The id of the span will be used to get the element in JavaScript. There are other great answers here that show how to use setTimeout and the. Other browsers will trigger the resize event many times while youre resizing. We added a br tag to move the cursor to a new line, and on the new line, we added another span with text Height=, and after that, we added another empty span with an id SpanID2. Internet Explorer provides a resizeEnd event. In the above code, we added a span with text Width=, and after that, we added an empty span with an id SpanID1 in the body section.
0 Comments
Leave a Reply. |