아래는 사용자의 요청에 따라 구현한 YouTube 웹사이트 UI의 프로토타입입니다. 이는 HTML, CSS, JavaScript를 사용하여 간단하게 모방한 버전으로, 실제 YouTube의 복잡한 기능을 모두 구현한 것은 아닙니다. 대신, 주요 요구사항에 초점을 맞췄습니다:
- YouTube UI 모방: 기본 레이아웃을 패널로 나누었습니다. (헤더 패널, 왼쪽 사이드바 패널, 메인 콘텐츠 패널, 오른쪽 사이드바 패널)
- 패널 자유 이동: 각 패널을 마우스로 드래그하여 자유롭게 이동할 수 있습니다.
- 패널 스냅 기능: 드래그를 끝낼 때 (mouseup), 다른 패널의 에지(왼쪽, 오른쪽, 위, 아래)에 가까우면 자동으로 스냅됩니다. 스냅 임계값은 20px로 설정했습니다. (예: 한 패널의 오른쪽 에지가 다른 패널의 왼쪽 에지에 20px 이내로 가까우면 스냅)
제한 사항 및 설명
- 이는 데모 목적으로 간단히 구현된 것입니다. 실제 프로덕션에서는 더 세밀한 조정(예: 충돌 방지, 리사이징 지원)이 필요할 수 있습니다.
- 스냅은 드래그 종료 시에만 발생합니다. 실시간 스냅(드래그 중)은 더 복잡한 라이브러리(예: Interact.js나 Draggable.js)를 사용하면 구현 가능하지만, 여기서는 순수 JavaScript로 유지했습니다.
- 패널은
position: absolute로 설정되어 화면 내에서 자유롭게 이동합니다. 초기 위치는 YouTube 레이아웃을 모방하도록 설정했습니다.
- 브라우저에서 직접 테스트하세요. (예: Chrome 개발자 도구)
코드
아래는 전체 코드입니다. HTML 파일로 저장하여 브라우저에서 열어보세요.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube UI with Movable Snapping Panels</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden; /* 패널이 화면 밖으로 나가지 않도록 */
background-color: #f9f9f9;
font-family: Arial, sans-serif;
}
.panel {
position: absolute;
background-color: white;
border: 1px solid #ddd;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
cursor: move; /* 드래그 커서 */
padding: 10px;
box-sizing: border-box;
}
#header {
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #ff0000; /* YouTube red */
color: white;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px;
}
#sidebar-left {
top: 60px;
left: 0;
width: 240px;
height: calc(100% - 60px);
background-color: #fff;
}
#main-content {
top: 60px;
left: 240px;
width: calc(100% - 480px); /* 왼쪽+오른쪽 사이드바 고려 */
height: calc(100% - 60px);
background-color: #f1f1f1;
overflow: auto;
}
#sidebar-right {
top: 60px;
right: 0;
width: 240px;
height: calc(100% - 60px);
background-color: #fff;
}
/* 간단한 YouTube 스타일 모방 */
#header .logo { font-size: 24px; font-weight: bold; }
#header .search { flex: 1; margin: 0 20px; padding: 5px; border: none; }
#sidebar-left ul { list-style: none; padding: 0; }
#sidebar-left li { padding: 10px; border-bottom: 1px solid #eee; }
#main-content .video { width: 100%; height: 400px; background: #000; color: white; text-align: center; line-height: 400px; }
#sidebar-right .related { padding: 10px; border-bottom: 1px solid #eee; }
</style>
</head>
<body>
<!-- 헤더 패널 -->
<div id="header" class="panel">
<div class="logo">YouTube</div>
<input type="text" class="search" placeholder="Search">
<div>Notifications | Upload | Profile</div>
</div>
<!-- 왼쪽 사이드바 패널 -->
<div id="sidebar-left" class="panel">
<ul>
<li>Home</li>
<li>Explore</li>
<li>Subscriptions</li>
<li>Library</li>
<li>History</li>
</ul>
</div>
<!-- 메인 콘텐츠 패널 -->
<div id="main-content" class="panel">
<div class="video">Video Player</div>
<p>Recommended Videos:</p>
<ul>
<li>Video 1</li>
<li>Video 2</li>
<li>Video 3</li>
</ul>
</div>
<!-- 오른쪽 사이드바 패널 (예: 관련 비디오) -->
<div id="sidebar-right" class="panel">
<div class="related">Related Video 1</div>
<div class="related">Related Video 2</div>
<div class="related">Related Video 3</div>
</div>
<script>
let isDragging = false;
let currentPanel = null;
let offsetX = 0;
let offsetY = 0;
const snapDistance = 20; // 스냅 임계값 (px)
// 모든 패널 가져오기
const panels = document.querySelectorAll('.panel');
// 드래그 시작
document.addEventListener('mousedown', (e) => {
if (e.target.closest('.panel')) {
currentPanel = e.target.closest('.panel');
isDragging = true;
const rect = currentPanel.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
currentPanel.style.zIndex = 1000; // 드래그 중 최상위로
}
});
// 드래그 중 이동
document.addEventListener('mousemove', (e) => {
if (isDragging && currentPanel) {
currentPanel.style.left = `${e.clientX - offsetX}px`;
currentPanel.style.top = `${e.clientY - offsetY}px`;
}
});
// 드래그 종료 및 스냅
document.addEventListener('mouseup', () => {
if (isDragging && currentPanel) {
snapPanel(currentPanel);
currentPanel.style.zIndex = ''; // z-index 초기화
isDragging = false;
currentPanel = null;
}
});
// 스냅 함수
function snapPanel(panel) {
const currentRect = panel.getBoundingClientRect();
panels.forEach(otherPanel => {
if (otherPanel === panel) return; // 자신 제외
const otherRect = otherPanel.getBoundingClientRect();
// 왼쪽 에지 스냅 (current의 left -> other의 right)
if (Math.abs(currentRect.left - otherRect.right) < snapDistance &&
currentRect.top < otherRect.bottom && currentRect.bottom > otherRect.top) {
panel.style.left = `${otherRect.right}px`;
}
// 오른쪽 에지 스냅 (current의 right -> other의 left)
if (Math.abs(currentRect.right - otherRect.left) < snapDistance &&
currentRect.top < otherRect.bottom && currentRect.bottom > otherRect.top) {
panel.style.left = `${otherRect.left - currentRect.width}px`;
}
// 위쪽 에지 스냅 (current의 top -> other의 bottom)
if (Math.abs(currentRect.top - otherRect.bottom) < snapDistance &&
currentRect.left < otherRect.right && currentRect.right > otherRect.left) {
panel.style.top = `${otherRect.bottom}px`;
}
// 아래쪽 에지 스냅 (current의 bottom -> other의 top)
if (Math.abs(currentRect.bottom - otherRect.top) < snapDistance &&
currentRect.left < otherRect.right && currentRect.right > otherRect.left) {
panel.style.top = `${otherRect.top - currentRect.height}px`;
}
});
}
</script>
</body>
</html>
사용 방법
- 이 코드를 HTML 파일로 저장하고 브라우저에서 열기.
- 각 패널(헤더, 사이드바 등)을 마우스로 클릭하고 드래그하여 이동.
- 드래그를 놓으면 (mouseup), 다른 패널의 에지에 가까우면 자동 스냅됨. (예: 메인 콘텐츠를 왼쪽 사이드바 오른쪽에 붙이면 스냅)
- 초기 상태는 YouTube의 기본 레이아웃을 모방합니다.
개선 제안
- 라이브러리 사용: 더 부드러운 드래그와 실시간 스냅을 위해 Interact.js나 Draggable를 추가할 수 있습니다.
- 추가 기능: 패널 리사이징, 화면 경계 스냅, 모바일 지원 등을 추가하면 더 좋을 것입니다.
- 문제 발생 시: 브라우저 콘솔을 확인하세요. 만약 더 세밀한 조정이 필요하다면 추가 요청해주세요!