滚动条不占位置-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

以Vue + Element Dialog为例用ResizeObserver监听容器高度变化当高度大于最大高度出现滚动条右边距减去滚动条的宽度反之恢复正常边距


<el-dialog>
  <div class="dialog_body" v-fix-scroll-bar="this">
    <slot />
  </div>
</el-dialog>

export default {
  data() {
    return {
      resizeObserver: null,
    }
  },

  methods: {
    setScrollBar() {
      this.resizeObserver = new ResizeObserver((entries) => {
        for (let entry of entries) {
          const cr = entry.contentRect;

          const el_dialog_body = entry.target.parentNode;
          const maxHeight = window.getComputedStyle(el_dialog_body).maxHeight;

          if (maxHeight && cr.height > parseInt(maxHeight)) {
            el_dialog_body.style.paddingRight = "20px";
          } else {
            el_dialog_body.style.paddingRight = "32px";
          }
        }
      });
    },
  },

  mounted() {
    this.setScrollBar();
  },

  directives: {
    fixScrollBar: {
      inserted: (el, binding) => {
        const _this = binding.value;
        _this.resizeObserver.observe(el);
      },
      unbind: (el, binding) => {
        const _this = binding.value;
        _this.resizeObserver.unobserve(el);
      },
    },
  },
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6