赞
踩
代码片段
- <template>
- <div :class="$options.name"></div>
- </template>
- <script>
- export default {
- mounted() {
- this.resizeObserverThisEl();
- },
- destroyed() {
- this.resizeObserver.unobserve(this.$el); // 停止监听元素的尺寸变化
- this.resizeObserver.disconnect(); // 如果不再需要该 ResizeObserver 实例,可以停止所有观察并清理资源
- },
- methods: {
- resizeObserverThisEl(d) {
- const div = this.$el; //监听组件自身
- this.resizeObserver = new ResizeObserver((entries) => {
- for (let entry of entries) {
- const width = entry.contentRect.width;
- const height = entry.contentRect.height;
- console.log(`Size: Width = ${width}, Height = ${height}`);
- }
- //当尺寸发生变化的时候触发一些行为…
- });
- this.resizeObserver.observe(div);
- },
- },
- };
- </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。