"MediaWiki:Common.js" 修訂間的差異
| 行 206: | 行 206: | ||
| + | //stickUp2/ppowalowski a jQuery plugin github.com/ppowalowski/stickUp2 | ||
| + | !function(t,e,o){var s=function(s,i){var r,n=0,a=0,p="",c=0,f=!1,g=!1,l=!1,u=!1,m=!1,d=!1,h=0,I=0,b=0,k=0,v=0,x=0,H=0,W=t(),w=0,y=0,z=t('<div style="margin-top:0;margin-bottom:0; padding:0"></div>'),C=t(),M={top:0,bottom:0,custom:[]},U={scrollHide:!1,lazyHeight:0,topMargin:"auto",keepInWrapper:!1,wrapperSelector:"",zIndex:99,syncPosition:!1,namespaceClass:"stuckElement",fixedClass:"isStuck",disableOn:function(){return!0}},T=function(){return"auto"===U.topMargin?parseInt(W.css("marginTop")):isNaN(U.topMargin)&&U.topMargin.search("px")>0?parseInt(U.topMargin.replace("px","")):isNaN(parseInt(U.topMargin))?0:parseInt(U.topMargin)},S=function(){var t={cancel:!1};W.trigger("stickUp:beforeUnstick",[t]),t.cancel||(z.remove(),W.removeClass(U.fixedClass).css({maxWidth:"",marginTop:"",marginLeft:"",marginRight:"",position:"",top:"",left:"",right:"",bottom:"",width:""}),f=!1,g=!1,l=!1,U.syncPosition&&E(),W.trigger("stickUp:unstick"))},N=function(t){W.before(z.css("height",I));var e=z.offsetParent();if(t){W.css({position:"absolute"});var o=C.offset().top+C.outerHeight()-e.offset().top-b-parseInt(C.css("paddingBottom"))}W.css({position:"absolute",marginTop:w,bottom:"",left:z.position().left,top:t?o:W.offset().top-e.offset().top-w})},P=function(){var t={cancel:!1};if(W.trigger("stickUp:beforeStick",[t]),!t.cancel){f=!0,W.before(z.css("height",I)),W.addClass(U.fixedClass);var e=-y;W.css({marginTop:w,position:"fixed",top:e+"px",left:"",right:"",bottom:""}),W.trigger("stickUp:stick")}},B=function(){W.before(z.css("height",I)),W.addClass(U.fixedClass);var t=-y;W.css({marginTop:w,position:"fixed",top:"",left:"",right:"",bottom:t})},O=function(){z.width()!==W.outerWidth()&&W.outerWidth(z.outerWidth())},j=function(){r=z.offset().left,r!==W.offset().left,W.offset({left:r})},E=function(){z.css({"margin-left":W.css("margin-left"),"margin-right":W.css("margin-left")}),W.css({"margin-left":z.css("margin-left"),"margin-right":z.css("margin-right")})},L=function(s){if(!U.disableOn())return u||(S(),u=!0),void 0;if(u&&(u=!1),!U.keepInWrapper||C.is(":visible")){if(a=t(s.target).scrollTop(),p=a>=n?"down":"up",c=Math.abs(n-a),k=t(e).outerHeight(),v=a+k,n=a,x=W.offset().top,h=parseInt(W.outerHeight()+w)+parseInt(W.css("marginBottom")),f||l||g||(I=parseInt(W.outerHeight(!0)),M.top=g||l?parseInt(z.offset().top):parseInt(W.offset().top),r=parseInt(W.offset().left)+5),b=parseInt(W.outerHeight())+parseInt(W.css("margin-bottom"))+w,M.bottom=U.keepInWrapper?C.offset().top+C.outerHeight()-parseInt(C.css("paddingBottom")):t(o).outerHeight(),H=W.offset().top+b,h>k)d=!0,m&&(y=0,l&&N(),m=!1),l&&"up"===p&&x-w>=a&&(P(),f=!0,g=!1,l=!1),!f&&!g&&(!U.keepInWrapper||U.keepInWrapper&&v<=M.bottom)&&v>=H-w&&(B(),g=!0,f=!0,l=!1),(!l&&f&&g&&"up"===p||!l&&f&&!g&&"down"===p&&H>=v)&&(N(),f=!1,g=!1,l=!0),v>=M.bottom&&U.keepInWrapper&&(!g&&!l||parseInt(H-w)!==parseInt(M.bottom))&&(N(!0),f=!1,g=!0,l=!0);else if(m=!0,d&&(l&&N(),d=!1),y=U.scrollHide?h+U.lazyHeight:U.lazyHeight,(!f&&!g&&a>=M.top-w+y||g&&l&&x-w+y>=a)&&(P(),f=!0,g=!1,l=!1),U.keepInWrapper&&parseInt(H-w)!==parseInt(M.bottom)&&a>=M.bottom-b+y&&(N(!0),f=!1,g=!0,l=!0),f){var i=parseInt(W.css("top"));if("up"===p&&0!==i){var T=c>-i?0:i+c;W.css("top",T+"px")}else if("down"===p&&i>-y){var T=c>y+i?-y:i-c;W.css("top",T+"px")}}(f||l||g)&&a<=M.top-w&&S(),(f||l||g)&&O(),(U.syncPosition&&f||l)&&j(),W.trigger("stickUp:scroll")}},Q=function(t){W.trigger("stickUp:resize",U),w=null!==U.topMargin?T():0,l&&(N(),g=!1),L(t)},R=function(s,i){W=t(s),W.addClass(U.namespaceClass),i&&t.extend(!0,U,i),w=null!==U.topMargin?T():0,U.lazyHeight&&(w+=U.lazyHeight),U.keepInWrapper?(""!==U.wrapperSelector&&(C=W.closest(U.wrapperSelector)),C.length||(C=W.parent())):C=t("body"),U.zIndex&&W.css("z-index",U.zIndex),j&&E(),t(e).on("scroll.stickUp",L),t(e).on("resize.stickUp",Q),L({target:o})};R.call(this,s,i)};t.fn.stickUp=function(t){return this.each(function(){new s(this,t)})}}(jQuery,window,document); | ||
| + | |||
| + | //封装nav_fixed | ||
| + | function nav_fixed() { | ||
| + | jQuery("#mw-head").stickUp({}); | ||
| + | } | ||
| + | //封装sidebar_fixed | ||
| + | function sidebar_fixed() { | ||
| + | jQuery("#ad_left").stickUp({ | ||
| + | wrapperSelector: "#mw-panel", | ||
| + | }); | ||
| + | } | ||
| + | //sidebar默认收起点击展开 | ||
| + | $('#p-tb-label').click(function() { | ||
| + | $('#p-tb-label+.body').slideToggle("300") | ||
| + | }); | ||
| + | //启动所有事件 | ||
| + | jQuery(document).ready(function() { | ||
| + | nav_fixed(); | ||
| + | sidebar_fixed(); | ||
| + | }); | ||
於 2019年2月15日 (五) 14:22 的修訂
/* 此處的JavaScript將載入於所有用戶每一個頁面。 */
/*
<pre><nowiki>
*/
function getParamValue(paramName) {
var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
var h=document.location;
var m;
if (m=cmdRe.exec(h)) {
try {
return decodeURI(m[1]);
} catch (someError) {}
}
return null;
};
function substitute(data,cmdBody) {
// alert('sub\nfrom: '+cmdBody.from+'\nto: '+cmdBody.to+'\nflags: '+cmdBody.flags);
var fromRe=RegExp(cmdBody.from, cmdBody.flags);
return data.replace(fromRe, cmdBody.to);
};
function execCmds(data, cmdList) {
for (var i=0; i<cmdList.length; ++i) {
data=cmdList[i].action(data, cmdList[i]);
}
return data;
}
function parseCmd(str) {
// returns a list of commands
if (!str.length) return [];
var p=false;
switch (str.charAt(0)) {
case 's':
p=parseSubstitute(str);
break;
case 'j':
p=parseJavascript(str);
break;
default:
return false;
}
if (p) return [p].concat(parseCmd(p.remainder));
return false;
};
function unEscape(str, sep) {
return str.split('\\\\').join('\\')
.split('\\'+sep).join(sep)
.split('\\n').join('\n');
};
function runJavascript(data, argWrapper) {
// flags aren't used (yet)
// from the user's viewpoint,
// data is a special variable may appear inside code
// and gets assigned the text in the edit box
// alert('eval-ing '+argWrapper.code);
return eval(argWrapper.code);
};
function parseJavascript(str) {
// takes a string like j/code/;othercmds and parses it
var tmp,code,flags;
if (str.length<3) return false;
var sep=str.charAt(1);
str=str.substring(2);
tmp=skipOver(str,sep);
if (tmp) { code=tmp.segment.split('\n').join('\\n'); str=tmp.remainder; }
else return false;
flags='';
if (str.length) {
tmp=skipOver(str,';') || skipToEnd(str, ';');
if (tmp) {flags=tmp.segment; str=tmp.remainder; }
}
return { action: runJavascript, code: code, flags: flags, remainder: str };
};
function parseSubstitute(str) {
// takes a string like s/a/b/flags;othercmds and parses it
var from,to,flags,tmp;
if (str.length<4) return false;
var sep=str.charAt(1);
str=str.substring(2);
tmp=skipOver(str,sep);
if (tmp) { from=tmp.segment; str=tmp.remainder; }
else return false;
tmp=skipOver(str,sep);
if (tmp) { to=tmp.segment; str=tmp.remainder; }
else return false;
flags='';
if (str.length) {
tmp=skipOver(str,';') || skipToEnd(str, ';');
if (tmp) {flags=tmp.segment; str=tmp.remainder; }
}
return {action: substitute, from: from, to: to, flags: flags, remainder: str};
};
function skipOver(str,sep) {
var endSegment=findNext(str,sep);
if (endSegment<0) return false;
var segment=unEscape(str.substring(0,endSegment), sep);
return {segment: segment, remainder: str.substring(endSegment+1)};
}
function skipToEnd(str,sep) {
return {segment: str, remainder: ''};
}
function findNext(str, ch) {
for (var i=0; i<str.length; ++i) {
if (str.charAt(i)=='\\') i+=2;
if (str.charAt(i)==ch) return i;
}
return -1;
};
function runOnLoad(f) {
if (window.addEventListener) {
window.addEventListener("load",f,false);
}
else if (window.attachEvent) {
window.attachEvent("onload",f);
}
else {
window._old_popup_autoedit_onload = window.onload;
window.onload = function() {
window._old_popup_autoedit_onload();
f();
}
}
};
window.autoEdit=function() {
if (window.autoEdit.alreadyRan) return false;
window.autoEdit.alreadyRan=true;
var cmdString=getParamValue('autoedit');
if (cmdString) {
try {
var editbox=document.editform.wpTextbox1;
} catch (dang) { return; }
var cmdList=parseCmd(cmdString);
var input=editbox.value;
var output=execCmds(input, cmdList);
editbox.value=output;
}
var summary=getParamValue('autosummary');
if (summary) document.editform.wpSummary.value=summary;
var minor=getParamValue('autominor');
if (minor) {
switch (minor) {
case '1':
case 'yes':
case 'true':
document.editform.wpMinoredit.checked=true;
break;
case '0':
case 'no':
case 'false':
document.editform.wpMinoredit.checked=false;
}
}
var btn=getParamValue('autoclick');
if (btn) {
if (document.editform && document.editform[btn]) {
var headings=document.getElementsByTagName('h1');
if (headings) {
var div=document.createElement('div');
var button=document.editform[btn];
div.innerHTML='<font size=+1><b>"' + button.value +
'" 按鈕已經被自動點擊。' +
' 請等待載入下一個頁面。</b></font>';
document.title='('+document.title+')';
headings[0].parentNode.insertBefore(div, headings[0]);
button.click();
}
} else {
alert('autoedit.js\n\nautoclick: 找不到按鈕 "'+ btn +'".');
}
}
};
runOnLoad(autoEdit);
//stickUp2/ppowalowski a jQuery plugin github.com/ppowalowski/stickUp2
!function(t,e,o){var s=function(s,i){var r,n=0,a=0,p="",c=0,f=!1,g=!1,l=!1,u=!1,m=!1,d=!1,h=0,I=0,b=0,k=0,v=0,x=0,H=0,W=t(),w=0,y=0,z=t('<div style="margin-top:0;margin-bottom:0; padding:0"></div>'),C=t(),M={top:0,bottom:0,custom:[]},U={scrollHide:!1,lazyHeight:0,topMargin:"auto",keepInWrapper:!1,wrapperSelector:"",zIndex:99,syncPosition:!1,namespaceClass:"stuckElement",fixedClass:"isStuck",disableOn:function(){return!0}},T=function(){return"auto"===U.topMargin?parseInt(W.css("marginTop")):isNaN(U.topMargin)&&U.topMargin.search("px")>0?parseInt(U.topMargin.replace("px","")):isNaN(parseInt(U.topMargin))?0:parseInt(U.topMargin)},S=function(){var t={cancel:!1};W.trigger("stickUp:beforeUnstick",[t]),t.cancel||(z.remove(),W.removeClass(U.fixedClass).css({maxWidth:"",marginTop:"",marginLeft:"",marginRight:"",position:"",top:"",left:"",right:"",bottom:"",width:""}),f=!1,g=!1,l=!1,U.syncPosition&&E(),W.trigger("stickUp:unstick"))},N=function(t){W.before(z.css("height",I));var e=z.offsetParent();if(t){W.css({position:"absolute"});var o=C.offset().top+C.outerHeight()-e.offset().top-b-parseInt(C.css("paddingBottom"))}W.css({position:"absolute",marginTop:w,bottom:"",left:z.position().left,top:t?o:W.offset().top-e.offset().top-w})},P=function(){var t={cancel:!1};if(W.trigger("stickUp:beforeStick",[t]),!t.cancel){f=!0,W.before(z.css("height",I)),W.addClass(U.fixedClass);var e=-y;W.css({marginTop:w,position:"fixed",top:e+"px",left:"",right:"",bottom:""}),W.trigger("stickUp:stick")}},B=function(){W.before(z.css("height",I)),W.addClass(U.fixedClass);var t=-y;W.css({marginTop:w,position:"fixed",top:"",left:"",right:"",bottom:t})},O=function(){z.width()!==W.outerWidth()&&W.outerWidth(z.outerWidth())},j=function(){r=z.offset().left,r!==W.offset().left,W.offset({left:r})},E=function(){z.css({"margin-left":W.css("margin-left"),"margin-right":W.css("margin-left")}),W.css({"margin-left":z.css("margin-left"),"margin-right":z.css("margin-right")})},L=function(s){if(!U.disableOn())return u||(S(),u=!0),void 0;if(u&&(u=!1),!U.keepInWrapper||C.is(":visible")){if(a=t(s.target).scrollTop(),p=a>=n?"down":"up",c=Math.abs(n-a),k=t(e).outerHeight(),v=a+k,n=a,x=W.offset().top,h=parseInt(W.outerHeight()+w)+parseInt(W.css("marginBottom")),f||l||g||(I=parseInt(W.outerHeight(!0)),M.top=g||l?parseInt(z.offset().top):parseInt(W.offset().top),r=parseInt(W.offset().left)+5),b=parseInt(W.outerHeight())+parseInt(W.css("margin-bottom"))+w,M.bottom=U.keepInWrapper?C.offset().top+C.outerHeight()-parseInt(C.css("paddingBottom")):t(o).outerHeight(),H=W.offset().top+b,h>k)d=!0,m&&(y=0,l&&N(),m=!1),l&&"up"===p&&x-w>=a&&(P(),f=!0,g=!1,l=!1),!f&&!g&&(!U.keepInWrapper||U.keepInWrapper&&v<=M.bottom)&&v>=H-w&&(B(),g=!0,f=!0,l=!1),(!l&&f&&g&&"up"===p||!l&&f&&!g&&"down"===p&&H>=v)&&(N(),f=!1,g=!1,l=!0),v>=M.bottom&&U.keepInWrapper&&(!g&&!l||parseInt(H-w)!==parseInt(M.bottom))&&(N(!0),f=!1,g=!0,l=!0);else if(m=!0,d&&(l&&N(),d=!1),y=U.scrollHide?h+U.lazyHeight:U.lazyHeight,(!f&&!g&&a>=M.top-w+y||g&&l&&x-w+y>=a)&&(P(),f=!0,g=!1,l=!1),U.keepInWrapper&&parseInt(H-w)!==parseInt(M.bottom)&&a>=M.bottom-b+y&&(N(!0),f=!1,g=!0,l=!0),f){var i=parseInt(W.css("top"));if("up"===p&&0!==i){var T=c>-i?0:i+c;W.css("top",T+"px")}else if("down"===p&&i>-y){var T=c>y+i?-y:i-c;W.css("top",T+"px")}}(f||l||g)&&a<=M.top-w&&S(),(f||l||g)&&O(),(U.syncPosition&&f||l)&&j(),W.trigger("stickUp:scroll")}},Q=function(t){W.trigger("stickUp:resize",U),w=null!==U.topMargin?T():0,l&&(N(),g=!1),L(t)},R=function(s,i){W=t(s),W.addClass(U.namespaceClass),i&&t.extend(!0,U,i),w=null!==U.topMargin?T():0,U.lazyHeight&&(w+=U.lazyHeight),U.keepInWrapper?(""!==U.wrapperSelector&&(C=W.closest(U.wrapperSelector)),C.length||(C=W.parent())):C=t("body"),U.zIndex&&W.css("z-index",U.zIndex),j&&E(),t(e).on("scroll.stickUp",L),t(e).on("resize.stickUp",Q),L({target:o})};R.call(this,s,i)};t.fn.stickUp=function(t){return this.each(function(){new s(this,t)})}}(jQuery,window,document);
//封装nav_fixed
function nav_fixed() {
jQuery("#mw-head").stickUp({});
}
//封装sidebar_fixed
function sidebar_fixed() {
jQuery("#ad_left").stickUp({
wrapperSelector: "#mw-panel",
});
}
//sidebar默认收起点击展开
$('#p-tb-label').click(function() {
$('#p-tb-label+.body').slideToggle("300")
});
//启动所有事件
jQuery(document).ready(function() {
nav_fixed();
sidebar_fixed();
});
/*
</nowiki></pre>
*/