MediaWiki:SupportPage.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
No edit summary |
||
Line 45: | Line 45: | ||
toc[i].link.on( 'click', toc[i], smoothScroll ); |
toc[i].link.on( 'click', toc[i], smoothScroll ); |
||
} |
} |
||
// fix inbound fragment links |
|||
function delayedFragmentTargetOffset() { |
|||
var offset = $(':target').offset(); |
|||
if (offset) { |
|||
var scrollto = offset.top - scrollOffset; |
|||
$('html, body').animate({ scrollTop: scrollto }, 0); |
|||
} |
|||
} |
|||
setTimeout(delayedFragmentTargetOffset, 500); |
|||
// Highlight current section in toc |
// Highlight current section in toc |
Revision as of 21:44, 16 December 2019
/*
* MediaWiki:SupportPage.js
* Loaded by MediaWiki:Common.js on support pages e.g. "Problems donating"
*/
$(function() {
/* -- language bar code -- */
function showHideLanguageExpand() {
if ( $('.sp-languages ul').height() > $('.sp-languages').height() ) {
$('.sp-languages-expand').show();
} else if ( !$('.sp-languages').hasClass('-expanded') ) {
$('.sp-languages-expand').hide();
}
}
showHideLanguageExpand();
$( window ).resize( showHideLanguageExpand );
/* -- end of language bar code -- */
/* -- table of contents code -- */
// Build a list of corresponding toc links and anchors
var toc = [];
$('#toc > ul a').each( function() {
var id = $(this).attr('href').replace('#', '');
toc.push({
link: $(this),
anchor: $( document.getElementById(id) )
});
});
// Add smooth scrolling effect
var scrollOffset = 88, // Offset in px
scrollDuration = 500, // in ms
smoothScroll = function(event) {
var offsetTop = event.data.anchor.offset().top;
$('html, body').stop().animate({
scrollTop: offsetTop - scrollOffset
}, scrollDuration);
window.location.hash = event.data.anchor.attr('id');
event.preventDefault();
};
for (var i = 0; i < toc.length; i++) {
toc[i].link.on( 'click', toc[i], smoothScroll );
}
// fix inbound fragment links
function delayedFragmentTargetOffset() {
var offset = $(':target').offset();
if (offset) {
var scrollto = offset.top - scrollOffset;
$('html, body').animate({ scrollTop: scrollto }, 0);
}
}
setTimeout(delayedFragmentTargetOffset, 500);
// Highlight current section in toc
var lastSection;
function highlightCurrentSection() {
var fromTop = $(this).scrollTop(); // Get container scroll position
var currentSection = toc.filter(function(item) {
return fromTop > item.anchor.offset().top - 64;
});
currentSection = currentSection[currentSection.length - 1];
if ( currentSection && lastSection !== currentSection ) {
lastSection = currentSection;
$('#toc > ul a').removeClass('-active');
currentSection.link.addClass('-active');
}
}
highlightCurrentSection();
$(window).scroll( highlightCurrentSection );
/* -- end of table of contents code -- */
// Collapsible continents/countries on Ways to Give
mw.loader.using('jquery.makeCollapsible', function() {
$('.mw-collapsible').makeCollapsible();
});
});