MediaWiki:Common.js:修订间差异

来自Age Of History 2 Chinese Wiki
跳转至:导航、​搜索
无编辑摘要
无编辑摘要
第1行: 第1行:
/**
// Wait for document ready
* MediaWiki轮播图JavaScript
mw.hook('wikipage.content').add(function() {
* 为轮播图提供交互功能
   // Initialize carousels
*/
   initializeCarousels();
/* 轮播图功能 */
});
mw.hook('wikipage.content').add(function($content) {
 
   // 查找页面中所有轮播图
function initializeCarousels() {
   $content.find('.wiki-carousel').each(function() {
  // Find all carousel containers
    const carousel = $(this);
  const carousels = document.querySelectorAll('.modern-carousel');
    const slides = carousel.find('.wiki-carousel-slide');
 
    const slideContainer = carousel.find('.wiki-carousel-slides');
  carousels.forEach(function(carousel) {
     const prevBtn = carousel.find('.wiki-carousel-prev');
     const track = carousel.querySelector('.carousel-track');
     const nextBtn = carousel.find('.wiki-carousel-next');
     const items = carousel.querySelectorAll('.carousel-item');
     const indicators = carousel.find('.wiki-carousel-indicators');
     const prevButton = carousel.querySelector('.carousel-button.prev');
      
     const nextButton = carousel.querySelector('.carousel-button.next');
    // 检查图片是否加载
    slides.each(function() {
      const slide = $(this);
      const imgElement = slide.find('.wiki-carousel-slide-image');
      const imgSrc = imgElement.attr('data-image');
     
      // 如果背景图片样式没有被应用,尝试另一种方式
      if (!imgElement.css('background-image') || imgElement.css('background-image') === 'none') {
        imgElement.css('background-image', 'url(' + imgSrc + ')');
      }
    });
   
     let currentIndex = 0;
     let currentIndex = 0;
    let autoplayInterval = null;
    const slideCount = slides.length;
      
      
     if (slideCount === 0) return;
     // Set initial width for track
    track.style.width = `${items.length * 100}%`;
      
      
     // 创建指示器
     // Function to move to specific slide
     for (let i = 0; i < slideCount; i++) {
     function moveToSlide(index) {
       const indicator = $('<div class="wiki-carousel-indicator"></div>');
       // Ensure index is within bounds
       if (i === 0) indicator.addClass('active');
      if (index < 0) index = items.length - 1;
       if (index >= items.length) index = 0;
        
        
       indicator.on('click', function() {
       currentIndex = index;
        goToSlide(i);
     
      });
      // Move track
      track.style.transform = `translateX(-${currentIndex * (100 / items.length)}%)`;
        
        
       indicators.append(indicator);
       // Update active class
      items.forEach(item => item.classList.remove('active'));
      items[currentIndex].classList.add('active');
     }
     }
      
      
     // 初始化轮播图状态
     // Add hover effect
     updateCarousel();
     items.forEach(function(item, index) {
   
      item.addEventListener('mouseenter', function() {
    // 自动播放
        moveToSlide(index);
    startAutoplay();
       });
   
    // 绑定按钮事件
    prevBtn.on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
       prevSlide();
     });
     });
      
      
     nextBtn.on('click', function(e) {
     // Add navigation
       e.preventDefault();
    prevButton.addEventListener('click', function() {
      e.stopPropagation();
       moveToSlide(currentIndex - 1);
      nextSlide();
     });
     });
      
      
     // 上一张
     nextButton.addEventListener('click', function() {
    function prevSlide() {
       moveToSlide(currentIndex + 1);
      currentIndex = (currentIndex - 1 + slides.length) % slides.length;
      updateCarousel();
      resetAutoplay();
    }
   
    // 下一张
    function nextSlide() {
       currentIndex = (currentIndex + 1) % slides.length;
      updateCarousel();
      resetAutoplay();
    }
   
    // 切换到指定幻灯片
    function goToSlide(index) {
      currentIndex = index;
      updateCarousel();
      resetAutoplay();
    }
   
    // 鼠标悬停事件
    carousel.on('mouseenter', function() {
      stopAutoplay();
     });
     });
      
      
     carousel.on('mouseleave', function() {
     // Initialize first slide as active
      startAutoplay();
    moveToSlide(0);
    });
      
      
     // 为每个幻灯片添加鼠标悬停和点击事件
     // Auto-rotate (optional)
    slides.each(function(index) {
    let autoRotate = setInterval(function() {
      const slide = $(this);
       moveToSlide(currentIndex + 1);
      const link = slide.find('.wiki-carousel-slide-link').attr('href');
     }, 5000);
     
      // 鼠标悬停事件
      slide.on('mouseenter', function() {
        goToSlide(index);
        stopAutoplay();
      });
     
      // 如果有链接,添加点击事件
       if (link && link.length > 0) {
        slide.find('.wiki-carousel-slide-inner').css('cursor', 'pointer').on('click', function() {
          window.location.href = link;
        });
      }
     });
      
      
     // 触摸事件支持
     // Pause auto-rotate on hover
    let touchStartX = 0;
     carousel.addEventListener('mouseenter', function() {
     carousel.on('touchstart', function(e) {
       clearInterval(autoRotate);
       touchStartX = e.originalEvent.touches[0].clientX;
      stopAutoplay();
     });
     });
      
      
     carousel.on('touchend', function(e) {
    // Resume auto-rotate on mouseleave
       const touchEndX = e.originalEvent.changedTouches[0].clientX;
     carousel.addEventListener('mouseleave', function() {
      const diff = touchEndX - touchStartX;
       autoRotate = setInterval(function() {
     
         moveToSlide(currentIndex + 1);
      if (diff > 50) {
       }, 5000);
        // 向右滑动,显示上一张
        prevSlide();
      } else if (diff < -50) {
         // 向左滑动,显示下一张
        nextSlide();
       }
     
      startAutoplay();
     });
     });
   
    // 更新轮播图显示
    function updateCarousel() {
      slides.removeClass('active');
      indicators.find('.wiki-carousel-indicator').removeClass('active');
     
      slides.css('transform', `translateX(-${currentIndex * 100}%)`);
      slides.eq(currentIndex).addClass('active');
      indicators.find('.wiki-carousel-indicator').eq(currentIndex).addClass('active');
    }
   
    // 启动自动播放
    function startAutoplay() {
      if (autoplayInterval === null) {
        autoplayInterval = setInterval(function() {
          nextSlide();
        }, 5000); // 5秒切换一次
      }
    }
   
    // 停止自动播放
    function stopAutoplay() {
      if (autoplayInterval !== null) {
        clearInterval(autoplayInterval);
        autoplayInterval = null;
      }
    }
   
    // 重置自动播放
    function resetAutoplay() {
      stopAutoplay();
      startAutoplay();
    }
   });
   });
});
}

2025年5月1日 (四) 19:40的版本

// Wait for document ready
mw.hook('wikipage.content').add(function() {
  // Initialize carousels
  initializeCarousels();
});

function initializeCarousels() {
  // Find all carousel containers
  const carousels = document.querySelectorAll('.modern-carousel');
  
  carousels.forEach(function(carousel) {
    const track = carousel.querySelector('.carousel-track');
    const items = carousel.querySelectorAll('.carousel-item');
    const prevButton = carousel.querySelector('.carousel-button.prev');
    const nextButton = carousel.querySelector('.carousel-button.next');
    let currentIndex = 0;
    
    // Set initial width for track
    track.style.width = `${items.length * 100}%`;
    
    // Function to move to specific slide
    function moveToSlide(index) {
      // Ensure index is within bounds
      if (index < 0) index = items.length - 1;
      if (index >= items.length) index = 0;
      
      currentIndex = index;
      
      // Move track
      track.style.transform = `translateX(-${currentIndex * (100 / items.length)}%)`;
      
      // Update active class
      items.forEach(item => item.classList.remove('active'));
      items[currentIndex].classList.add('active');
    }
    
    // Add hover effect
    items.forEach(function(item, index) {
      item.addEventListener('mouseenter', function() {
        moveToSlide(index);
      });
    });
    
    // Add navigation
    prevButton.addEventListener('click', function() {
      moveToSlide(currentIndex - 1);
    });
    
    nextButton.addEventListener('click', function() {
      moveToSlide(currentIndex + 1);
    });
    
    // Initialize first slide as active
    moveToSlide(0);
    
    // Auto-rotate (optional)
    let autoRotate = setInterval(function() {
      moveToSlide(currentIndex + 1);
    }, 5000);
    
    // Pause auto-rotate on hover
    carousel.addEventListener('mouseenter', function() {
      clearInterval(autoRotate);
    });
    
    // Resume auto-rotate on mouseleave
    carousel.addEventListener('mouseleave', function() {
      autoRotate = setInterval(function() {
        moveToSlide(currentIndex + 1);
      }, 5000);
    });
  });
}