-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliderEffect07.html
569 lines (524 loc) · 24.7 KB
/
sliderEffect07.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<!DOCTYPE html>
<html lang="ko">
<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>슬라이드 이펙트03</title>
<link rel="stylesheet" href="../assets/css/slider.css">
<link rel="stylesheet" href="../assets/css/deracula.css">
<style>
/* slider */
.slider__wrap {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.slider__img { /* 이미지가 보이는 영역 */
position: relative;
width: 800px;
height: 450px;
/* overflow: hidden; */
}
.slider__inner {/* 전체 이미지를 감싸고 있는 부모 : 움직이는 부분 속성주기 */
display: flex;
flex-wrap: wrap;
width: 10000px; /* 스크립트로 처리할 것이므로 안깨지게 끔 width값 주면 됨 */
height: 450px;
position: relative;
left: -800px; /* 슬라이더 초기값 */
}
/* 마지막에서 처음으로 순간이동 */
.slider__inner.transition {
transition: all 0.3s;
}
.slider { /* 개별적인 이미지 (각각의 이미지 설정) */
position: relative;
width: 800px;
height: 450px;
}
.slider span {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 5px 10px;
}
@media(max-width : 800px){
.slider__img {
width: 400px;
height: 250px;
}
.slider {
width: 400px;
height: 250px;
}
}
/* 슬라이드 버튼 연속으로 누를 수 없게 하기 01 */
.slider__btn.disable {
pointer-events: none;
}
.slider__btn button {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background: rgba(0,0,0,0.4);
text-align: center;
line-height: 50px;
transition: all 0.2s;
display: block;
color: #fff;
pointer-events: auto;
border: 0;
cursor: pointer;
}
.slider__btn button:hover {
border-radius: 50px;
background: rgba(38, 52, 103, 0.4);
}
.slider__btn button.prev {
left: 0;
}
.slider__btn button.next {
right: 0;
}
.slider__dot {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 20px;
}
.slider__dot .dot {
width: 20px;
height: 20px;
background: rgba(0,0,0,0.4);
display: inline-block;
border-radius: 50%;
text-indent: -9999px;
transition: all 0.3s;
margin: 3px;
}
.slider__dot .dot.active {
background: rgba(255,255,255,0.9);
}
.slider__dot .play {
display: none;
/* display: inline-block; */
width: 23px;
height: 20px;
margin-left: 7px;
vertical-align: -4px;
border-left: 14px solid #fff;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
}
.slider__dot .stop {
display: none;
/* display: inline-block; */
width: 16px;
height: 20px;
vertical-align: -4px;
position: relative;
}
.slider__dot .stop::before {
content: '';
width: 5px;
height: 20px;
background: #fff;
position: absolute;
left: 0;
top: 0;
}
.slider__dot .stop::after {
content: '';
width: 5px;
height: 20px;
background: #fff;
position: absolute;
right: 0;
top: 0;
}
.slider__dot .show {
display: inline-block;
}
</style>
</head>
<body class="img11">
<header id="header">
<h1>Javascript Slider Effect07</h1>
<p>슬라이드 이펙트 - 이미지슬라이드(버튼, dot메뉴, 무한, 플레이 버튼, 정지버튼, 자동 플레이)</p>
<ul>
<li><a href="sliderEffect01.html">1</a></li>
<li><a href="sliderEffect02.html">2</a></li>
<li><a href="sliderEffect03.html">3</a></li>
<li><a href="sliderEffect04.html">4</a></li>
<li><a href="sliderEffect05.html">5</a></li>
<li><a href="sliderEffect06.html">6</a></li>
<li class="active"><a href="sliderEffect07.html">7</a></li>
</ul>
</header>
<!-- //header -->
<main id="main">
<section id="sliderType07">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider s1" role="group" aria-label="1/5">
<span>이미지1</span>
<img src="../assets/img/Mountian_bg01.jpg" alt="이미지6">
</div>
<div class="slider s2" role="group" aria-label="2/5">
<span>이미지2</span>
<img src="../assets/img/Mountian_bg02.jpg" alt="이미지7">
</div>
<div class="slider s3" role="group" aria-label="3/5">
<span>이미지3</span>
<img src="../assets/img/Mountian_bg03.jpg" alt="이미지8">
</div>
<div class="slider s4" role="group" aria-label="4/5">
<span>이미지4</span>
<img src="../assets/img/Mountian_bg04.jpg" alt="이미지9">
</div>
<div class="slider s5" role="group" aria-label="5/5">
<span>이미지5</span>
<img src="../assets/img/Mountian_bg05.jpg" alt="이미지10">
</div>
</div>
</div>
<div class="slider__btn">
<button href="#" class="prev" role="button" aria-label="왼쪽 이미지">prev</button>
<button href="#" class="next" role="button" aria-label="오른쪽 이미지">next</button>
</div>
<div class="slider__dot">
<!-- <a href="#" class="dot active">이미지1</a>
<a href="#" class="dot">이미지2</a>
<a href="#" class="dot">이미지3</a>
<a href="#" class="dot">이미지4</a>
<a href="#" class="dot">이미지5</a>
<a href="#" class="play">재생</a>
<a href="#" class="stop">정지5</a> -->
</div>
</div>
</section>
</main>
<!-- //main -->
<footer id="footer" >
<div class="modal__wrap">
<div class="modal__btn">Source</div>
<div class="modal__cont">
<div class="modal__box">
<div class="title">
<span class="dot" aria-label="ture"></span>
<div class="tabs">
<div class="active">
<span class="favicon" aria-label="ture">
<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="ture">
<svg width="17" 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="16" 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="ture">
<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="16" 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="ture">
<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">const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = document.querySelector(".slider__img"); //보여지는 영역
const sliderInner = document.querySelector(".slider__inner"); //움직이는 영역
const slider = sliderWrap.querySelectorAll(".slider"); //각각의 이미지
let currentIndex = 0; //현재 보이는 이미지
let sliderCount = slider.length; //이미지 총 갯수
let sliderWidth = sliderImg.offsetWidth; //이미지 가로 값 (offsetWidth : 가로 값 구하는 메서드)
let sliderClone = sliderInner.firstElementChild.cloneNode(true); // 제이쿼리는 클론만 적기 복사를 위해서는 true 꼭 적기 : 첫번째 자식 요소 복사하기(이미지)
sliderInner.appendChild(sliderClone); //첫번째 이미지를 마지막에 넣어준다.
function sliderEffect(){
currentIndex++;
sliderInner.style.transition = "all 0.6s";
sliderInner.style.transform = "translateX(-" + sliderWidth * currentIndex + "px)";
// sliderInner.style.transform = "translateX(-800px)"; 800 * 1
// sliderInner.style.transform = "translateX(-1600px)"; 800 * 2
// sliderInner.style.transform = "translateX(-2400px)"; 800 * 3
// sliderInner.style.transform = "translateX(-3200px)"; 800 * 4
// sliderInner.style.transform = "translateX(-4000px)"; 800 * 5
// sliderInner.style.transform = "translateX(-4800px)"; 800 * 6
//마지막 사진에 위치했을 때 : 효율성보단 간편한 코드
// if(currentIndex == sliderCount){
// sliderInner.style.transition = "0s";
// sliderInner.style.transform = "translateX(0px)";
// currentIndex = 0;
// 이 코드를 사용하게 된다면 마지막에서 첫번쨰로 넘어갈 때 어색하게 넘어가진다. (애니메이션 적용이 되지 않기 때문.)
// }
//마지막 사진에 위치했을 때 : 효율성보단 간편한 코드
//첫번째로 넘어갈 때 스무스하게 넘어가도록 한 것.
if(currentIndex == sliderCount){
setTimeout(() => {
sliderInner.style.transition = "0s";
sliderInner.style.transform = "translateX(0px)";
},700);
currentIndex = 0; //마지막 사진이 왔을 때 다음 이미지로 넘어가지 않고 다시 초기화 시키기
}
}
setInterval(sliderEffect,2000);
//setInterval : 여러번 반복시켜 주는 것
//setTimeout : 한 번만 사용
</code></pre>
</div>
<div>
<pre><code class="language-html"><section id="sliderType03">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider">
<img src="../assets/img/Mountian_bg01.jpg" alt="이미지6">
</div>
<div class="slider">
<img src="../assets/img/Mountian_bg02.jpg" alt="이미지7">
</div>
<div class="slider">
<img src="../assets/img/Mountian_bg03.jpg" alt="이미지8">
</div>
<div class="slider">
<img src="../assets/img/Mountian_bg04.jpg" alt="이미지9">
</div>
<div class="slider">
<img src="../assets/img/Mountian_bg05.jpg" alt="이미지10">
</div>
</div>
</div>
</div>
</section>
</code></pre>
</div>
<div>
<pre><code class="language-css">/* slider */
.slider__wrap {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.slider__img { /* 이미지가 보이는 영역 */
position: relative;
width: 800px;
height: 450px;
overflow: hidden;
}
.slider__inner {/* 전체 이미지를 감싸고 있는 부모 : 움직이는 부분 속성주기 */
display: flex;
flex-wrap: wrap;
width: 4800px; /* 총 여섯개의 이미지 */
height: 450px;
}
.slider { /* 개별적인 이미지 (각각의 이미지 설정) */
position: relative;
width: 800px;
height: 450px;
}
.slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 5px 10px;
}
.slider:nth-child(1)::before {content: '이미지6';}
.slider:nth-child(2)::before {content: '이미지7';}
.slider:nth-child(3)::before {content: '이미지8';}
.slider:nth-child(4)::before {content: '이미지9';}
.slider:nth-child(5)::before {content: '이미지10';}
.slider:nth-child(6)::before {content: '이미지6';}
@media(max-width : 800px){
.slider__img {
width: 400px;
height: 250px;
}
}
</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>
const sliderWrap = document.querySelector(".slider__wrap"); //전체 이미지 슬라이드
const sliderImg = document.querySelector(".slider__img"); //보여지는 영역
const sliderInner = document.querySelector(".slider__inner"); //움직이는 영역
const slider = document.querySelectorAll(".slider"); //이미지
const sliderDot = document.querySelector(".slider__dot"); //닷 메뉴
const sliderBtn = document.querySelector(".slider__btn"); //버튼
const sliderBtnPrev = document.querySelector(".slider__btn .prev"); //왼쪽 버튼
const sliderBtnNext = document.querySelector(".slider__btn .next"); //오른쪽 버튼
// const sliderBtnPlay = document.querySelector(".slider__dot .play"); //플레이 버튼
// const sliderBtnStop = document.querySelector(".slider__dot .stop"); //정지 버튼
let currentIndex = 0, //현재 이미지
sliderLength = slider.length, //슬라이더 총 길이
sliderWidth = slider[0].offsetWidth, //슬라이더 가로 값
sliderFirst = slider[0], //첫번째 이미지
sliderLast = slider[sliderLength -1], //마지막 이미지
cloneFirst = sliderFirst.cloneNode(true), //첫번째 이미지 복사
cloneLast = sliderLast.cloneNode(true); //마지막 이미지 복사
let dotIndex = ""; //닷 생성시킬 문자열
let interval = 3000;
let sliderTimer = "";
function init(){
imgClone(); // 이미지 복사 바로 실행시키기
createDot(); // 닷 버튼 생성시키기
// autoPlay(); // 자동플레이
}
init();
// 자동플레이 함수
function autoPlay(){
sliderTimer = setInterval(() => {
let intervalNum = currentIndex + 1; //매개변수 값
gotoSlider(intervalNum);
}, interval);
}
function stopPlay(){
clearInterval(sliderTimer);
}
// 이미지 복사 함수
function imgClone(){
sliderInner.appendChild(cloneFirst);
sliderInner.insertBefore(cloneLast, sliderFirst);
}
// 닷 버튼 생성 함수
function createDot(){
for(let i=0; i<sliderLength; i++){
dotIndex += "<a href='#' class='dot'>이미지1</a>";
}
dotIndex += "<a href='#' class='play show'></a>";
dotIndex += "<a href='#' class='stop'></a>";
sliderDot.innerHTML = dotIndex;
sliderDot.firstChild.classList.add("active");
//닷 버튼 움직이기
// sliderDot.forEach((e, i) => {cu
// if(e.classList.){}
// });
const dotActive = document.querySelectorAll(".slider__dot .dot");
document.querySelectorAll(".slider__dot .dot").forEach((dot, index)=>{
dot.addEventListener("click", ()=>{
dotActive.forEach(el => el.classList.remove("active"));
dotActive[index].classList.add("active");
gotoSlider(index);
});
});
}
// 슬라이드 함수
function gotoSlider(index){
sliderInner.classList.add("transition");
let posInitial = sliderInner.offsetLeft; // -800px
// if(direction == -1){
// // sliderInner.style.left = "800px"; //왼쪽으로 800만큼 움직이기
// //부모박스 offsetLeft값을 구하여 더하는 이유는? sliderWidth는 슬라이더 하나만의 가로값이기 때문에 이동시 늘어나거나 줄어드는 값을 구할 수 없다 때문에 그 슬라이더를 감싸는 부모박스의 가로값을 구하여 늘어나는 값 만큼 길이를 더하여 추가시킨 것 이다.
// sliderInner.style.left = (posInital + sliderWidth) + "px"; //왼쪽으로 슬라이더 가로값만큼 움직이기. 슬라이더의 부모박스의 왼쪽값을 더한 이유는?
// currentIndex --;
// }else if(direction == 1){
// // sliderInner.style.left = "-800px";
// sliderInner.style.left = (posInital - sliderWidth) + "px";
// currentIndex ++;
// }
// 위 식을 간편하게 작업
sliderInner.style.left = -sliderWidth * (index + 1) + "px";
currentIndex = index;
}
// 순간이동 함수
function checkIndex(){
//슬라이드가 끝나면 transition 클래스를 다시 지워서 transition효과가 나오지 않도록 하고 이미지 순간이동 시키기
sliderInner.classList.remove("transition");
if(currentIndex == sliderLength){
sliderInner.style.left = -(1 * sliderWidth) + "px";
currentIndex = 0;
}
if(currentIndex == -1){
sliderInner.style.left = -(sliderLength * sliderWidth) + "px";
currentIndex = sliderLength - 1;
}
}
// 실행문
sliderBtnPrev.addEventListener("click" ,() => {
let prevIndex = currentIndex - 1;
gotoSlider(prevIndex); //매개변수로 전달
});
sliderBtnNext.addEventListener("click" ,() => {
let nextIndex = currentIndex + 1;
gotoSlider(nextIndex); //매개변수로 전달
});
document.querySelector(".slider__dot .play").addEventListener("click", () => {
document.querySelector(".play").classList.remove("show");
document.querySelector(".stop").classList.add("show");
autoPlay();
});
document.querySelector(".slider__dot .stop").addEventListener("click", () => {
document.querySelector(".stop").classList.remove("show");
document.querySelector(".play").classList.add("show");
stopPlay();
});
// sliderInner.addEventListener("mouseenter", stopPlay);
// sliderInner.addEventListener("mouseleave", autoPlay);
sliderInner.addEventListener("transitionend", checkIndex);
</script>
</body>
</html>