User:KokoroSenshi/common.js: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
m (auto-formatted and fixed all ide errors)
m (More formatting; Tried some refactoring, relating to variables, scope)
Line 2: Line 2:


$(function() {
$(function() {
var targets = $(".explain, .tooltip")
  , target = false
  , tooltip = false
  , title = false;
targets.bind("mouseenter", function() {
$(".explain, .tooltip").bind("mouseenter", function() {
target = $(this);
tip = target.attr("title");
tooltip = $('<ul id="tooltip" class="referencetooltip"></ul>');
if (!tip || tip === "") return false;
var $this = $(this)
  , tipText = $this.attr("title");
target.removeAttr("title");
if (!tipText || tipText === "") return false;
tooltip
$this.removeAttr("title");
var $tooltip = $('<ul id="tooltip" class="referencetooltip"></ul>');
$tooltip
.css("opacity", 0)
.css("opacity", 0)
.html('<li style="padding: 10px 8px">' + tip + "</li>"  
.html('<li style="padding: 10px 8px">' + tipText + "</li>"  
+ '<li style="margin: auto; margin-top: -2px;"></li>')
+ '<li style="margin: auto; margin-top: -2px;"></li>')
.appendTo("body");
.appendTo("body");
var init_tooltip = function() {
var init_tooltip = function() {
if ($(window).width() < tooltip.outerWidth() * 1.5)
var windowWidth = $(window).width()
tooltip.css("max-width", $(window).width() / 2);
  , thisWidth  = $this.outerWidth()
  , thisHeight = $this.outerHeight()
  , tooltipWidth  = $tooltip.outerWidth()
  , tooltipHeight = $tooltip.outerHeight();
if (windowWidth < tooltipWidth * 1.5)
$tooltip.css("max-width", windowWidth / 2);
else
else
tooltip.css("max-width", 340);
$tooltip.css("max-width", 340);
var pos_left = target.offset().left + target.outerWidth() / 2  
var pos_left = $this.offset().left + thisWidth / 2  
- tooltip.outerWidth() / 2,
- tooltipWidth / 2
pos_top = target.offset().top - tooltip.outerHeight()
  , pos_top = $this.offset().top - tooltipHeight
- $("#netbar").height();
- $("#netbar").height();
if (pos_left < 0) {
if (pos_left < 0) {
pos_left = target.offset().left + target.outerWidth() / 2 - 20;
pos_left = $this.offset().left + thisWidth / 2 - 20;
tooltip.addClass("left");
$tooltip.addClass("left");
} else tooltip.removeClass("left");
} else $tooltip.removeClass("left");
if (pos_left + tooltip.outerWidth() > $(window).width()) {
if (pos_left + tooltipWidth > windowWidth) {
pos_left = target.offset().left - tooltip.outerWidth()
pos_left = $this.offset().left - tooltipWidth + thisWidth / 2 + 20;
+ target.outerWidth() / 2 + 20;
$tooltip.addClass("right");
tooltip.addClass("right");
} else $tooltip.removeClass("right");
} else tooltip.removeClass("right");
if (pos_top < 0) {
if (pos_top < 0) {
pos_top = target.offset().top + target.outerHeight();
pos_top = $this.offset().top + thisHeight;
tooltip.addClass("top");
$tooltip.addClass("top");
} else tooltip.removeClass("top");
} else $tooltip.removeClass("top");
tooltip
$tooltip
.css({ left: pos_left, top: pos_top })
.css({ left: pos_left, top: pos_top })
.animate({ opacity: 1 }, 100);
.animate({ opacity: 1 }, 100);
Line 57: Line 61:
var remove_tooltip = function() {
var remove_tooltip = function() {
tooltip.animate({ opacity: 0 }, 100, function() {
$tooltip.animate({ opacity: 0 }, 100, function() {
$(this).remove();
$(this).remove();
});
});
target.attr("title", tip);
$this.attr("title", tipText);
};
};
target.bind("mouseleave", remove_tooltip);
$this.bind("mouseleave", remove_tooltip);
tooltip.bind("click", remove_tooltip);
$tooltip.bind("click", remove_tooltip);
});
});
});
});

Revision as of 13:00, 12 April 2018

// Original by Osvaldas Valutis: http://web.archive.org/web/20180306004159/https://osvaldas.info/elegant-css-and-jquery-tooltip-responsive-mobile-friendly

$(function() {
	
	$(".explain, .tooltip").bind("mouseenter", function() {
		
		var $this = $(this)
		  , tipText = $this.attr("title");
		
		if (!tipText || tipText === "") return false;
		
		$this.removeAttr("title");
		
		var $tooltip = $('<ul id="tooltip" class="referencetooltip"></ul>');
		
		$tooltip
			.css("opacity", 0)
			.html('<li style="padding: 10px 8px">' + tipText + "</li>" 
					+ '<li style="margin: auto; margin-top: -2px;"></li>')
			.appendTo("body");
		
		var init_tooltip = function() {
			var windowWidth = $(window).width()
			  , thisWidth  = $this.outerWidth()
			  , thisHeight = $this.outerHeight()
			  , tooltipWidth  = $tooltip.outerWidth()
			  , tooltipHeight = $tooltip.outerHeight();
			
			if (windowWidth < tooltipWidth * 1.5)
				$tooltip.css("max-width", windowWidth / 2);
			else
				$tooltip.css("max-width", 340);
			
			var pos_left = $this.offset().left + thisWidth / 2 
					- tooltipWidth / 2
			  , pos_top = $this.offset().top - tooltipHeight 
					- $("#netbar").height();
			
			if (pos_left < 0) {
				pos_left = $this.offset().left + thisWidth / 2 - 20;
				$tooltip.addClass("left");
			} else $tooltip.removeClass("left");
			
			if (pos_left + tooltipWidth > windowWidth) {
				pos_left = $this.offset().left - tooltipWidth + thisWidth / 2 + 20;
				$tooltip.addClass("right");
			} else $tooltip.removeClass("right");
			
			if (pos_top < 0) {
				pos_top = $this.offset().top + thisHeight;
				$tooltip.addClass("top");
			} else $tooltip.removeClass("top");
			
			$tooltip
				.css({ left: pos_left, top: pos_top })
				.animate({ opacity: 1 }, 100);
		};
		
		init_tooltip();
		$(window).resize(init_tooltip);
		
		var remove_tooltip = function() {
			$tooltip.animate({ opacity: 0 }, 100, function() {
				$(this).remove();
			});
			$this.attr("title", tipText);
		};
		
		$this.bind("mouseleave", remove_tooltip);
		$tooltip.bind("click", remove_tooltip);
	});
});