Resizer.js

2023. 11. 18. 21:00·JavaScript

웹 페이지에 기둥을 하나 만들어서 좌우로 리사이징을 하는 JS

 

// Query the element
const resizer = document.getElementById("resizer");
const leftSide = resizer.previousElementSibling;
const rightSide = resizer.nextElementSibling;

// The Current Position of mouse
let x = 0;
let y = 0;

// Width of left side
let leftWidth = 0;

// Handle the mousedown event
// that's triggered when user drags the resizer
const mouseDownHandler = function (e) {
  // Get the current mouse position
  x = e.clientX;
  y = e.clientY;
  leftWidth = leftSide.getBoundingClientRect().width;

  // Attach the listeners to `document`
  document.addEventListener("mousemove", mouseMoveHandler);
  document.addEventListener("mouseup", mouseUpHandler);
};

const mouseMoveHandler = function (e) {
  // How far the mouse has been moved
  const dx = e.clientX - x;
  const dy = e.clientY - y;

  const newLeftWidth =
    ((leftWidth + dx) * 100) / resizer.parentNode.getBoundingClientRect().width;

  leftSide.style.width = `${newLeftWidth}%`;
  document.body.style.cursor = "col-resize";

  leftSide.style.userSelect = "none";
  leftSide.style.pointerEvents = "none";

  rightSide.style.userSelect = "none";
  rightSide.style.pointerEvents = "none";
};

const mouseUpHandler = function () {
  resizer.style.removeProperty("cursor");
  document.body.style.removeProperty("cursor");

  leftSide.style.removeProperty("user-select");
  leftSide.style.removeProperty("pointer-events");

  rightSide.style.removeProperty("user-select");
  rightSide.style.removeProperty("pointer-events");

  // Remove the handlers of `mousemove` and `mouseup`
  document.removeEventListener("mousemove", mouseMoveHandler);
  document.removeEventListener("mouseup", mouseUpHandler);
};

const mouseOverHandler = function () {
  document.body.style.cursor = "col-resize";
};

const mouseLeaveHandler = function () {
  resizer.style.removeProperty("cursor");
  document.body.style.removeProperty("cursor");
};

// Attach the handler
resizer.addEventListener("mousedown", mouseDownHandler);
resizer.addEventListener("mouseover", mouseOverHandler);
resizer.addEventListener("mouseleave", mouseLeaveHandler);
저작자표시 비영리 (새창열림)

'JavaScript' 카테고리의 다른 글

Uncaught SyntaxError: Invalid shorthand property initializer 해결  (0) 2023.02.27
Javascript 날짜 계산기  (0) 2023.01.31
Swiper <DevTools failed to load source map>  (0) 2022.12.08
TypeError: ws.Server is not a constructor  (0) 2022.11.01
Element Resize 시 window resize 이벤트 발생 속도  (0) 2022.10.31
'JavaScript' 카테고리의 다른 글
  • Uncaught SyntaxError: Invalid shorthand property initializer 해결
  • Javascript 날짜 계산기
  • Swiper <DevTools failed to load source map>
  • TypeError: ws.Server is not a constructor
요술공주밍키
요술공주밍키
조금씩이라도 꾸준히..
  • 요술공주밍키
    삽질의흔적
    요술공주밍키
  • 전체
    오늘
    어제
    • 분류 전체보기 (139)
      • Java (42)
        • Spring Boot (14)
        • Spring Boot 게시판 (14)
        • 공중화장실 찾기 (4)
        • 쇼핑몰 (8)
      • JavaScript (8)
        • NodeJS (2)
      • Python (5)
        • Django (4)
      • Server (10)
        • Docker (4)
        • K8S (0)
        • Jenkins (1)
      • 알고리즘 (24)
        • 프로그래머스 (19)
        • 백준 (5)
      • Etc (21)
        • 개발 팁 (1)
      • 일상 (27)
        • 독서 포스트 (25)
        • 회고록 (2)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
요술공주밍키
Resizer.js
상단으로

티스토리툴바