Difference between revisions of "User:Wookipan/Sandbox/Page.js"
< User:Wookipan | Sandbox
m (Minor adjustments, increase target width to accommodate the wiki's odd container width) |
m (Change block order to ensure that tooltips containing links are handled before checking the offset, remove unnecessary "pointer-events" applied to link tooltips) |
||
Line 6: | Line 6: | ||
$tooltips.each(function() { | $tooltips.each(function() { | ||
var $this = $(this); | var $this = $(this); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
// Check for links and handle them on handheld devices | // Check for links and handle them on handheld devices | ||
Line 22: | Line 14: | ||
wikiTooltip.hasLink($this); | wikiTooltip.hasLink($this); | ||
} | } | ||
+ | } | ||
+ | |||
+ | // Prevent tooltips from overflowing the viewport | ||
+ | edgeRect = $this.width() + $this.offset().left; | ||
+ | widthRect = $(window).width(); | ||
+ | if (edgeRect - widthRect > $this.width()*0) { | ||
+ | $this.css('left', ''); | ||
+ | $this.css('right', '100%'); | ||
} | } | ||
}); | }); | ||
Line 34: | Line 34: | ||
url.attr('href', 'javascript:void(0);'); | url.attr('href', 'javascript:void(0);'); | ||
tooltip.append($a); | tooltip.append($a); | ||
− | |||
} | } | ||
}; | }; | ||
$(wikiTooltip.init); | $(wikiTooltip.init); |
Revision as of 19:56, 5 September 2023
var wikiTooltip = { init: function() { var $tooltips = $('.wiki-tooltip .wiki-tooltip-content'); if ($tooltips[0]) { $tooltips.each(function() { var $this = $(this); // Check for links and handle them on handheld devices if ($this.closest('a').attr('href') !== undefined) { $this.closest('a').removeAttr('title'); if (widthRect <= 1000) { wikiTooltip.hasLink($this); } } // Prevent tooltips from overflowing the viewport edgeRect = $this.width() + $this.offset().left; widthRect = $(window).width(); if (edgeRect - widthRect > $this.width()*0) { $this.css('left', ''); $this.css('right', '100%'); } }); } }, hasLink: function(tooltip) { var $a = $('<a>'); var url = tooltip.closest('a'); var href = url.attr('href'); $a.attr('href', href); url.attr('href', 'javascript:void(0);'); tooltip.append($a); } }; $(wikiTooltip.init);