Code Monkey home page Code Monkey logo

size-sensor's Introduction

DOM element size sensor which will callback when the element size changed.

DOM 元素尺寸监听器,当元素尺寸变化的时候,将会触发回调函数!

Build Status npm npm gzip

Install

npm i --save size-sensor

Then import it.

import { bind, clear } from 'size-sensor';

or import it by script in HTML, then get sizeSensor on window.

<script src="https://unpkg.com/size-sensor/dist/size-sensor.min.js"></script>

Usage

  • bind & unbind
import { bind, clear } from 'size-sensor';

// bind the event on element, will get the `unbind` function
const unbind1 = bind(document.querySelector('.container'), element => {
  // do what you want to to.
});

const unbind2 = bind(document.querySelector('.container'), element => {
  // do what you want to to.
});

// if you want to cancel bind event.
unbind1();
  • clear
import { bind, clear } from 'size-sensor';

/*
 * // bind the resize event.
 * const unbind1 = bind(...);
 * const unbind2 = bind(...);
 * ...
 */

// you can cancel all the event of element.
clear(element);

API

There is only 2 API:

  • bind(element, callback)

Bind the resize trigger function on element. The function will return unbind function.

  • clear(element)

Clear all the object and resize event on element.

Strategies

The size sensor strategies include:

  • ResizeObserver: use resizeObserver to observe element's size.
  • object: use object document's resize event.

If ResizeObserver exists, use it, else use object as default.

Let me know

Online demo click here. Used By:

License

ISC@hustcc.

size-sensor's People

Contributors

alexlomm avatar cerber-ursi avatar darlk avatar hustcc avatar sidkwok avatar tzimmermann 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

size-sensor's Issues

使用echarts-for-react中遇到一个问题(涉及到size-sensor)

公司项目使用了echarts-for-react绘制echarts-wordcloud的功能,最近交付给客户使用时出现了一个奇怪现象,就是词云图生成后不断的闪烁。
定位了客户机器,发现是在windows系统下,屏幕内容放大率大于100%时,就会出现这情况。我一路从echarts-for-react源码定位,发现只要注释掉size-sencor注册部分就不会出现这个闪烁。于是从github中找到你的size-sensor源码,定位到/lib/sensors/object.js中第43行,设置监听对象style时,就会导致resizeListener死循环执行。我看了下_constant.SensorStyle内容,只要把width:100%去掉或者改成width:auto,echarts-wordcloud在windows下且屏幕内容放大率大于100%都不会出现resizeListener死循环的请况了
同时查阅了下资料 https://www.auslogics.com/en/articles/fix-google-chrome-scaling-on-win-10/ ,发现chrome在屏幕内容放大率大于100%的情况下都有各种怪异行为,不知道是否跟chrome自身bug有关。我向github提一个pr,希望你能在百忙之中测试下

Allow usage of this library without forcing CSP `unsafe-inline`

We are currently trying to enforce stricter Content Security Policies on our webapp.
The webapp uses echarts-for-react, and this one in turn uses size-sensor.

Unfortunately, we are forced to set style-src: 'unsafe-inline' due to this dependency, because there is a code snippet in object.js that sets an inline style here:

obj.setAttribute('style', SensorStyle);

It would be really good if we could enhance the security of our webapp by removing unsafe-inline and still be able to use echarts.

I see a couple of ways to fix this:

  • Put styles into a separate CSS file and use CSS classes to reference them.
  • Create separate <style> tags in the same document and add a nonce attribute to them that is populated from a <meta> property on the page (ass JSS does it)

Size-sensor not responding to width changes in Safari (desktop)

This issue originated here: hustcc/echarts-for-react#354

I'm using echarts-for-react version 2.0.15-beta.1 and the charts aren't resizing in Safari when only their width changes (when height changes both height and width are updated correctly). For context, it looks like size-sensor is using the object document rather than ResizeObserver.

I was able to make it work correctly by changing the style on the <object> element, switching width:auto to width:100%. I'm not sure if this is an issue with the way Safari calculates "auto" widths but it seems like this setting is preventing resize events from being triggered correctly.

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.