打开/关闭菜单
26
6679
46
1.2万
OGAS数据中枢
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Widget:GameSwitcher:修订间差异

来自OGAS数据中枢
弃权者留言 | 贡献
无编辑摘要
弃权者留言 | 贡献
无编辑摘要
标签手工回退
 
(未显示同一用户的8个中间版本)
第1行: 第1行:
<noinclude>
<noinclude>
本Widget为少女前线系列多游戏首页提供换交互逻辑
少女前线系列多游戏首页切换Widget。调用方式:<code><nowiki>{{#widget:GameSwitcher}}</nowiki></code>
不接受参数,直接在首页调用:<code><nowiki>{{#widget:GFHomepageSwitcher}}</nowiki></code>
</noinclude><includeonly><script>
</noinclude><includeonly><script>
(function () {
( function () {
   'use strict';
   'use strict';


第16行: 第15行:
     var canonical = document.querySelector( 'link[rel="canonical"]' );
     var canonical = document.querySelector( 'link[rel="canonical"]' );
     if ( canonical ) {
     if ( canonical ) {
       var match = canonical.href.match( /^(https?:\/\/[^\/]+(?:\/[^\/]+)*?)\/wiki\// );
       var m = canonical.href.match( /^(https?:\/\/[^\/]+(?:\/[^\/]+)*?)\/wiki\// );
       if ( match ) { return match[ 1 ] + '/api.php'; }
       if ( m ) { return m[ 1 ] + '/api.php'; }
     }
     }
     return window.location.origin + '/api.php';
     return window.location.origin + '/api.php';
第28行: 第27行:
       .then( function ( data ) {
       .then( function ( data ) {
         var pages = data.query.pages;
         var pages = data.query.pages;
         var page = pages[ Object.keys( pages )[ 0 ] ];
         var page = pages[ Object.keys( pages )[ 0 ] ];
         if ( page.imageinfo && page.imageinfo[ 0 ] ) {
         if ( page.imageinfo && page.imageinfo[ 0 ] ) {
           callback( page.imageinfo[ 0 ].url );
           callback( page.imageinfo[ 0 ].url );
第36行: 第35行:
   }
   }


   function applyFadeColors() {
   function applyFadeColor() {
     var style = getComputedStyle( document.documentElement );
     var style = getComputedStyle( document.documentElement );
     var bg = style.getPropertyValue( '--color-surface-0' ).trim()
     var bg = style.getPropertyValue( '--color-surface-0' ).trim()
           || style.getPropertyValue( '--background-color-base' ).trim()
           || style.getPropertyValue( '--background-color-base' ).trim()
           || '#1d1e20';
           || '#1d1e20';
     var defs = {
     var el = document.getElementById( 'gf-hero-fade-bottom' );
      'gf-hero-fade-bottom': 'linear-gradient(to bottom, transparent 0%, ' + bg + ' 100%)',
    if ( el ) {
       'gf-hero-fade-left':  'linear-gradient(to right, '  + bg + ' 0%, transparent 100%)',
       el.style.background = 'linear-gradient(to bottom, transparent 0%, ' + bg + ' 100%)';
      'gf-hero-fade-right':  'linear-gradient(to left, '  + bg + ' 0%, transparent 100%)'
     }
    };
     Object.keys( defs ).forEach( function ( id ) {
      var el = document.getElementById( id );
      if ( el ) { el.style.background = defs[ id ]; }
    } );
   }
   }


   /* 当前正在显示的游戏,用于防止动画中途重复触发 */
function moveIndicator( tab ) {
   var indicator = document.getElementById( 'gf-tab-indicator' );
  if ( !indicator ) { return; }
  indicator.style.width    = tab.offsetWidth + 'px';
  indicator.style.transform = 'translateX(' + tab.offsetLeft + 'px)';
}
 
   var currentGame = 'gf';
   var currentGame = 'gf';
   var switching = false;
   var switching   = false;
 
   var pending     = null;
   function switchGame( game ) {
    if ( game === currentGame || switching ) { return; }
     switching = true;


  function doSwitch( game, tabEl ) {
     var slides = document.querySelectorAll( '#gf-homepage .gf-hero-slide' );
     var slides = document.querySelectorAll( '#gf-homepage .gf-hero-slide' );
     var panels = document.querySelectorAll( '#gf-homepage .gf-content-panel' );
     var panels = document.querySelectorAll( '#gf-homepage .gf-content-panel' );
第67行: 第65行:
     var inSlide  = document.getElementById( 'hero-' + game );
     var inSlide  = document.getElementById( 'hero-' + game );


    /* 新图先置于底层不透明度0,再淡入;旧图同时淡出 */
     if ( inSlide ) {
     if ( inSlide ) {
       inSlide.style.zIndex   = '1';
       inSlide.style.zIndex = '1';
       inSlide.style.opacity  = '0';
       inSlide.classList.add( 'active' );
      inSlide.style.transition = 'opacity 0.5s ease';
      /* 强制reflow让transition生效 */
      void inSlide.offsetWidth;
      inSlide.style.opacity = '1';
     }
     }
     if ( outSlide ) {
     if ( outSlide ) {
       outSlide.style.zIndex   = '0';
       outSlide.style.zIndex = '0';
       outSlide.style.opacity  = '1';
       outSlide.classList.remove( 'active' );
      outSlide.style.transition = 'opacity 0.5s ease';
      void outSlide.offsetWidth;
      outSlide.style.opacity  = '0';
     }
     }


    /* 内容面板切换 */
    panels.forEach( function ( p ) {
      p.classList.toggle( 'active', p.id === 'content-' + game );
    } );
    /* tab状态 */
     tabs.forEach( function ( t ) {
     tabs.forEach( function ( t ) {
       var on = t.getAttribute( 'data-game' ) === game;
       var on = t.getAttribute( 'data-game' ) === game;
       t.classList.toggle( 'active', on );
       t.classList.toggle( 'active', on );
       t.setAttribute( 'aria-selected', on ? 'true' : 'false' );
       t.setAttribute( 'aria-selected', on ? 'true' : 'false' );
    } );
    if ( tabEl ) { moveIndicator( tabEl ); }
    panels.forEach( function ( p ) {
      p.classList.toggle( 'active', p.id === 'content-' + game );
     } );
     } );


     /* 动画结束后清理zIndex,解除锁定 */
     currentGame = game;
    switching  = true;
 
     setTimeout( function () {
     setTimeout( function () {
       slides.forEach( function ( s ) {
       slides.forEach( function ( s ) { s.style.zIndex = ''; } );
        s.style.zIndex   = '';
      switching = false;
         s.style.opacity   = s.id === 'hero-' + game ? '1' : '0';
      if ( pending && pending.game !== currentGame ) {
         s.style.transition = '';
        var p = pending;
         pending = null;
        doSwitch( p.game, p.tabEl );
      } else {
        pending = null;
      }
    }, 520 );
   }
 
  function switchGame( game, tabEl ) {
    if ( game === currentGame ) { return; }
    if ( switching ) {
      pending = { game: game, tabEl: tabEl };
      var tabs = document.querySelectorAll( '#gf-homepage .gf-tab' );
      tabs.forEach( function ( t ) {
        var on = t.getAttribute( 'data-game' ) === game;
        t.classList.toggle( 'active', on );
         t.setAttribute( 'aria-selected', on ? 'true' : 'false' );
       } );
       } );
       currentGame = game;
       if ( tabEl ) { moveIndicator( tabEl ); }
       switching  = false;
       return;
     }, 520 );
     }
    doSwitch( game, tabEl );
   }
   }


第112行: 第121行:
     if ( !tabs.length ) { return; }
     if ( !tabs.length ) { return; }


    /* 预加载并注入背景图 */
     Object.keys( heroImages ).forEach( function ( game ) {
     Object.keys( heroImages ).forEach( function ( game ) {
       getWikiImageUrl( heroImages[ game ], function ( url ) {
       getWikiImageUrl( heroImages[ game ], function ( url ) {
         var el = document.getElementById( 'hero-' + game );
         var bg  = document.getElementById( 'hero-' + game + '-bg' );
         if ( el ) { el.style.backgroundImage = 'url("' + url + '")'; }
         var fg  = document.getElementById( 'hero-' + game + '-fg' );
        var val = 'url("' + url + '")';
        if ( bg ) { bg.style.backgroundImage = val; }
        if ( fg ) { fg.style.backgroundImage = val; }
       } );
       } );
     } );
     } );


     applyFadeColors();
     applyFadeColor();
 
    var firstActive = document.querySelector( '#gf-homepage .gf-tab.active' );
    if ( firstActive ) {
      setTimeout( function () { moveIndicator( firstActive ); }, 50 );
    }


     tabs.forEach( function ( tab ) {
     tabs.forEach( function ( tab ) {
       tab.addEventListener( 'click', function () {
       tab.addEventListener( 'click', function () {
         switchGame( this.getAttribute( 'data-game' ) );
         switchGame( this.getAttribute( 'data-game' ), this );
       } );
       } );
       tab.addEventListener( 'keydown', function ( e ) {
       tab.addEventListener( 'keydown', function ( e ) {
         if ( e.key === 'Enter' || e.key === ' ' ) {
         if ( e.key === 'Enter' || e.key === ' ' ) {
           e.preventDefault();
           e.preventDefault();
           switchGame( this.getAttribute( 'data-game' ) );
           switchGame( this.getAttribute( 'data-game' ), this );
         }
         }
       } );
       } );
    } );
    window.addEventListener( 'resize', function () {
      var active = document.querySelector( '#gf-homepage .gf-tab.active' );
      if ( active ) { moveIndicator( active ); }
     } );
     } );
   }
   }

2026年6月20日 (六) 23:23的最新版本

少女前线系列多游戏首页切换Widget。调用方式:{{#widget:GameSwitcher}}