UNPKG

element-resize-event-polyfill

Version:

Native event listener polyfill to capture element size changes

53 lines (49 loc) 1.46 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <script src="../dist/element-resize-event-polyfill.umd.js"></script> <style> .container { width: 300px; height: 200px; border: 1px solid; resize: both; overflow: auto; } .marker { background: pink; } </style> <title>Document</title> </head> <body> <div class="container" id="container"> <span class="marker"></span> </div> </body> <script> const elm = document.getElementById('container') const marker = document.querySelector('.marker') function markSize(el) { const width = el.clientWidth const height = el.clientHeight const marker = document.querySelector('.marker') console.log(`The size of the target element is width:${width},height:${height}`) marker.innerHTML = `${width}*${height}` } markSize(elm) elm.addEventListener('resize', function(e) { console.log('Trigger resizable event by listener one') markSize(e.target) }) elm.addEventListener('resize', function(e) { console.log('Trigger resizable event by listener two') }) elm.onresize = function(e) { console.log('Trigger resizable event by handler') } </script> </html>