MediaWiki:Common.js:修订间差异
来自神国反恶俗
跳到导航跳到搜索
无编辑摘要 |
无编辑摘要 |
||
| 第4行: | 第4行: | ||
/* ========================================================== | /* ========================================================== | ||
一、把页面里的目录(TOC)搬到左侧栏 | 一、把页面里的目录(TOC)搬到左侧栏 | ||
========================================================== */ | ========================================================== */ | ||
function moveTOC() { | function moveTOC() { | ||
| 第34行: | 第33行: | ||
/* ========================================================== | /* ========================================================== | ||
二、吸顶样式 | |||
顶部两条通栏固定;左侧栏吸顶但不出现滚动条 | |||
========================================================== */ | ========================================================== */ | ||
var STICKY_CSS = [ | var STICKY_CSS = [ | ||
' | '.sg-sticky-bar{', | ||
' position:fixed;top:0;z-index:401;', | ' position:fixed;left:0;right:0;top:0;z-index:401;', | ||
' background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.18);', | ' background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.18);', | ||
' padding: | ' padding:3px 10px;font-size:87%;line-height:1.3;', | ||
'}', | '}', | ||
' | '.sg-sticky-bar a{', | ||
' color:#36c;text-decoration:none;padding:2px 6px;', | |||
' display:inline-block;', | |||
' color:#36c;text-decoration:none;padding:2px | |||
'}', | '}', | ||
' | '.sg-sticky-bar a:hover{text-decoration:underline;}', | ||
'#column-one{', | '#column-one{', | ||
' position:sticky;top:var(--sg-top-offset,0px);', | ' position:sticky;top:var(--sg-top-offset,0px);', | ||
' | ' align-self:flex-start;', | ||
'}', | '}', | ||
'body.sg-sticky-on #content{', | 'body.sg-sticky-on #content{', | ||
| 第103行: | 第95行: | ||
var cactions = document.getElementById( 'p-cactions' ); /* 编辑标签那排 */ | var cactions = document.getElementById( 'p-cactions' ); /* 编辑标签那排 */ | ||
/* | var topH = 0; | ||
personal.style.visibility = 'hidden'; | /* 账号栏:吸顶,整条通栏,账号链接靠右 */ | ||
var pBar = buildBar( personal, 'sg-personal-bar' ); | |||
if ( pBar ) { | |||
pBar.className = 'sg-sticky-bar'; | |||
pBar.style.textAlign = 'right'; | |||
pBar.style.top = '0px'; | |||
if ( personal ) { personal.style.visibility = 'hidden'; } | |||
topH += pBar.offsetHeight; | |||
} | } | ||
/* | /* 编辑标签栏:紧贴在账号栏下方,整条通栏,从左开始 */ | ||
var ca = buildBar( cactions, 'sg-cactions-bar' ); | var ca = buildBar( cactions, 'sg-cactions-bar' ); | ||
if ( ca ) { | if ( ca ) { | ||
ca.className = 'sg-sticky-bar'; | |||
ca.style.textAlign = 'left'; | |||
ca.style.top = topH + 'px'; | ca.style.top = topH + 'px'; | ||
if ( cactions ) { cactions.style.visibility = 'hidden'; } | |||
topH += ca.offsetHeight; | topH += ca.offsetHeight; | ||
} | } | ||
2026年10月11日 (日) 14:35的版本
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
( function () {
/* ==========================================================
一、把页面里的目录(TOC)搬到左侧栏
========================================================== */
function moveTOC() {
var toc = document.getElementById( 'toc' )
|| document.querySelector( '.mw-table-of-contents-container' )
|| document.querySelector( '.mw-table-of-contents' );
var sidebar = document.getElementById( 'column-one' )
|| document.getElementById( 'mw-panel' );
if ( !toc || !sidebar || document.getElementById( 'p-toc' ) ) {
return;
}
var box = document.createElement( 'div' );
box.className = 'portlet';
box.id = 'p-toc';
var title = document.createElement( 'h3' );
title.textContent = '目录';
var inner = document.createElement( 'div' );
inner.className = 'pBody';
inner.appendChild( toc );
box.appendChild( title );
box.appendChild( inner );
sidebar.appendChild( box );
}
/* ==========================================================
二、吸顶样式
顶部两条通栏固定;左侧栏吸顶但不出现滚动条
========================================================== */
var STICKY_CSS = [
'.sg-sticky-bar{',
' position:fixed;left:0;right:0;top:0;z-index:401;',
' background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.18);',
' padding:3px 10px;font-size:87%;line-height:1.3;',
'}',
'.sg-sticky-bar a{',
' color:#36c;text-decoration:none;padding:2px 6px;',
' display:inline-block;',
'}',
'.sg-sticky-bar a:hover{text-decoration:underline;}',
'#column-one{',
' position:sticky;top:var(--sg-top-offset,0px);',
' align-self:flex-start;',
'}',
'body.sg-sticky-on #content{',
' padding-top:var(--sg-top-offset,0px) !important;',
'}'
].join( '\n' );
function injectCSS() {
if ( document.getElementById( 'sg-sticky-style' ) ) { return; }
var s = document.createElement( 'style' );
s.id = 'sg-sticky-style';
s.type = 'text/css';
s.textContent = STICKY_CSS;
document.head.appendChild( s );
}
/* 把某个栏目里的链接复制到一条固定栏上;栏目为空就不做 */
function buildBar( portlet, barId ) {
if ( !portlet ) { return null; }
var links = portlet.querySelectorAll( 'ul a' );
if ( !links.length ) { return null; }
var old = document.getElementById( barId );
if ( old && old.parentNode ) { old.parentNode.removeChild( old ); }
var bar = document.createElement( 'div' );
bar.id = barId;
Array.prototype.forEach.call( links, function ( a ) {
var copy = document.createElement( 'a' );
copy.href = a.href;
copy.textContent = a.textContent;
bar.appendChild( copy );
} );
document.body.appendChild( bar );
return bar;
}
function makeSticky() {
injectCSS();
var personal = document.getElementById( 'p-personal' ); /* 账号那排 */
var cactions = document.getElementById( 'p-cactions' ); /* 编辑标签那排 */
var topH = 0;
/* 账号栏:吸顶,整条通栏,账号链接靠右 */
var pBar = buildBar( personal, 'sg-personal-bar' );
if ( pBar ) {
pBar.className = 'sg-sticky-bar';
pBar.style.textAlign = 'right';
pBar.style.top = '0px';
if ( personal ) { personal.style.visibility = 'hidden'; }
topH += pBar.offsetHeight;
}
/* 编辑标签栏:紧贴在账号栏下方,整条通栏,从左开始 */
var ca = buildBar( cactions, 'sg-cactions-bar' );
if ( ca ) {
ca.className = 'sg-sticky-bar';
ca.style.textAlign = 'left';
ca.style.top = topH + 'px';
if ( cactions ) { cactions.style.visibility = 'hidden'; }
topH += ca.offsetHeight;
}
document.documentElement.style.setProperty( '--sg-top-offset', topH + 'px' );
if ( topH > 0 ) {
document.body.classList.add( 'sg-sticky-on' );
} else {
document.body.classList.remove( 'sg-sticky-on' );
}
}
/* ==========================================================
启动
========================================================== */
function run() {
moveTOC();
makeSticky();
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', run );
} else {
run();
}
window.addEventListener( 'resize', function () {
clearTimeout( window.__sgTimer );
window.__sgTimer = setTimeout( makeSticky, 150 );
} );
}() );