본문 바로가기
Vue.js

실시간 resize 확인 코드

by 하이란 2022. 8. 8.

반응형을 진행할 때 실시간으로 해상도를 체크해서 그에 따른 ui를 구현해 내야할 때가 있습니다. 

그럴 때 사용할 수 있는 코드입니다. 


HTML

  <div>
    <div class="size-container">
      <h2>width: {{ window.width }}</h2>
      <h2>height: {{ window.height }}</h2>
    </div>
  </div>

 

SCRIPT

export default {
  data() {
    return {
      window: {
        width: "",
        height: "",
      },
    };
  },
  mounted() {},
  created() {
    window.addEventListener("resize", this.handleResize);
    this.handleResize();
  },
  destroy() {
    window.removeEventListener("resize", this.handleResize);
  },
  methods: {
    handleResize() {
      this.window.width = window.innerWidth;
      this.window.height = window.innerHeight;
    },
  },
};

** 참고 ** 

created()에 resize이벤트 외에 메서드를 실행해주는 이유 : 

처음 Load될 때 사이즈도 체크해주기 위해서 입니다. 

구현화면

'Vue.js' 카테고리의 다른 글

vue에서 addClass, removeClass 처럼 사용  (0) 2022.08.08
1:1 error The template root requires exactly one element  (0) 2022.08.08
$nextTick()  (0) 2022.08.08
코드 스플리팅  (0) 2022.08.08
vue chart.js  (0) 2022.08.08