See the Pen imageZoom by Oh Ikmyeong (@dpffpself) on CodePen.
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Zoom Effect in E-commerce Website using Javascript</title>
<link rel="stylesheet" href="./style.css">
<script src="./main.js" type="module"></script>
</head>
<body>
<div>
<a href="https://www.youtube.com/watch?v=bO3OzrkSuvo">Image Zoom Effect in E-commerce Website using
Javascript</a>
<a
href="https://www.freepik.com/free-photo/woman-posing-snapback-cap-white-street-style-outfit-full-body_15476066.htm#fromView=search&page=2&position=6&uuid=bc8d4ddc-aab7-4ae9-bbbd-59138fe5b422">이미지
출처 FreePik</a>
</div>
<div id="image-zoom">
<img src="./img/model.jpg" alt="">
</div>
</body>
</html>
|
cs |
CSS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@charset "utf-8";
*{margin:0;padding:0;box-sizing:border-box;}
#image-zoom{
position:relative;overflow:hidden;
width:100%;max-width:400px;
aspect-ratio:1/1.5;
margin:20px auto;
background:#ededed;
border:1px solid #ccc;
border-radius:4px;
}
#image-zoom img{
width:100%; height:100%;
object-fit:cover;
/* opacity:0; */
}
.zoom{
position:absolute;
top:0;left:0;
width:100%;height:100%;
background-repeat:no-repeat;
background-size:250% auto;
background-position:center center;
opacity:0;
transition:opacity .2s;
}
#image-zoom:hover .zoom{
opacity:1;
cursor:zoom-in;
}
|
cs |
JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
export class ImageZoom {
constructor() {
this.$dom = null;
this.$img = null;
this.$zoom = null;
this.domInfo = { width: null, height: null };
}
/* Builder */
set_dom($dom) {
this.$dom = $dom;
return this;
}//set_dom
init() {
if (!this.$dom) return console.error("no DOM");
this.$img = this.$dom.querySelector("IMG");
if (!this.$img) return console.error("이미지 없음");
const imgUrl = this.$img.src;
if (!imgUrl) return console.error("이미지 경로 없음");
/* zoom dom */
this.$zoom = this.make_zoom_element(imgUrl);
this.$dom.appendChild(this.$zoom);
/* set info */
this.domInfo.width = this.$dom.offsetWidth;
this.domInfo.height = this.$dom.offsetHeight;
/* event */
this.$dom.addEventListener("mousemove", this.on_mouse_move);
}//init
/**
*
* @param {*} imgUrl
* @returns
*/
make_zoom_element(imgUrl) {
const $zoom = document.createElement("DIV");
$zoom.classList.add("zoom");
$zoom.style.backgroundImage = `url(${imgUrl})`;
return $zoom;
}//make_zoom_element
/**
*
* @param {*} e
*/
on_mouse_move = (e) => {
const { width, height } = this.domInfo;
const { offsetX, offsetY } = e;
const x = (offsetX / width) * 100;
const y = (offsetY / height) * 100;
this.$zoom.style.backgroundPosition = `${x}% ${y}%`;
}//on_mouse_move
}//ImageZoom
|
cs |
핵심 :
- 줌된 이미지는 200%정도의 배율로 background-image로 깔아준다
- 마우스 좌표값에 따라 (좌표값 / DOM의 너비 혹은 높이값) * 100 해주면 현재 좌표의 %값을 구할 수 있다.
'CSS&JS > 👀Study and Copy' 카테고리의 다른 글
[JS]Trello 스타일의 drag & drop 구현하기 (1) | 2024.09.27 |
---|---|
[LunDev]CSS only marquee slider (0) | 2024.09.19 |
[JS]팝업창에 함수를 전달하고, 팝업창의 변수를 부모창에 전달하는 방법. (0) | 2024.04.16 |
[OnlineTutorials]링크 아이콘 자석효과 (0) | 2024.03.06 |
[ZeroCho]인간 JS엔진 되기 1-6 this는 호출 때 결정된다. (1) | 2024.01.04 |