MediaWiki:Gadget-flashAnchored.js

From Calamity Mod Wiki
Jump to navigation Jump to search

CSS and Javascript changes must comply with the wiki design rules.


Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
if (window.location.hash.length > 0) {
	var hash = window.location.hash.replace(/\./g,'\\.');
	flash($(hash));
} 

window.onhashchange = function() {
	var hash = window.location.hash.replace(/\./g,'\\.');
	flash($(hash));
}

function findFlashable($element) {
	var flashable = [ 'tr', 'h1', 'h2', 'h3', 'h4', 'h5', 'li', 'div' ];

	if ($.inArray($element.prop('tagName'), flashable) < 0) { 
		var $newElement;

		$.each(flashable, function(_, val) {
			$newElement = $element.closest(val);

			if ($newElement.length > 0) {
				return false;
			}
		});

		return $newElement;
	}

	return $element;
}

function flash($element) {
	// Check if the element is suitable for flashing, otherwise find a suitable parent 
	$element = findFlashable($element);

	// Reset any existing flashed elements
	$('.flash-anchored').removeClass("flash-anchored");

	$element.addClass("flash-anchored");
}