-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouseEffect04.html
489 lines (476 loc) · 21 KB
/
mouseEffect04.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
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>마우스 이펙트04</title>
<link rel="stylesheet" href="../assets/css/mouse.css">
<link rel="stylesheet" href="../assets/css/dracula.css">
<link href="https://webfontworld.github.io/NexonLv1Gothic/NexonLv1Gothic.css" rel="stylesheet">
<style>
/* mouseType */
.mouse__wrap {
/* vw라고 써주면 스크롤바 생기기 때문에 */
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
color: #fff;
overflow: hidden;
/* 두 줄로 나오게 하기 */
/* cursor: none; */
}
.mouse__wrap figure {
width: 50vw;
height: 50vh;
position: relative;
overflow: hidden;
transition: transform 500ms ease;
cursor: none;
}
.mouse__wrap figure:hover {
transform: scale(1/025);
}
.mouse__wrap figure img {
position: absolute;
left: -5%;
top: -5%;
width: 110%;
height: 110%;
/* 반응형위해 백그라운드 사이즈 커버와 비슷한 효과 */
object-fit: cover;
}
.mouse__wrap figure figcaption{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 16px;
/* 한줄로 나오게하기 */
white-space: nowrap;
line-height: 1.4;
font-weight: 300;
}
.mouse__cursor {
position: absolute;
left: 0;
top: 0;
width: 20px;
height: 20px;
background: #fff;
border-radius: 50%;
z-index: 1000;
user-select: none;
pointer-events: none;
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
</style>
</head>
<body class="img06">
<header id="header">
<h1><a href="mouseEffect.html">Javascript Mouse Effect03</a></h1>
<p>마우스 이펙트 - 이미지 효과</p>
<ul class="active1">
<li><a href="mouseEffect01.html">1</a></li>
<li><a href="mouseEffect02.html">2</a></li>
<li><a href="mouseEffect03.html">3</a></li>
<li class="active"><a href="mouseEffect04.html">4</a></li>
<li><a href="mouseEffect05.html">5</a></li>
<li><a href="mouseEffect06.html">6</a></li>
<li><a href="mouseEffect07.html">7</a></li>
</ul>
</header>
<!-- //header -->
<main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<figure>
<img src="../assets/img/effect_bg05.jpg" alt="이미지">
<figcaption>
<p>When you have a dream, you've got to grab it and never let go.</p>
<p>꿈이 있다면, 그 꿈을 잡고 절대 놓아주지마라.</p>
</figcaption>
</figure>
</div>
</section>
<div class="mouse__info">
<ul>
<li>mousePageX : <span class="mousePageX">0</span>px</li>
<li>mousePageY : <span class="mousePageY">0</span>px</li>
<li>centerPageX : <span class="centerPageX">0</span>px</li>
<li>centerPageY : <span class="centerPageY">0</span>px</li>
</ul>
</div>
</main>
<!-- //main -->
<footer id="footer">
<div class="modal__wrap">
<div class="modal__btn active2">소스 보기</div>
<div class="modal__cont">
<div class="modal__box">
<div class="title">
<span class="dot" aria-label="true"></span>
<div class="tabs">
<div class="active">
<span class="favicon" aria-label="true">
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
<circle cx="9" cy="9" r="9" fill="#fff"></circle>
<path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
<path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
</svg>
</span>
<em>Javascript</em>
<span class="close">
<svg width="18" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
<path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</span>
</div>
<div>
<span class="favicon" aria-label="true">
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
<circle cx="9" cy="9" r="9" fill="#fff"></circle>
<path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
<path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
</svg>
</span>
<em>HTML</em>
<span class="close">
<svg width="18" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
<path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</span>
</div>
<div>
<span class="favicon" aria-label="true">
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
<circle cx="9" cy="9" r="9" fill="#fff"></circle>
<path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
<path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
</svg>
</span>
<em>CSS</em>
<span class="close">
<svg width="18" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
<path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</span>
</div>
</div>
<span class="plus" aria-label="true">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
<path d="M2.5 8H13.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M8 2.5V13.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</span>
</div>
<div class="cont">
<div class="active">
<pre><code class="language-js"> // 마우스 따라다니게 하기
// 마우스가 움직였을 때 마우스 움직이는 값(event)을 가져올 수 있음
window.addEventListener("mousemove", (event) => {
document.querySelector(".clientX").innerText = event.clientX
document.querySelector(".clientY").innerText = event.clientY
document.querySelector(".offsetX").innerText = event.offsetX
document.querySelector(".offsetY").innerText = event.offsetY
document.querySelector(".pageX").innerText = event.pageX
document.querySelector(".pageY").innerText = event.pageY
document.querySelector(".screenX").innerText = event.screenX
document.querySelector(".screenY").innerText = event.screenY
});
const cursor = document.querySelector(".mouse__cursor")
window.addEventListener("mousemove", (e) => {
//const cursor = document.querySelector(".mouse__cursor")
//커서에 포지션 되어있기 때문에 left
//px 처럼 단위값을 써주어야 실행됨
cursor.style.left = e.clientX -25 + "px";
cursor.style.top = e.clientY -25 + "px";
});
// document.querySelector(".style1").addEventListener("mouseover", () => {
// //작동안함 : 위에 지역변수 이기 때문에
// //cursor.classList.add(".style1");
// cursor.classList.add("style1");
// })
// document.querySelector(".style1").addEventListener("mouseout", () => {
// cursor.classList.remove("style1");
// })
// document.querySelector(".style2").addEventListener("mouseover", () => {
// cursor.classList.add("style2");
// })
// document.querySelector(".style2").addEventListener("mouseout", () => {
// cursor.classList.remove("style2");
// })
// document.querySelector(".style3").addEventListener("mouseover", () => {
// cursor.classList.add("style3");
// })
// document.querySelector(".style3").addEventListener("mouseout", () => {
// cursor.classList.remove("style3");
// })
// document.querySelector(".style4").addEventListener("mouseover", () => {
// cursor.classList.add("style4");
// })
// document.querySelector(".style4").addEventListener("mouseout", () => {
// cursor.classList.remove("style4");
// })
// document.querySelector(".style5").addEventListener("mouseover", () => {
// cursor.classList.add("style5");
// })
// document.querySelector(".style5").addEventListener("mouseout", () => {
// cursor.classList.remove("style5");
// })
// document.querySelector(".style6").addEventListener("mouseover", () => {
// cursor.classList.add("style6");
// })
// document.querySelector(".style6").addEventListener("mouseout", () => {
// cursor.classList.remove("style6");
// })
// for(let i=1; i<7; i++){
// document.querySelector(".style" + i).addEventListener("mouseover", () => {
// cursor.classList.add("style" + i)})
// document.querySelector(".style" + i).addEventListener("mouseout", () => {
// cursor.classList.remove("style" + i)})
// };
//forEach() 나
// const num = [1, 2, 3, 4, 5, 6]
// num.forEach((el)=>{
// document.querySelector(".style" + [el]).addEventListener("mouseover", () => {
// cursor.classList.add("style" + [el])})
// document.querySelector(".style" + [el]).addEventListener("mouseout", () => {
// cursor.classList.remove("style" + [el])})
// })
//forEach() 선생님
// document.querySelectorAll(".mouse__wrap span").forEach((span, num) => {
// span.addEventListener("mouseover", () => {
// cursor.classList.add("style" + (num+1))
// })
// span.addEventListener("mouseout", () => {
// cursor.classList.remove("style" + (num+1))
// })
// })
//속성값 가져오기 (만약에 class="style55"이럴때 : 클래스 명이 바껴도 상관없게!)
//getAttribute
document.querySelectorAll(".mouse__wrap span").forEach( span => {
let attr = span.getAttribute("class");
span.addEventListener("mouseover", () => {
cursor.classList.add(attr)
})
span.addEventListener("mouseout", () => {
cursor.classList.remove(attr)
})
})
</code></pre>
</div>
<div>
<pre><code class="language-html"><main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p>The <span class="style1">future</span> depends on what we <span class="style2">do</span> in the <span class="style3">present</span></p>
<p><span class="style4">미래</span>는 <span class="style5">현재</span> 우리가 무엇을 <span class="style6">하는가</span>에 달려 있다.</p>
</div>
</section>
<div class="mouse__info">
<ul>
<li>clientX : <span class="clientX">0</span>px</li>
<li>clientY : <span class="clientY">0</span>px</li>
<li>offsetX : <span class="offsetX">0</span>px</li>
<li>offsetY : <span class="offsetY">0</span>px</li>
<li>pageX : <span class="pageX">0</span>px</li>
<li>pageY : <span class="pageY">0</span>px</li>
<li>screenX : <span class="screenX">0</span>px</li>
<li>screenY : <span class="screenY">0</span>px</li>
</ul>
</div>
</main>
</code></pre>
</div>
<div>
<pre><code class="language-css">/* mouseType */
.mouse__wrap {
/* vw라고 써주면 스크롤바 생기기 때문에 */
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
overflow: hidden;
/* 두 줄로 나오게 하기 */
flex-direction: column;
cursor: none;
}
.mouse__wrap p {
font-size: 2vw;
line-height: 2;
font-weight: 300;
}
.mouse__wrap p:last-child {
font-size: 3vw;
font-weight: 300;
}
.mouse__wrap p span {
border-bottom: 0.15vw dashed rgb(255, 253, 164);
color: rgb(255, 253, 164);
}
@media (max-width: 800px) {
.mouse__wrap p {
font-size: 30px;
padding: 0 20px;
line-height: 1.5;
text-align: center;
margin-bottom: 10px;
}
.mouse__wrap p:last-child {
font-size: 40px;
line-height: 1.5;
text-align: center;
/* 단어로 끊기 */
word-break: keep-all;
}
}
.mouse__cursor {
position: absolute;
left: 0;
top: 0;
width: 50px;
height: 50px;
border-radius: 50%;
border: 3px solid#fff;
background-color: rgba(225,225,225,0.1);
/* 선택이나 클릭 안되게 할 때 */
user-select: none;
pointer-events: none;
transition:
background-color 0.3s,
border-color 0.3s,
/* style 2에 트랜스폼줬으니 여기도 줘야함 */
transform 0.6s,
border-radius 0.3s
;
}
.mouse__cursor.style1{
background-color: rgba(229, 67, 215, 0.484);
border-color: #de78f3;
}
.mouse__cursor.style2{
background-color: rgba(111, 235, 94, 0.584);
border-color: #9bfd7d;
/* 커지고 y축으로 회전 */
transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3{
background-color: rgba(90, 128, 233, 0.721);
border-color: #2f3cb4;
/* 커지고 x축으로 회전 */
transform: scale(1.5) rotateX(720deg);
}
.mouse__cursor.style4{
background-color: rgba(235, 235, 69, 0.606);
border-color: #f2fc59;
/* 커지는 효과 */
transform: scale(10);
}
.mouse__cursor.style5{
background-color: rgba(243, 209, 141, 0.868);
border-color: #f5ab77;
}
.mouse__cursor.style6{
background-color: rgba(145, 248, 131, 0.846);
border-color: #ccfdb0;
border-radius: 0;
/* 커지는 효과 */
transform: scale(5);
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
</code></pre>
</div>
</div>
</div>
<div class="modal__close">
<svg width="18" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
<path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</div>
</div>
</div>
</footer>
<!-- //footer -->
<script src="../assets/js/highlight.min.js"></script>
<script src="../assets/js/common.js"></script>
<script src="../assets/js/gsap.min.js"></script>
<script>
// 모든 좌표값 가져오기 getBoundingClientRect()
const cursor = document.querySelector(".mouse__cursor");
const cursorRect = cursor.getBoundingClientRect();
//figure부분에서 마우스를 움직일 때
document.querySelector(".mouse__wrap figure").addEventListener("mousemove", (e) => {
// 커서
//0.5초 동안
gsap.to(cursor, {
duration: .5,
// 동그라미 마우스 커서 방향별 값을 설정해주어야 움직임
left: e.pageX - cursorRect.width/2,
top: e.pageY - cursorRect.height/2,
});
//pageX, pageY : X좌표값, Y좌표값
//마우스 좌표 값 (페이지 기준)
let mousePageX = e.pageX;
let mousePageY = e.pageY;
//전체 가로
// window.innerWidth //1920 : 브라우저 크기 (스크롤 미포함)
// window.outerWidth //1920 : 브라우저 크기 (스크롤바 포함)
// window.screen.width //1920 : 화면 크기
//마우스 좌표값 가운데로 초기화
//전체 길이/2 - 마우스 좌표값 == 0
//window.innerWidth/2, window.innerHeight/2은 화면의 중앙이고 고정되어 있음.
//살짝의 오차를 주어 이미지에 움직이는 효과를 주기 위한 기본적인 방법으로 mousePageX, mousePageY(페이지 가운데 있다는 가정 하에 마우스 커서만큼의 값)을 빼주어야함.
//그럼 이미지의 움직임 가능(그림의 중앙 좌표값(?)centerPage이 바뀜)
//mousePageX 오른쪽으로 가면 페이지 절반 값보다 좌표값이 더 크므로 전체 - : 이미지 왼쪽으로 이동
//mousePageX 왼쪽으로 가면 페이지 절반 값보다 좌표값이 더 작으므로 전체 + : 이미지 오른쪽으로 이동
let centerPageX = window.innerWidth/2 - mousePageX;
let centerPageY= window.innerHeight/2 - mousePageY;
//이미지 움직이기
const imgMove = document.querySelector(".mouse__wrap figure img");
// imgMove.style.transform = "translate("+ centerPageX/20 +"px, "+ centerPageY/20 +"px)";
//gsap
//이미지를 움직이게 하는 0.3초동안의 움직임
//x축으로 centerPageX에서 20으로 나눈 값 : 조금 더 움직임을 위해? (더 많은 값으로 나눌수록 적게 이동함)
//커서와 같은 방향으로 움직이게 하려면 음수로 나누어주고, 반대방향으로 움직이게 하려면 양수로 나누어 주면 가능함.
// = imgMove.style.transform = "translate("+ centerPageX/20 +"px, "+ centerPageY/20 +"px)";
gsap.to (imgMove, {
duration: 0.3,
x: centerPageX/20,
y: centerPageY/20
})
//출력
document.querySelector(".mousePageX").textContent = mousePageX;
document.querySelector(".mousePageY").textContent = mousePageY;
document.querySelector(".centerPageX").textContent = centerPageX;
document.querySelector(".centerPageY").textContent = centerPageY;
});
</script>
</body>
</html>