window resize & resize observer

https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event

https://drafts.csswg.org/resize-observer-1/


<canvas id="elipse" style="display:block"></canvas>
<div id="menu" style="display:block;width:100px">
<img src="hamburger.jpg" style="width:24px;height:24px">
<p class="title">menu title</p>
</div>
// In response to resize, elipse paints an elipse inside a canvas
document.querySelector('#elipse').handleResize = entry => {
entry.target.width = entry.borderBoxSize.inlineSize;
entry.target.height = entry.borderBoxSize.blockSize;
let rx = Math.floor(entry.target.width / 2);
let ry = Math.floor(entry.target.height / 2);
let ctx = entry.target.getContext('2d');
ctx.beginPath();
ctx.ellipse(rx, ry, rx, ry, 0, 0, 2 * Math.PI);
ctx.stroke();
}
// In response to resize, change title visibility depending on width
document.querySelector('#menu').handleResize = entry => {
let title = entry.target.querySelector(".title")
if (entry.borderBoxSize.inlineSize < 40)
title.style.display = "none";
else
title.style.display = "inline-block";
} var ro = new ResizeObserver( entries => {
for (let entry of entries) {
let cs = window.getComputedStyle(entry.target);
console.log('watching element:', entry.target);
console.log(entry.contentRect.top,' is ', cs.paddingTop);
console.log(entry.contentRect.left,' is ', cs.paddingLeft);
console.log(entry.borderBoxSize.inlineSize,' is ', cs.width);
console.log(entry.borderBoxSize.blockSize,' is ', cs.height);
if (entry.target.handleResize)
entry.target.handleResize(entry);
}
});
ro.observe(document.querySelector('#elipse'));
ro.observe(document.querySelector('#menu'));

css resize

https://developer.mozilla.org/en-US/docs/Web/CSS/resize

js resize

https://codepen.io/xgqfrms/pen/dyyWrWb

react demo


import React, {
useState,
useEffect,
} from 'react'; import "./index.css"; import ChartTooltip from "./ChartTooltip";
import ChartBar from "./ChartBar";
import ChartPercentage from "./ChartPercentage"; const ChartBox = (props) => {
const {
isFirst,
data,
refClick,
} = props;
const barClick = () => {
const width = refClick();
console.log(`click width`, width);
setWidth(width);
};
const [width, setWidth] = useState(0);
useEffect(() => {
let width = refClick();
setWidth(width);
function reportWindowSize() {
console.log(`reszie`);
let width = refClick();
setWidth(width);
}
window.onresize = reportWindowSize;
}, [refClick]); return(
<>
<div className="funnel-chart-box" onClick={() => barClick()}>
<ChartTooltip data={data}>
<ChartBar
data={data}
isBase={isFirst}
base={width}
// base={barWidth}
/>
<ChartPercentage data={data} />
</ChartTooltip>
{/* <ChartBar
data={data}
isBase={isFirst}
base={width}
// base={barWidth}
/>
<ChartPercentage data={data} /> */}
</div>
</>
);
}; export default ChartBox;

Mutation Observer

https://javascript.ruanyifeng.com/dom/mutationobserver.html

https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver


https://codepen.io/xgqfrms/pen/eYYRwRM

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


window resize & resize observer的更多相关文章

  1. 关于window的resize事件

    也许你也遇到过这样的问题,或者还没注意到有过这样的问题,如下代码,在窗口发生变化时,会进入死循环: var _funResize = function(){ console.log('resize.. ...

  2. 获取屏幕翻转:var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'

    var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',这段是为了获取移动端屏幕是否翻转(手机重力 ...

  3. autoHeight # 动态高度添加 用 window.addEventListener('resize', function () {

    动态高度添加 用 window.addEventListener('resize', function () { mounted () { this.init() window.addEventLis ...

  4. vue 中监听窗口发生变化,触发监听事件, window.onresize && window.addEventListener('resize',fn) ,window.onresize无效的处理方式

    // 开始这样写,不执行 window.onresize = function() { console.log('窗口发生变化') } // 改成window监听事件 window.addEventL ...

  5. 动态高度计算 height window.addEventListener('resize', () => {

    created() { window.addEventListener('resize', () => { }) },

  6. openstack instance resize Resize error: Unable to resize disk down

  7. JQuery 在$(window).load() 事件中 不运行 $(window).resize()

    本文转载至: http://stackoverflow.com/questions/2597152/jquery-window-resize-doesnt-work-on-load 原文标题   :J ...

  8. 解决Javascript中$(window).resize()多次执行(转)

    https://www.cnblogs.com/shuilangyizu/p/6816756.html 有些时候,我们需要在浏览器窗口发生变化的时候,动态的执行一些操作,比如做自适应页面时的适配.这个 ...

  9. 解决Javascript中$(window).resize()多次执行

    有些时候,我们需要在浏览器窗口发生变化的时候,动态的执行一些操作,比如做自适应页面时的适配.这个时候,我们需要在窗口拖动的时候去执行代码.但是有些时候,执行的操作比较复杂,我们只希望在窗口拖动完毕之后 ...

随机推荐

  1. Communicating sequential processes CSP 通信顺序进程 CSP writing to a file by name (process, Erlang) vs. writing to a file descriptor (channel, Go)

    the-way-to-go_ZH_CN/01.2.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...

  2. python3编码转换

    str->bytes:encode编码 bytes->str:decode解码 字符串通过编码成为字节码,字节码通过解码成为字符串. >>> text = '我是文本' ...

  3. P1663 山

    写在前面 简单的二分答案,稍微加点数学计算,很有意思. 算法思路 二分答案可行的原因:答案具有单调性. 这道题目中证明一下:首先无限高显然是能看到任何一个点的,且山的每一条边都是对答案的一个限制,因此 ...

  4. Prometheus 初探和配置(安装测试)

    本文大纲: • Prometheus 官⽹下载• Prometheus 开始安装• Prometheus 启动运⾏• Prometheus 基本配置⽂件讲解• 安装第⼀个exporter => ...

  5. 获取 *.properties配置文件内容

    package com.loan.modules.common.util; import java.util.ResourceBundle; /** * 获取 *.properties配置文件内容 * ...

  6. TZOJ6556: 嗅探器

    最近在练Tarjan,看到这道题目分类在割点里面就想尝试做一下,点开发现题目数据范围竟然如此之小,算了,bfs暴力一发. 题目意思就是你需要找到一个关键节点,也可以理解成,行军打仗时必需经过的地方,敌 ...

  7. docker部署 springboot 多模块项目+vue

    之前学习了docker,今天就来试试将这个项目打包成docker镜像并通过运行一个镜像来运行项目.这里使用的项目是el-admin.是一个开源的springboot后端管理框架(前端vue),有兴趣的 ...

  8. CF-1291 D - Irreducible Anagrams

    D. Irreducible Anagrams 题意 若两个字符串中每个字符的个数都是一样的,则称他们互为\(anagrams\).现在定义两个字符串s,t是\(reducible~anagram\) ...

  9. HDU-6148 Valley Number (数位DP)

    当一个数字,从左到右依次看过去数字没有出现先递增接着递减的"山峰"现象,就被称作 Valley Number.它可以递增,也可以递减,还可以先递减再递增.在递增或递减的过程中可以出 ...

  10. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park

    Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who l ...