MediaWiki:Gadget-tooltipPopup-core.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "// Adapted from // https://stackoverflow.com/questions/12539006/tooltips-for-mobile-browsers function init() { $('.add-tooltip-popup').click(function () { var $title = $(this).find('.title'); if (!$title.length) { // Remove any existing tooltips $('.add-tooltip-popup .title').remove(); const tooltip = $(document.createElement('span')); tooltip.addClass('title').text($(this).attr('title')); tooltip.css('left', 0); $(thi...")
 
No edit summary
 
Line 1: Line 1:
"use strict";

// Adapted from
// Adapted from
// https://stackoverflow.com/questions/12539006/tooltips-for-mobile-browsers
// https://stackoverflow.com/questions/12539006/tooltips-for-mobile-browsers
Line 7: Line 9:
// Remove any existing tooltips
// Remove any existing tooltips
$('.add-tooltip-popup .title').remove();
$('.add-tooltip-popup .title').remove();
var tooltip = $(document.createElement('span'));
const tooltip = $(document.createElement('span'));
tooltip.addClass('title').text($(this).attr('title'));
tooltip.addClass('title').text($(this).attr('title'));
tooltip.css('left', 0);
tooltip.css('left', 0);
Line 14: Line 15:


// Reposition offscreen tooltip
// Reposition offscreen tooltip
const endPosition = tooltip.offset().left + tooltip.width();
var endPosition = tooltip.offset().left + tooltip.width();
const windowWidth = $(window).width();
var windowWidth = $(window).width();
const overflowAmount = Math.max(0, endPosition - windowWidth + 15); // leave 15px right padding
var overflowAmount = Math.max(0, endPosition - windowWidth + 15); // leave 15px right padding
tooltip.css('left', -overflowAmount);
tooltip.css('left', -overflowAmount);
} else {
} else {
Line 23: Line 24:
});
});
}
}

$(init);
$(init);

Latest revision as of 12:06, 20 October 2024

"use strict";

// Adapted from
// https://stackoverflow.com/questions/12539006/tooltips-for-mobile-browsers
function init() {
  $('.add-tooltip-popup').click(function () {
    var $title = $(this).find('.title');
    if (!$title.length) {
      // Remove any existing tooltips
      $('.add-tooltip-popup .title').remove();
      var tooltip = $(document.createElement('span'));
      tooltip.addClass('title').text($(this).attr('title'));
      tooltip.css('left', 0);
      $(this).append(tooltip);

      // Reposition offscreen tooltip
      var endPosition = tooltip.offset().left + tooltip.width();
      var windowWidth = $(window).width();
      var overflowAmount = Math.max(0, endPosition - windowWidth + 15); // leave 15px right padding
      tooltip.css('left', -overflowAmount);
    } else {
      $title.remove();
    }
  });
}
$(init);