MediaWiki:Common.js:修订间差异
恢复每页 60 件 标签:手工回退 |
首页 v1:分类切换与卡片跳转 |
||
| (未显示同一用户的2个中间版本) | |||
| 第176行: | 第176行: | ||
} ); | } ); | ||
} ); | } ); | ||
/* ---- 类型折叠 ---- */ | /* ---- 类型折叠 ---- */ | ||
| 第269行: | 第268行: | ||
var t = card.getAttribute( 'data-title' ); | var t = card.getAttribute( 'data-title' ); | ||
if ( !t ) { return; } | if ( !t ) { return; } | ||
/* 卡片里的图片自带「文件:」链接,这里拦掉,统一走武器页 */ | |||
e.preventDefault(); | |||
/* 按住 Ctrl / Cmd / Shift 时在新标签页打开,与普通链接习惯一致 */ | /* 按住 Ctrl / Cmd / Shift 时在新标签页打开,与普通链接习惯一致 */ | ||
if ( e.metaKey || e.ctrlKey || e.shiftKey ) { | if ( e.metaKey || e.ctrlKey || e.shiftKey ) { | ||
| 第284行: | 第285行: | ||
go( card ); | go( card ); | ||
} ); | } ); | ||
}() ); | |||
/* ===== 首页:分类切换 · 卡片跳转(2026-11-13) ===== */ | |||
( function () { | |||
'use strict'; | |||
var root = document.querySelector( '.hp' ); | |||
if ( ! root ) { return; } | |||
var tabbar = root.querySelector( '.tabbar' ); | |||
var tabs = [].slice.call( root.querySelectorAll( '.tab' ) ); | |||
var panes = [].slice.call( root.querySelectorAll( '.pane' ) ); | |||
var body = root.querySelector( '.body' ); | |||
var cur = 0; | |||
var hTimer = 0; | |||
function show( i, focus, mode ) { | |||
if ( i < 0 || i >= tabs.length || ! panes[ i ] ) { return; } | |||
var h0 = body ? body.offsetHeight : 0; | |||
cur = i; | |||
tabs.forEach( function ( t, k ) { | |||
var on = ( k === i ); | |||
t.classList.toggle( 'on', on ); | |||
t.setAttribute( 'aria-selected', on ? 'true' : 'false' ); | |||
t.setAttribute( 'tabindex', on ? '0' : '-1' ); | |||
if ( on && focus ) { t.focus(); } | |||
} ); | |||
panes.forEach( function ( p, k ) { | |||
p.classList.toggle( 'on', k === i ); | |||
} ); | |||
if ( body && h0 ) { | |||
var h1 = body.offsetHeight; | |||
if ( h1 && h1 !== h0 ) { | |||
clearTimeout( hTimer ); | |||
body.style.transition = 'none'; | |||
body.style.height = h0 + 'px'; | |||
body.style.overflow = 'hidden'; | |||
body.offsetHeight; /* reflow */ | |||
body.style.transition = 'height .4s cubic-bezier(.22,.72,.28,1)'; | |||
body.style.height = h1 + 'px'; | |||
hTimer = setTimeout( function () { | |||
body.style.transition = ''; | |||
body.style.height = ''; | |||
body.style.overflow = ''; | |||
}, 440 ); | |||
} | |||
} | |||
try { | |||
var url = '#' + tabs[ i ].id; | |||
if ( mode === 'init' ) { | |||
history.replaceState( null, '', url ); | |||
} else if ( mode !== 'silent' && location.hash !== url ) { | |||
history.pushState( null, '', url ); | |||
} | |||
} catch ( e ) {} | |||
} | |||
function fromHash() { | |||
var h = ''; | |||
try { h = decodeURIComponent( location.hash.replace( '#', '' ) ); } catch ( e ) {} | |||
if ( ! h ) { return -1; } | |||
var idx = -1; | |||
tabs.forEach( function ( t, k ) { | |||
if ( idx < 0 && ( t.id === h || t.textContent.indexOf( h ) > -1 ) ) { idx = k; } | |||
} ); | |||
if ( idx < 0 ) { | |||
panes.forEach( function ( p, k ) { | |||
if ( idx < 0 && p.id === h ) { idx = k; } | |||
} ); | |||
} | |||
return idx; | |||
} | |||
if ( tabs.length && tabbar ) { | |||
tabs.forEach( function ( t, i ) { | |||
t.addEventListener( 'click', function () { show( i, false ); } ); | |||
t.addEventListener( 'keydown', function ( e ) { | |||
var d = ( e.key === 'ArrowRight' ) ? 1 : ( ( e.key === 'ArrowLeft' ) ? -1 : 0 ); | |||
if ( d ) { | |||
e.preventDefault(); | |||
show( ( i + d + tabs.length ) % tabs.length, true ); | |||
} else if ( e.key === 'Enter' || e.key === ' ' ) { | |||
e.preventDefault(); | |||
show( i, false ); | |||
} | |||
} ); | |||
} ); | |||
[].slice.call( root.querySelectorAll( '.nav' ) ).forEach( function ( b ) { | |||
b.addEventListener( 'click', function () { | |||
var d = parseInt( b.getAttribute( 'data-nav' ), 10 ) || 0; | |||
show( ( cur + d + tabs.length ) % tabs.length, false ); | |||
} ); | |||
b.addEventListener( 'keydown', function ( e ) { | |||
if ( e.key === 'Enter' || e.key === ' ' ) { | |||
e.preventDefault(); | |||
b.click(); | |||
} | |||
} ); | |||
} ); | |||
var init = fromHash(); | |||
if ( init > -1 ) { show( init, false, 'init' ); } | |||
window.addEventListener( 'hashchange', function () { | |||
var i = fromHash(); | |||
if ( i < 0 ) { i = 0; } | |||
if ( i !== cur ) { show( i, false, 'silent' ); } | |||
} ); | |||
} | |||
/* 卡片 / 条目跳转 */ | |||
function go( el ) { | |||
var url = el.getAttribute( 'data-href' ); | |||
if ( ! url || url === '#' ) { return; } | |||
location.href = url; | |||
} | |||
[].slice.call( root.querySelectorAll( '[data-href]' ) ).forEach( function ( el ) { | |||
el.addEventListener( 'click', function ( e ) { | |||
if ( e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1 ) { | |||
window.open( el.getAttribute( 'data-href' ), '_blank' ); | |||
return; | |||
} | |||
go( el ); | |||
} ); | |||
el.addEventListener( 'keydown', function ( e ) { | |||
if ( e.key === 'Enter' || e.key === ' ' ) { | |||
e.preventDefault(); | |||
go( el ); | |||
} | |||
} ); | |||
} ); | |||
}() ); | }() ); | ||