12 슬라이드
1. 기본 슬라이드
1.1. fadeInOut
1.1.1. 메서드 활용
- 예제
- HTML
- CSS
- JS
리소스 이미지
http://qwerew.cafe24.com/images/1.jpghttp://qwerew.cafe24.com/images/2.jpghttp://qwerew.cafe24.com/images/3.jpghttp://qwerew.cafe24.com/images/4.jpghttp://qwerew.cafe24.com/images/5.jpghttp://qwerew.cafe24.com/images/6.jpghttp://qwerew.cafe24.com/images/7.jpghttp://qwerew.cafe24.com/images/8.jpghttp://qwerew.cafe24.com/images/9.jpghttp://qwerew.cafe24.com/images/10.jpg1<body>2 <div id="content">3 <h3>Simple Crossfade Waterfalls Slideshow</h3>4 <ul id="crossfade">5 <li>6 <a href="#"><img src="http://qwerew.cafe24.com/images/1.jpg" alt="" /></a>7 <p>1번</p>8 </li>9 <li>10 <a href="#"><img src="http://qwerew.cafe24.com/images/2.jpg" alt="" /></a>11 <p>2번</p>12 </li>13 <li>14 <a href="#"><img src="http://qwerew.cafe24.com/images/3.jpg" alt="" /></a>15 <p>3번</p>16 </li>17 </ul>18 </div>19</body>1#crossfade {2 position: relative;3 margin: auto;4 padding: 0;5 list-style-type: none;6 width: 600px;7 height: 400px;8 overflow: hidden;9}10
11#crossfade li {12 position: absolute;13 width: 600px;14 height: 400px;15}16
17#crossfade p {18 position: absolute;19 bottom: 0;20 padding: 20px;21 color: #fff;22 background: #000;23 background-color: rgba(0, 0, 0, 0.6);24 margin: 0;25 left: 0;26 right: 0;27}방법1
1$(() => {2 $('#crossfade li').hide().filter(':first').show();3 setInterval(slideshow, 3000);4 function slideshow() {5 $('#crossfade li:first').fadeOut('slow').next().fadeIn('slow').end().appendTo('#crossfade');6 }7});방법2
1$(() => {2 let idx = 0;3 $('#crossfade li').hide();4 $('#crossfade li').eq(0).show();5 setInterval(slideshow, 3000);6 function slideshow() {7 $('#crossfade li').eq(idx).fadeOut('slow');8 idx++;9 if (idx > $('#crossfade li').length - 1) {10 idx = 0;11 }12 $('#crossfade li').eq(idx).fadeIn('slow');13 }14});1.1.2. 클래스 활용
- HTML
- CSS
- JS
1<body>2 <div id="content">3 <h3>Simple Crossfade Waterfalls Slideshow</h3>4 <ul id="crossfade">5 <li>6 <a href="#"><img src="http://qwerew.cafe24.com/images/1.jpg" alt="" /></a>7 <p>1번</p>8 </li>9 <li>10 <a href="#"><img src="http://qwerew.cafe24.com/images/2.jpg" alt="" /></a>11 <p>2번</p>12 </li>13 <li>14 <a href="#"><img src="http://qwerew.cafe24.com/images/3.jpg" alt="" /></a>15 <p>3번</p>16 </li>17 </ul>18 </div>19</body>1#crossfade {2 position: relative;3 margin: auto;4 padding: 0;5 list-style-type: none;6 width: 600px;7 height: 400px;8 overflow: hidden;9}10
11#crossfade li {12 position: absolute;13 width: 600px;14 height: 400px;15 opacity: 0;16 transition: opacity 0.5s;17}18
19#crossfade p {20 position: absolute;21 bottom: 0;22 padding: 20px;23 color: #fff;24 background: #000;25 background-color: rgba(0, 0, 0, 0.6);26 margin: 0;27 left: 0;28 right: 0;29}1$(() => {2 $('#crossfade li').hide().filter(':first').show();3 setInterval(slideshow, 3000);4 function slideshow() {5 $('#crossfade li:first').fadeOut('slow').next().fadeIn('slow').end().appendTo('#crossfade');6 }7});1.2. 이동
HTML
1<div id="content">2 <h3>Slideshow</h3>3 <ul id="crossfade">4 <li>5 <a href="#"><img src="http://qwerew.cafe24.com/images/1.jpg" alt="" /></a>6 <p>1번</p>7 </li>8 <li>9 <a href="#"><img src="http://qwerew.cafe24.com/images/2.jpg" alt="" /></a>10 <p>2번</p>11 </li>12 <li>13 <a href="#"><img src="http://qwerew.cafe24.com/images/3.jpg" alt="" /></a>14 <p>3번</p>15 </li>16 </ul>17</div>CSS
1#crossfade {2 position: relative;3 margin: auto;4 padding: 0;5 list-style-type: none;6 width: 600px;7 height: 400px;8 overflow: hidden;9}10
11#crossfade li {12 position: absolute;13 width: 600px;14 height: 400px;15}16
17#crossfade p {18 position: absolute;19 bottom: 0;20 padding: 20px;21 color: #fff;22 background: #000;23 background-color: rgba(0, 0, 0, 0.6);24 margin: 0;25 left: 0;26 right: 0;27}JQurey1
1const slide = $('#crossfade li');2
3// 정렬4slide.each((i, o) => {5 $(o).css('left', i * 100 + '%');6});7
8let idx = 0;9
10// 무한루프를 위한 setInterval11setInterval(() => {12 slide13 .eq(idx)14 .stop()15 .animate({ left: -100 + '%' }, 1000, function () {16 $(this).css('left', (slide.length - 1) * 100 + '%');17 });18 idx++;19 if (idx > slide.length - 1) {20 idx = 0;21 }22 slide23 .eq(idx)24 .stop()25 .animate({ left: 0 + '%' }, 1000);26}, 3000); // 3초마다 반복JQurey2
1 const slide = $('#crossfade li');2 let current = 0;3 // 정렬4 slide.each((i, o) => {5 $(o).css('left', i * 100 + '%');6 });7
8 setInterval(function () {9 var prev = slide.eq(current);10 move(prev, 0, '-100%');11 current++;12 if (current == slide.length) {13 current = 0;14 }15 var next = slide.eq(current);16 move(next, '100%', '0%');17 }, 3000);18
19 function move(tg, start, end) {20 tg.css('left', start).stop().animate({ left: end }, 1000);21 }- 코드
- HTML
- CSS
- JQ
- JS
1<div class="slide_wrap">2 <div class="brand_visual">3 <ul>4 <li class="visual_0"><a href="#">배너이미지1</a></li>5 <li class="visual_1"><a href="#">배너이미지2</a></li>6 <li class="visual_2"><a href="#">배너이미지3</a></li>7 </ul>8 </div>9 <div class="btns">10 <img src="img/left.png" class="prev" width="30" height="50" alt="" />11 <img src="img/right.png" class="next" width="30" height="50" alt="" />12 </div>13 <ul class="buttons">14 <li class="on"><a href="#">배너1</a></li>15 <li><a href="#">배너2</a></li>16 <li><a href="#">배너3</a></li>17 </ul>18</div>1/* common */2html {3 width: 100%;4 height: 100%;5 overflow-y: scroll;6}7html,8body,9div,10span,11applet,12object,13iframe,14h1,15h2,16h3,17h4,18h5,19h6,20p,21blockquote,22pre,23a,24abbr,25acronym,26address,27big,28cite,29code,30del,31dfn,32em,33img,34ins,35kbd,36q,37s,38samp,39small,40strike,41strong,42sub,43sup,44tt,45var,46b,47u,48i,49center,50dl,51dt,52dd,53ol,54ul,55li,56fieldset,57form,58label,59legend,60table,61caption,62tbody,63tfoot,64thead,65tr,66th,67td,68article,69aside,70canvas,71details,72embed,73figure,74figcaption,75footer,76header,77hgroup,78menu,79nav,80output,81ruby,82section,83summary,84time,85mark,86audio,87video {88 margin: 0px;89 padding: 0px;90 font: inherit;91 vertical-align: baseline;92}93body {94 font-size: 12px;95 font-family: Dotum, Arial;96 color: #74767a;97 line-height: 120%;98}99a,100a:link,101a:visited {102 color: #74767a;103 text-decoration: none;104}105ul,106ol {107 list-style: none;108}109table,110fieldset,111th,112td,113img {114 border: none;115}116img,117input,118select {119 vertical-align: middle;120}1.slide_wrap{position:relative;}2.brand_visual ul {3 overflow: hidden;4 position: relative;5 width: 100%;6 height: clamp(50vh,500px,20vh);7}8.brand_visual ul li {9 position: absolute;10 width: 100%;11 height: 100%;12}13 /* 글씨 제거 */14.brand_visual ul li a {15 display: block;16 text-indent: -9999px;17}18
19/* visual_0, visual_1, visual_2 위치정렬 */20.brand_visual .visual_0 {21 left: 0;22 background: url(img/0.png) 50% 0 no-repeat;23}24.brand_visual .visual_1 {25 left: 100%;26 background: url(img/1.png) 50% 0 no-repeat;27}28.brand_visual .visual_2 {29 left: 200%;30 background: url(img/2.png) 50% 0 no-repeat;31}32
33/* 화살표 */34.btns {35 position: relative;36 top: -250px;37 width: 100%;38}39.btns .prev {40 position: absolute;41 left: 100px;42}43.btns .next {44 position: absolute;45 right: 100px;46}47
48/* 버튼 위치 지정 */49.buttons {50 display: flex;51 position: absolute;52 left: 50%;53 top: -135px;54 gap:10px;55 transform:translateX(-50%);56}57.buttons li {58 background: url(img/btnVisual.png) 0 -16px no-repeat;59 width: 14px;60 height: 15px;61 overflow: hidden;62 cursor: pointer;63}64/* 글씨 제거 */65.buttons li a {66 display: block;67 text-indent: -9999px;68}69/* 활성화된 버튼 */70.buttons li.on {71 background-position: 0 0;72}1$(() => {2 const visual = $('.brand_visual>ul>li');3 const button = $('.buttons>li');4 const leftBtn = $('.btns .prev');5 const rightBtn = $('.btns .next');6 let setIntervalId;7 const counter = $('.counter');8
9 let current = 0;10
11 function timer() {12 setIntervalId = setInterval(function () {13 changeSlide((current + 1) % visual.length); // 0%3=0/1%3=1/2%3=2/3%3=014 }, 3000);15 }16
17 function move(tg, start, end) {18 tg.css('left', start).stop().animate({ left: end }, { duration: 500, ease: 'easeOutCubic' });19 }20
21 function cnt(num) {22 counter.html(`${num + 1}`);23 }24
25 button.on('click', function () {26 const i = $(this).index();27 changeSlide(i);28 });29
30 function changeSlide(i, direction = 'right') {31 if (current == i) return;32 const currentEl = visual.eq(current);33 const nextEl = visual.eq(i);34 if (direction === 'right') {35 move(currentEl, 0, '-100%');36 move(nextEl, '100%', 0);37 } else {38 move(currentEl, 0, '100%');39 move(nextEl, '-100%', 0);40 }41 button.eq(current).removeClass('on');42 button.eq(i).addClass('on');43 cnt(i);44 current = i;45 }46
47 $('.slide_wrap').on({48 mouseover: function () {49 clearInterval(setIntervalId);50 },51 mouseout: timer,52 });53
54 rightBtn.click(function () {55 changeSlide((current + 1) % visual.length);56 return false;57 });58
59 leftBtn.click(function () {60 changeSlide((current - 1 + visual.length) % visual.length, 'left');61 return false;62 });63
64 timer(); // timer를 마지막에 호출하여 초기 슬라이드 쇼를 시작65});1const visual = document.querySelectorAll('#brandVisual>ul>li');2const button = document.querySelectorAll('#buttonList>li');3const leftBtn = document.querySelector('.btnImg .prev');4const rightBtn = document.querySelector('.btnImg .next');5let current = 0;6let setIntervalId;7let isMove = false;8
9// 슬라이드 이동10function moveSlide(prevIndex, nextIndex) {11 if (isMove) return;12 isMove = true;13 move(visual[prevIndex], 0, '-100%');14 button[prevIndex].classList.remove('on');15 move(visual[nextIndex], '100%', 0);16 button[nextIndex].classList.add('on');17 current = nextIndex;18}19
20// 애니메이션21function move(tg, start, end) {22 let keyframes = [{ left: start }, { left: end }];23 let options = { duration: 1000, fill: 'forwards' };24 let animation = tg.animate(keyframes, options);25 animation.onfinish = () => {26 isMove = false;27 };28}29
30// 타이머31function timer() {32 setIntervalId = setInterval(() => {33 let nextIndex = (current + 1) % visual.length;34 moveSlide(current, nextIndex);35 }, 3000);36}37
38setTimeout(timer, 1000);39
40// 이벤트 리스너들41document.querySelector('#wrap').addEventListener('mouseover', () => clearInterval(setIntervalId));42document.querySelector('#wrap').addEventListener('mouseout', timer);43
44button.forEach((btn, i) => {45 btn.addEventListener('click', () => moveSlide(current, i));46});47
48rightBtn.addEventListener('click', (e) => {49 e.preventDefault();50 let nextIndex = (current + 1) % visual.length;51 moveSlide(current, nextIndex);52});53
54leftBtn.addEventListener('click', (e) => {55 e.preventDefault();56 let nextIndex = (current - 1 + visual.length) % visual.length;57 moveSlide(current, nextIndex);58});