How to detect DIV's dimension changed?

后端 未结 25 2366
抹茶落季
抹茶落季 2020-11-22 06:14

I\'ve the following sample html, there is a DIV which has 100% width. It contains some elements. While performing windows re-sizing, the inner elements may be re-positioned,

25条回答
  •  清酒与你
    2020-11-22 06:32

    The best solution would be to use the so-called Element Queries. However, they are not standard, no specification exists - and the only option is to use one of the polyfills/libraries available, if you want to go this way.

    The idea behind element queries is to allow a certain container on the page to respond to the space that's provided to it. This will allow to write a component once and then drop it anywhere on the page, while it will adjust its contents to its current size. No matter what the Window size is. This is the first difference that we see between element queries and media queries. Everyone hopes that at some point a specification will be created that will standardize element queries (or something that achieves the same goal) and make them native, clean, simple and robust. Most people agree that Media queries are quite limited and don't help for modular design and true responsiveness.

    There are a few polyfills/libraries that solve the problem in different ways (could be called workarounds instead of solutions though):

    • CSS Element Queries - https://github.com/marcj/css-element-queries
    • BoomQueries - https://github.com/BoomTownROI/boomqueries
    • eq.js - https://github.com/Snugug/eq.js
    • ElementQuery - https://github.com/tysonmatanich/elementQuery
    • And a few more, which I'm not going to list here, but you're free to search. I would not be able to say which of the currently available options is the best. You'll have to try a few and decide.

    I have seen other solutions to similar problems proposed. Usually they use timers or the Window/viewport size under the hood, which is not a real solution. Furthermore, I think ideally this should be solved mainly in CSS, and not in javascript or html.

提交回复
热议问题