模板:轮播图:修订间差异

来自黑神话百科
跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
第1行: 第1行:
<div class="carousel">
<div class="slider-container">
   <div class="carousel-images">
   <div class="slider" id="slider"></div>
    {{#ask: [[Category:Images]]
   <div class="arrow prev" onclick="prevSlide()"><</div>
      |?image
   <div class="arrow next" onclick="nextSlide()">></div>
      |?link
   <div class="dots" id="dotsContainer"></div>
      |format=template
      |template=轮播图模板
      |limit=10
    }}
  </div>
   <div class="prev" onclick="moveSlide(-1)"><</div>
   <div class="next" onclick="moveSlide(1)">></div>
   <div class="indicators">
    {{#ask:[[Category:Images]]
      |format=template
      |template=指示器
      |limit=10
    }}
  </div>
</div>
</div>
{{#css:.carousel {
<includeonly>
<script type="text/javascript">
    let slider = document.getElementById('slider');
    let dotsContainer = document.getElementById('dotsContainer');
    let currentSlide = 0;
 
    function createSlider(images, links) {
      images.forEach((obj, index) => {
        let img = document.createElement('img');
        img.src = obj.src;
        img.style.opacity = index === 0 ? 1 : 0;
        img.onclick = () => window.open(obj.link, '_blank');
        slider.appendChild(img);
 
        let dot = document.createElement('span');
        dot.className = 'dot';
        dot.onclick = () => jumpToSlider(index);
        dotsContainer.appendChild(dot);
      });
 
      dotsContainer.children[0].classList.add('active');
     
      setInterval(() => {
        nextSlide();
      }, 1000 * 5)
    }
    function updateSlider() {
      Array.from(slider.children).forEach((slide, index) => {
        slide.style.opacity = index === currentSlide ? 1 : 0;
      });
 
      Array.from(dotsContainer.children).forEach((dot, index) => {
        dot.classList.toggle('active', index === currentSlide);
      });
    }
    function nextSlide() {
      currentSlide = (currentSlide + 1)%images.length;
      updateSlider();
    }
    function prevSlide() {
      currentSlide = (currentSlide - 1 + images.length)%images.length;
      updateSlider();
    }
    function jumpToSlider(slideIndex) {
      currentSlide = slideIndex;
      updateSlider();
    }
</script>
</includeonly>
{{#css:
.slider-container {
   position: relative;
   position: relative;
  overflow: hidden;
   width: 100%;
   width: 100%;
  height: 300px;
}
}
.carousel-images {
.slider {
   display: flex;
   position: relative;
   transition: transform 0.5s ease;
   width: 100%;
  height: 469px;
  overflow: hidden;
}
}
.slide {
.slider img {
   min-width: 100%;
   opacity: 0;
   box-sizing: border-box;
  width: 100%;
   height: 100%;
  object-fit: cover;
  object-position: center;
  transition: opacity 700ms ease-in-out;
  position: absolute;
}
}
div.prev, div.next {
.arrow {
  cursor: pointer;
   position: absolute;
   position: absolute;
   top: 50%;
   top: 50%;
   transform: translateY(-50%);
   width: auto;
   background-color: rgba(255, 255, 255, 0.5);
  padding: 16px;
   border: none;
  margin-top: -22px;
   cursor: pointer;
  color: white;
  font-weight: bold;
   font-size: 18px;
  transition: 0.6s ease;
   border-radius: 0 3px 3px 0;
  user-select: none;
   -webkit-user-select: none;
}
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}
 
.prev:hover, .next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}
}
div.prev {
 
.dots {
  width: max-content;
  text-align: center;
  position: absolute;
   left: 0;
   left: 0;
}
div.next {
   right: 0;
   right: 0;
  bottom: 43px;
  margin: auto;
}
}
.indicator {
.dot {
  height: 12px;
  width: 94px;
  margin: 0 22px;
  background-color: #D9D9D9;
   display: inline-block;
   display: inline-block;
  width: 10px;
  height: 10px;
  margin: 5px;
  background-color: gray;
  border-radius: 50%;
   cursor: pointer;
   cursor: pointer;
  transition: background-color 0.6s ease;
}
.dot.active {
  background-color: #717171;
}
}
}}
}}
<includeonly>
<script type="text/javascript">
let currentSlide = 0;
function showSlide(index) {
  const slides = document.querySelectorAll('.slide');
  if(index >= slides.length) currentSlide = 0;
  if(index < 0) currentSlide = slides.length - 1;
  const offset = -currentSlide * 100;
  document.querySelector('.carousel-images').style.transform = `translateX(${offset}%)`;
}
function moveSlide(direction) {
  showSlide(currentSlide += direction);
}
showSlide(currentSlide);
</script>
</includeonly>

2024年8月18日 (日) 05:29的版本