Giter Site home page Giter Site logo

kyleamathews / element-resize-event Goto Github PK

View Code? Open in Web Editor NEW
176.0 9.0 49.0 45 KB

Library to make it easy to listen for element resize events

Home Page: http://kyleamathews.github.io/element-resize-event/

License: MIT License

Makefile 3.68% JavaScript 90.54% HTML 5.25% Shell 0.53%

element-resize-event's Introduction

element-resize-event

Library to make it easy to listen for element resize events

Code borrowed from a blog post by backalleycoder.com.

Install

npm install element-resize-event

Dependencies

This library depends on the availability of requestAnimationFrame and cancelAnimationFrame

Usage

var elementResizeEvent = require("element-resize-event")

var element = document.getElementById("resize")

elementResizeEvent(element, function () {
  console.log("resized!")
  console.log(element.offsetWidth)
})

Unbinding The Event Listener

var unbind = require("element-resize-event").unbind

unbind(element)

element-resize-event's People

Contributors

alesya-h avatar davidenglishmusic avatar drcmda avatar eaglus avatar eligolding avatar karleberts avatar kbremner avatar klamping avatar kyleamathews avatar tirli avatar trevordmiller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

element-resize-event's Issues

Could someone explain to me how this package work?

I've read the source code, and I got some questions:

  1. Any browser which implements attachEvent will support the resize event on any DOM element such as <div>?
  2. According to question 1, other browsers do not support attachEvent so the resize event will not work, but how does the <object> element can?
  3. Can we use <object> to detect resize in all browsers, even in IE 9 10?

__resizeListeners__ is undefined

Hi,
In versions 2.0.8 and 2.0.9 this line has been removed:
if(trigger !== undefined)
which caused undefined error.
There was specific reason for the removal or we can return them? currently it causes error in our console.

element-resize-event pollutes nodejs environment with "document" and "window" objects

window and document are declared without the var keyword:

https://github.com/KyleAMathews/element-resize-event/blob/master/index.js#L2

This has an equivalent effect to this line:

global.window = {}

It's a best practice for npm packages to not pollute the global namespace. In particular, plenty of other modules use a rough check on window to tell if it's a nodejs environment or not, and this line invalidates the check.

Would you mind updating the package with this fix?

resize event doesn't get triggered on Chrome

I'm using this package in Angular 7 in OnInit method which seems to be working ok in Firefox but not in Chrome Version 80.0.3987.106 (Official Build) (64-bit). Technically it shouldn't be working in Firefox as I don't resize the element on page load.

how to use `unbind` ?

use in es6

import elementResizeEvent, {unbind} from 'element-resize-event'

but the unbind is undefined

error when using Vite with @antv/l7

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

I'm using Vite with @antv/l7 which has your project as one of the dependcies.
image

Since Vite prebundled all modules with esbuild when developing, it tells me that
image

Maybe the function name exports is confused with module.exports here.

Here is the diff that solved my problem:

diff --git a/node_modules/element-resize-event/index.js b/node_modules/element-resize-event/index.js
index fb1e2a3..d26e41a 100644
--- a/node_modules/element-resize-event/index.js
+++ b/node_modules/element-resize-event/index.js
@@ -14,7 +14,7 @@ function resizeListener(e) {
   })
 }
 
-var exports = function exports(element, fn) {
+var _exports = function _exports(element, fn) {
   var window = this
   var document = window.document
   var isIE
@@ -64,7 +64,7 @@ var exports = function exports(element, fn) {
   element.__resizeListeners__.push(fn)
 }
 
-module.exports = typeof window === 'undefined' ? exports : exports.bind(window)
+module.exports = typeof window === 'undefined' ? _exports : _exports.bind(window)
 
 module.exports.unbind = function (element, fn) {
   var attachEvent = document.attachEvent

This issue body was partially generated by patch-package.

Use parentNode to listen, not the element itself

Perhaps this is an edge case that I am seeing, but if the element has a defined height and width, the object DOM node will not detect a difference, and consequently will not trigger an event.

Here is what my DOM node looks like:

<div class="ReactTabs__TabPanel ReactTabs__TabPanel--selected" role="tabpanel" id="react-tabs-51" aria-labelledby="react-tabs-50" style="position: relative; height: 212px;">
    <div style="background: red; width: 100%; height: 100%;"></div>
    <object style="display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; pointer-events: none; z-index: -1; opacity: 0;" class="resize-sensor" type="text/html" data="about:blank"></object>
</div>

In this example, you can see that the root element has a height of 212px. When the parent div (not shown in this example) is dragged and resized, because the 'panel' div already has a height associated with it, it will not trigger an event.

I'd like to propose a solution: attach the object DOM node into the parentNode of the element:

// https://github.com/KyleAMathews/element-resize-event/blob/master/index.js#L73
    element.parentNode.appendChild(obj)  // line 78 & line 73

Here is what the DOM would look like after the change:

screen shot 2017-05-11 at 1 46 07 pm

Here is a screenshot with current implementation:

screen shot 2017-05-11 at 1 15 42 pm

window undefined

I rely on the echarts-for-react the package depends on your bag, but because your bag in the update cause in error in my code, trouble update before test.

Unbind event?

It appears there is no way to remove event handlers. Is this correct?

version 2.0.8 throws when requiring in node.

version 2.0.7 gracefully handles there not being a window when requiring in node.
The latest update throws (see error below).

$ node -r element-resize-event
/node_modules/element-resize-event/index.js:4
    window.webkitRequestAnimationFrame ||
                                       ^

ReferenceError: window is not defined
    at /node_modules/element-resize-event/index.js:4:40
    at Object.<anonymous> (/node_modules/element-resize-event/index.js:11:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at module.js:660:12
    at Array.forEach (native)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.