MediaWiki:SupportPage.js: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
minor cleanup
No edit summary
Line 20: Line 20:
topMenu = $('#toc > ul'),
topMenu = $('#toc > ul'),
menuItems = topMenu.find('a'), // All TOC links
menuItems = topMenu.find('a'), // All TOC links
scrollItems = $('.sp-content .mw-headline'); // Anchors corresponding to TOC links
scrollItems = $('.sp-content .mw-headline'), // Anchors corresponding to TOC links
scrollOffset = 16, // Offset in px
scrollDuration = 500; // in ms


/* Bind click handler to menu items so we can get a fancy scroll animation */
/* Bind click handler to menu items so we can get a fancy scroll animation */
menuItems.click(function(e){
menuItems.click(function(e){
var id = $(this).attr('href').replace('#', ''),
var id = $(this).attr('href').replace('#', '');
offsetTop = $( document.getElementById(id) ).offset().top;
var offsetTop = $( document.getElementById(id) ).offset().top;
$('html, body').stop().animate({
$('html, body').stop().animate({
scrollTop: offsetTop
scrollTop: offsetTop - scrollOffset
}, 850);
}, scrollDuration);
e.preventDefault();
e.preventDefault();
});
});
Line 34: Line 36:
/* Highlighting current section in TOC */
/* Highlighting current section in TOC */
$(window).scroll(function(){
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop(); // Get container scroll position
var fromTop = $(this).scrollTop();


// Get id of current scroll item
// Get id of current scroll item
var cur = scrollItems.map(function(){
var cur = scrollItems.map(function(){
if ( $(this).offset().top < fromTop )
if ( $(this).offset().top < fromTop ) {
return this;
return this;
});
}
});
// Get the id of the current element
// Get the id of the current element
cur = cur[cur.length-1];
cur = cur[cur.length-1];