MediaWiki

EventList.js:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
 
(未显示同一用户的3个中间版本)
第1行: 第1行:
$(function() {
$(function() {
mw.loader.load( mw.util.getUrl( 'MediaWiki:EventList.css', { action: 'raw', ctype: 'text/css' } ), 'text/css' );
    mw.loader.load(mw.util.getUrl('MediaWiki:EventList.css', { action: 'raw', ctype: 'text/css' }), 'text/css');


    var heightCache = {};
     // 计算并设置容器高度
   
     function calculateHeight($container) {
     // 为每个事件选项添加点击事件
         var baseHeight = 170; // 基础高度(到选项区域顶部)
     $('.event-option').click(function() {
         var totalHeight = baseHeight;
         var $this = $(this);
          
         var optionIndex = $this.data('option-index');
         // 计算选项容器内所有内容的实际高度
         var $container = $this.closest('.event-container');
         var $optionsContainer = $container.find('.event-options');
         var containerId = $container.data('event-id') || $container.index();
        var $effect = $container.find('.event-effect[data-effect-index="' + optionIndex + '"]');
         var $allEffects = $container.find('.event-effect');
          
          
         // 如果点击的是已经激活的选项
         // 遍历选项容器内的所有子元素(包括选项和效果)
         if ($this.hasClass('active')) {
         $optionsContainer.children().each(function() {
            // 移除激活状态
             totalHeight += $(this).outerHeight(true); // 包含 margin
            $this.removeClass('active');
         });
           
            // 添加隐藏动画类
            $effect.addClass('hiding');
           
            // 等待动画完成后隐藏元素
            setTimeout(function() {
                $effect.hide().removeClass('hiding');
                smoothAdjustHeight($container, containerId);
            }, 300);
           
        } else {
             // 移除其他选项的激活状态
            $container.find('.event-option').removeClass('active');
           
            // 隐藏其他效果
            $allEffects.not($effect).each(function() {
                var $this = $(this);
                if ($this.is(':visible')) {
                    $this.addClass('hiding');
                    setTimeout(function() {
                        $this.hide().removeClass('hiding');
                    }, 300);
                }
            });
           
            // 激活当前选项
            $this.addClass('active');
           
            // 延迟显示新效果,确保旧效果先隐藏
            setTimeout(function() {
                $effect.show();
                smoothAdjustHeight($container, containerId);
            }, $allEffects.filter(':visible').not($effect).length > 0 ? 150 : 0);
         }
    });
   
    // 平滑调整容器高度
    function smoothAdjustHeight($container, containerId) {
        // 先测量需要的高度
        var measuredHeight = measureHeight($container);
          
          
         // 如果高度没有变化,不执行动画
         // 添加底部间距
         if (heightCache[containerId] === measuredHeight) {
         totalHeight += 15;
            return;
        }
          
          
         // 更新缓存
         // 设置容器最小高度
         heightCache[containerId] = measuredHeight;
         $container.css('min-height', totalHeight + 'px');
          
          
         // 使用requestAnimationFrame确保动画流畅
         // 背景高度 = 总高度 - 顶部偏移(160px)
         requestAnimationFrame(function() {
         var bgHeight = totalHeight - 160;
            $container.css('min-height', measuredHeight + 'px');
        $container.find('.event-background').css('height', bgHeight + 'px');
            $container.find('.event-background').css('height', (measuredHeight - 160) + 'px');
        });
     }
     }
      
      
     // 测量所需高度
     // 初始化时计算所有容器的高度
     function measureHeight($container) {
     // 使用延迟确保图片和内容已加载
        var totalHeight = 170; // 基础高度
    function initializeHeights() {
       
         $('.event-container').each(function() {
        $container.find('.event-option').each(function() {
             calculateHeight($(this));
            totalHeight += $(this).outerHeight(true);
        });
       
         $container.find('.event-effect:visible').each(function() {
             totalHeight += $(this).outerHeight(true);
         });
         });
       
        return totalHeight + 15; // 添加底部边距
     }
     }
      
      
     // 初始化所有容器的高度
     // 页面加载完成后初始化
     $('.event-container').each(function() {
     initializeHeights();
        var $container = $(this);
   
        var containerId = $container.data('event-id') || $container.index();
    // 图片加载完成后重新计算
        var initialHeight = measureHeight($container);
    $('.event-image img').on('load', function() {
        heightCache[containerId] = initialHeight;
        initializeHeights();
        $container.css('min-height', initialHeight + 'px');
        $container.find('.event-background').css('height', (initialHeight - 160) + 'px');
     });
     });
      
      
     // 监听窗口大小变化,重新计算高度
     // 监听窗口大小变化,重新计算高度
     var resizeTimer;
     var resizeTimer;
     $(window).resize(function() {
     $(window).on('resize', function() {
         clearTimeout(resizeTimer);
         clearTimeout(resizeTimer);
         resizeTimer = setTimeout(function() {
         resizeTimer = setTimeout(function() {
             $('.event-container').each(function() {
             initializeHeights();
                 var $container = $(this);
        }, 250); // 防抖处理
                var containerId = $container.data('event-id') || $container.index();
    });
                smoothAdjustHeight($container, containerId);
   
    // 使用 MutationObserver 监听内容变化
    if (window.MutationObserver) {
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                 var $container = $(mutation.target).closest('.event-container');
                if ($container.length) {
                    calculateHeight($container);
                }
             });
             });
         }, 250);
         });
    });
       
        $('.event-container').each(function() {
            observer.observe(this, {
                childList: true,
                subtree: true,
                characterData: true
            });
        });
    }
});
});

2025年10月18日 (六) 13:41的最新版本

$(function() {
    mw.loader.load(mw.util.getUrl('MediaWiki:EventList.css', { action: 'raw', ctype: 'text/css' }), 'text/css');

    // 计算并设置容器高度
    function calculateHeight($container) {
        var baseHeight = 170; // 基础高度(到选项区域顶部)
        var totalHeight = baseHeight;
        
        // 计算选项容器内所有内容的实际高度
        var $optionsContainer = $container.find('.event-options');
        
        // 遍历选项容器内的所有子元素(包括选项和效果)
        $optionsContainer.children().each(function() {
            totalHeight += $(this).outerHeight(true); // 包含 margin
        });
        
        // 添加底部间距
        totalHeight += 15;
        
        // 设置容器最小高度
        $container.css('min-height', totalHeight + 'px');
        
        // 背景高度 = 总高度 - 顶部偏移(160px)
        var bgHeight = totalHeight - 160;
        $container.find('.event-background').css('height', bgHeight + 'px');
    }
    
    // 初始化时计算所有容器的高度
    // 使用延迟确保图片和内容已加载
    function initializeHeights() {
        $('.event-container').each(function() {
            calculateHeight($(this));
        });
    }
    
    // 页面加载完成后初始化
    initializeHeights();
    
    // 图片加载完成后重新计算
    $('.event-image img').on('load', function() {
        initializeHeights();
    });
    
    // 监听窗口大小变化,重新计算高度
    var resizeTimer;
    $(window).on('resize', function() {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function() {
            initializeHeights();
        }, 250); // 防抖处理
    });
    
    // 使用 MutationObserver 监听内容变化
    if (window.MutationObserver) {
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                var $container = $(mutation.target).closest('.event-container');
                if ($container.length) {
                    calculateHeight($container);
                }
            });
        });
        
        $('.event-container').each(function() {
            observer.observe(this, {
                childList: true,
                subtree: true,
                characterData: true
            });
        });
    }
});