iT邦幫忙

2024 iThome 鐵人賽

DAY 28
0
自我挑戰組

認識JavaScript系列 第 28

[第二十八天] 牛刀小試-圖片輪播

  • 分享至 

  • xImage
  •  
<html>
<head>
  <title>圖片輪播</title>
  <style>
    .carousel {
      position: relative;
      width: 300px;
      height: 200px;
      overflow: hidden;
      margin: auto;
      }
    .carousel img {
      width: 100%;
      height: 100%;
      display: none;
      }
    .carousel img.selected {
      display: block;
      }
    .carousel-controls {
      position: absolute;
      top: 50%;
      width: 100%;
      display: flex;
      justify-content: space-between;
      transform: translateY(-50%);
      }
    .modal {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.8);
      justify-content: center;
      align-items: center;
      }
    .modal img {
      width: 80%;
      max-width: 700px;
      height: auto;
      }
    .modal-text {
      color: white;
      position: absolute;
      bottom: 10%;
      text-align: center;
      font-size: 20px;
      }
    .close {
      position: absolute;
      top: 20px;
      right: 20px;
      color: white;
      font-size: 30px;
      cursor: pointer;
      }
  </style>
</head>
<body>
  <h1>圖片輪播</h1>
  <div class="carousel"> <!--輪播區-->
    <img src="https://illustcenter.com/wp-content/uploads/2024/10/rdesign_17985.png" alt="img 1" class="selected" data-text="聖誕老公公">
    <img src="https://illustcenter.com/wp-content/uploads/2024/10/rdesign_17981.png" alt="img 2" data-text="聖誕襪">
    <img src="https://illustcenter.com/wp-content/uploads/2024/10/rdesign_17972.png" alt="img 3" data-text="聖誕樹">
    
    <div class="carousel-controls">
      <button id="btnPre" onclick="fPre()">‹</button>
      <button id="btnNext" onclick="fNext()">›</button>
    </div>
  </div>
  <div id="modal" class="modal"> <!--放大區-->
        <span class="close" id="btnClose" onclick="fClose()">&times;</span>
        <img id="imgModal" src="">
        <div class="modal-text" id="divModal"></div>
    </div>
</body>
</html>

主要利用CSS去呈現點選該張圖片後的放大。

const images = document.querySelectorAll('.carousel img');
const btnPre = document.getElementById('btnPre');
const btnNext = document.getElementById('btnNext');
const modal = document.getElementById('modal');
const imgModal = document.getElementById('imgModal');
const divModal = document.getElementById('divModal');
const btnClose = document.getElementById('btnClose');
let currentIndex = 0;

function showImage(index) {
	images.forEach((img, i) => {
  	img.classList.remove('selected');
    if (i === index) {
    	img.classList.add('selected');
    }
  });
}
function fNext() {
	currentIndex = (currentIndex + 1) % images.length;
  showImage(currentIndex);
}

function fPre() {
	currentIndex = (currentIndex - 1 + images.length) % images.length;
  showImage(currentIndex);
}

images.forEach(img => {
	img.addEventListener('click', () => {
  	modal.style.display = 'flex';
    imgModal.src = img.src;
    divModal.textContent = img.dataset.text;
  });
});

function fClose() {
	modal.style.display = 'none';
}

若是需要自動換圖,就設下計時器呼叫function即可!


上一篇
[第二十七天] 試著解題 2652. Sum Multiples
下一篇
[第二十九天] 試著解題 2677. Chunk Array
系列文
認識JavaScript30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言