MediaWiki:SupportPage.js: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
No edit summary
new toc code
Line 14: Line 14:
$(function() {
$(function() {


/* Code for highlighting current section in TOC */
/* -- 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) )
});
});


// // Cache selectors
// Add smooth scrolling effect
var scrollOffset = 24, // Offset in px
// var lastId,
// topMenu = $('#toc > ul'),
scrollDuration = 500, // in ms
// menuItems = topMenu.find('a'), // All TOC links
smoothScroll = function(event) {
var offsetTop = event.data.anchor.offset().top;
// scrollItems = $('.sp-content .mw-headline'), // Anchors corresponding to TOC links
// scrollOffset = 16, // Offset in px
$('html, body').stop().animate({
scrollTop: offsetTop - scrollOffset
// scrollDuration = 500; // in ms
}, scrollDuration);
event.preventDefault();
};
for (var i = 0; i < toc.length; i++) {
toc[i].link.on( 'click', toc[i], smoothScroll );
}


// Highlight current section in toc
// /* Bind click handler to menu items so we can get a fancy scroll animation */
var lastSection;
// menuItems.click(function(e){
$(window).scroll(function() {
// var id = $(this).attr('href').replace('#', '');
// var offsetTop = $( document.getElementById(id) ).offset().top;
var fromTop = $(this).scrollTop(); // Get container scroll position
// $('html, body').stop().animate({
var currentSection = toc.filter(function(item) {
// scrollTop: offsetTop - scrollOffset
return fromTop > item.anchor.offset().top - 48;
// }, scrollDuration);
});
currentSection = currentSection[currentSection.length - 1];
// e.preventDefault();
if ( lastSection !== currentSection ) {
// });
lastSection = currentSection;
$('#toc > ul a').removeClass('-active');
currentSection.link.addClass('-active');
}
});
/* -- end of table of contents code -- */


// /* Highlighting current section in TOC */
// $(window).scroll(function(){
// var fromTop = $(this).scrollTop(); // Get container scroll position

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

// if (lastId !== id) {
// lastId = id;
// // Set/remove active class
// menuItems
// .removeClass('-active')
// .end().filter('[href=#' + id + ']').addClass('-active');
// }
// });
// /* END of highlighting current section in TOC */


/*!
/*!