Guest User

Untitled

a guest
Feb 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="ko">
  3.  
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
  7. <title>Carousel</title>
  8. <style>
  9. .carouselbox {
  10. font-family: helvetica, sans-serif;
  11. font-size: 14px;
  12. width: 100px;
  13. position: relative;
  14. margin: 1em;
  15. border: 1px solid #ccc;
  16. box-shadow: 2px 2px 10px #ccc;
  17. overflow: hidden;
  18. }
  19.  
  20. .content {
  21. margin: 0;
  22. padding: 0;
  23. }
  24.  
  25. .content li {
  26. font-size: 100px;
  27. margin: 0;
  28. padding: 0;
  29. width: 100%;
  30. list-style: none;
  31. text-align: center;
  32. }
  33.  
  34. .active {
  35. height: 150px;
  36. }
  37.  
  38. .active li {
  39. position: absolute;
  40. /* top: 200px; */
  41. }
  42.  
  43. .active li.current {
  44. /* top: 30px; */
  45. }
  46.  
  47. .active .buttons {
  48. padding: 5px 0;
  49. background: #eee;
  50. text-align: center;
  51. z-index: 10;
  52. position: relative;
  53. }
  54.  
  55. .carouselbox button {
  56. border: none;
  57. display: none;
  58. }
  59.  
  60. .active button {
  61. display: block;
  62. }
  63.  
  64. .offscreen {
  65. position: absolute;
  66. left: -2000px;
  67. }
  68.  
  69. .active li {
  70. position: absolute;
  71.  
  72. opacity: 0;
  73. transform: translateX(100px);
  74. transition: transform 1s, opacity 1s;
  75. }
  76.  
  77. .active li.current {
  78. opacity: 1;
  79. transform: translateX(0);
  80. transition: transform 1s, opacity 1s;
  81. }
  82.  
  83. .active li.prev {
  84. opacity: 0;
  85. transform: translateX(-100px);
  86. transition: transform 1s, opacity 1s;
  87.  
  88. }
  89. </style>
  90. </head>
  91.  
  92. <body>
  93. <div id="container">
  94. <div class="carouselbox active">
  95. <div class="buttons">
  96. <button class="prev">
  97. ◀ <span class="offscreen">Previous</span>
  98. </button>
  99. <button class="next">
  100. <span class="offscreen">Next</span> ▶
  101. </button>
  102. </div>
  103. <ol class="content">
  104. <li>1</li>
  105. <li>2</li>
  106. <li>3</li>
  107. <li>4</li>
  108. </ol>
  109. </div>
  110. </div>
  111. <script>
  112. carousel = (function () {
  113. var box = document.querySelector('.carouselbox');
  114. var next = box.querySelector('.next');
  115. var prev = box.querySelector('.prev');
  116. var items = box.querySelectorAll('.content li');
  117. var counter = 0;
  118. var amount = items.length;
  119. var current = items[0];
  120. box.classList.add('active');
  121. function navigate(direction) {
  122. items.forEach(v => {
  123. v.classList.remove('prev')
  124. v.classList.remove('current')
  125. })
  126.  
  127. counter = counter + direction;
  128. if (direction === -1 && counter < 0) {
  129. counter = amount - 1;
  130. }
  131. if (direction === 1 && !items[counter]) {
  132. current.classList.add('prev')
  133. counter = 0;
  134. }
  135.  
  136. console.log(counter)
  137. current = items[counter];
  138. prev = items[counter === 0 ? items.length - 1: counter - 1];
  139. current.classList.add('current');
  140. prev.classList.add('prev');
  141. }
  142. next.addEventListener('click', function (ev) {
  143. navigate(1);
  144. });
  145. prev.addEventListener('click', function (ev) {
  146. navigate(-1);
  147. });
  148. navigate(0);
  149. })();
  150. </script>
  151. </body>
  152.  
  153. </html>
Add Comment
Please, Sign In to add comment