MediaWiki:Common.js:修订间差异

来自Age Of History 2 Chinese Wiki
跳转至:导航、​搜索
创建页面,内容为“这里的任何JavaScript将为所有用户在每次页面加载时加载。:​ 轮播图功能:​ mw.hook('wikipage.content').add(function($content) { // 查找页面中所有轮播图 $content.find('.mw-carousel').each(function() { const $carousel = $(this); const $items = $carousel.find('.carousel-item'); const $indicators = $carousel.find('.carousel-indicators'); const $prevBtn = $carousel.find('.carousel-prev'); const $nextBtn…”
 
无编辑摘要
第1行: 第1行:
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/**
* MediaWiki轮播图JavaScript
* 为轮播图提供交互功能
*/
/* 轮播图功能 */
/* 轮播图功能 */
mw.hook('wikipage.content').add(function($content) {
mw.hook('wikipage.content').add(function($content) {
   // 查找页面中所有轮播图
   // 查找页面中所有轮播图
   $content.find('.mw-carousel').each(function() {
   $content.find('.wiki-carousel').each(function() {
     const $carousel = $(this);
     const carousel = $(this);
     const $items = $carousel.find('.carousel-item');
     const slides = carousel.find('.wiki-carousel-slide');
     const $indicators = $carousel.find('.carousel-indicators');
     const slideContainer = carousel.find('.wiki-carousel-slides');
     const $prevBtn = $carousel.find('.carousel-prev');
     const prevBtn = carousel.find('.wiki-carousel-prev');
     const $nextBtn = $carousel.find('.carousel-next');
     const nextBtn = carousel.find('.wiki-carousel-next');
    const indicators = carousel.find('.wiki-carousel-indicators');
      
      
     let currentIndex = 0;
     let currentIndex = 0;
     const slideCount = $items.length;
    let autoplayInterval = null;
     const slideCount = slides.length;
      
      
     if (slideCount === 0) return;
     if (slideCount === 0) return;
第17行: 第22行:
     // 创建指示器
     // 创建指示器
     for (let i = 0; i < slideCount; i++) {
     for (let i = 0; i < slideCount; i++) {
       const $indicator = $('<div class="indicator"></div>');
       const indicator = $('<div class="wiki-carousel-indicator"></div>');
       if (i === 0) $indicator.addClass('active');
       if (i === 0) indicator.addClass('active');
        
        
       $indicator.on('click', function() {
       indicator.on('click', function() {
         goToSlide(i);
         goToSlide(i);
       });
       });
        
        
       $indicators.append($indicator);
       indicators.append(indicator);
     }
     }
      
      
     // 初始化第一张幻灯片
     // 初始化轮播图状态
     $items.eq(0).addClass('active');
     updateCarousel();
      
      
     // 给每个幻灯片添加点击事件
     // 自动播放
     $items.each(function() {
     startAutoplay();
      const $item = $(this);
   
      const link = $item.data('link');
    // 绑定按钮事件
     
    prevBtn.on('click', function(e) {
      if (link && link.length > 0) {
      e.preventDefault();
        $item.css('cursor', 'pointer').on('click', function() {
      e.stopPropagation();
          window.location.href = link;
       prevSlide();
        });
       }
     });
     });
      
      
     // 切换到指定幻灯片
     nextBtn.on('click', function(e) {
    function goToSlide(index) {
       e.preventDefault();
      $items.removeClass('active');
       e.stopPropagation();
       $indicators.find('.indicator').removeClass('active');
       nextSlide();
        
     });
      $items.eq(index).addClass('active');
       $indicators.find('.indicator').eq(index).addClass('active');
     
      currentIndex = index;
     }
      
      
     // 上一张
     // 上一张
     function prevSlide() {
     function prevSlide() {
       const newIndex = (currentIndex - 1 + slideCount) % slideCount;
       currentIndex = (currentIndex - 1 + slides.length) % slides.length;
       goToSlide(newIndex);
      updateCarousel();
       resetAutoplay();
     }
     }
      
      
     // 下一张
     // 下一张
     function nextSlide() {
     function nextSlide() {
       const newIndex = (currentIndex + 1) % slideCount;
       currentIndex = (currentIndex + 1) % slides.length;
       goToSlide(newIndex);
      updateCarousel();
       resetAutoplay();
     }
     }
      
      
     // 绑定按钮事件
     // 切换到指定幻灯片
     $prevBtn.on('click', function(e) {
     function goToSlide(index) {
       e.preventDefault();
       currentIndex = index;
       e.stopPropagation();
      updateCarousel();
       prevSlide();
       resetAutoplay();
    }
   
    // 鼠标悬停事件
    carousel.on('mouseenter', function() {
       stopAutoplay();
     });
     });
      
      
     $nextBtn.on('click', function(e) {
     carousel.on('mouseleave', function() {
       e.preventDefault();
       startAutoplay();
      e.stopPropagation();
      nextSlide();
     });
     });
      
      
     // 自动播放
     // 为每个幻灯片添加鼠标悬停和点击事件
     let autoplayInterval = setInterval(nextSlide, 5000);
     slides.each(function(index) {
   
      const slide = $(this);
    $carousel.on('mouseenter', function() {
      const link = slide.find('.wiki-carousel-slide-link').attr('href');
      clearInterval(autoplayInterval);
     
    }).on('mouseleave', function() {
      // 鼠标悬停事件
      autoplayInterval = setInterval(nextSlide, 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;
        });
      }
     });
     });
      
      
     // 触摸滑动支持
     // 触摸事件支持
     let touchStartX = 0;
     let touchStartX = 0;
     let touchEndX = 0;
     carousel.on('touchstart', function(e) {
   
    $carousel.on('touchstart', function(e) {
       touchStartX = e.originalEvent.touches[0].clientX;
       touchStartX = e.originalEvent.touches[0].clientX;
      stopAutoplay();
     });
     });
      
      
     $carousel.on('touchend', function(e) {
     carousel.on('touchend', function(e) {
       touchEndX = e.originalEvent.changedTouches[0].clientX;
       const touchEndX = e.originalEvent.changedTouches[0].clientX;
      const diff = touchEndX - touchStartX;
        
        
      // 判断滑动方向
       if (diff > 50) {
       if (touchEndX < touchStartX - 50) {
         // 向右滑动,显示上一张
         nextSlide(); // 左滑,下一张
        prevSlide();
       } else if (touchEndX > touchStartX + 50) {
       } else if (diff < -50) {
         prevSlide(); // 右滑,上一张
         // 向左滑动,显示下一张
        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:19的版本

/**
 * MediaWiki轮播图JavaScript
 * 为轮播图提供交互功能
 */
/* 轮播图功能 */
mw.hook('wikipage.content').add(function($content) {
  // 查找页面中所有轮播图
  $content.find('.wiki-carousel').each(function() {
    const carousel = $(this);
    const slides = carousel.find('.wiki-carousel-slide');
    const slideContainer = carousel.find('.wiki-carousel-slides');
    const prevBtn = carousel.find('.wiki-carousel-prev');
    const nextBtn = carousel.find('.wiki-carousel-next');
    const indicators = carousel.find('.wiki-carousel-indicators');
    
    let currentIndex = 0;
    let autoplayInterval = null;
    const slideCount = slides.length;
    
    if (slideCount === 0) return;
    
    // 创建指示器
    for (let i = 0; i < slideCount; i++) {
      const indicator = $('<div class="wiki-carousel-indicator"></div>');
      if (i === 0) indicator.addClass('active');
      
      indicator.on('click', function() {
        goToSlide(i);
      });
      
      indicators.append(indicator);
    }
    
    // 初始化轮播图状态
    updateCarousel();
    
    // 自动播放
    startAutoplay();
    
    // 绑定按钮事件
    prevBtn.on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
      prevSlide();
    });
    
    nextBtn.on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
      nextSlide();
    });
    
    // 上一张
    function prevSlide() {
      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() {
      startAutoplay();
    });
    
    // 为每个幻灯片添加鼠标悬停和点击事件
    slides.each(function(index) {
      const slide = $(this);
      const link = slide.find('.wiki-carousel-slide-link').attr('href');
      
      // 鼠标悬停事件
      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;
        });
      }
    });
    
    // 触摸事件支持
    let touchStartX = 0;
    carousel.on('touchstart', function(e) {
      touchStartX = e.originalEvent.touches[0].clientX;
      stopAutoplay();
    });
    
    carousel.on('touchend', function(e) {
      const touchEndX = e.originalEvent.changedTouches[0].clientX;
      const diff = touchEndX - touchStartX;
      
      if (diff > 50) {
        // 向右滑动,显示上一张
        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();
    }
  });
});