Tooltip.js: Difference between revisions
From The-West Wiki
(Created page with "$(document).ready(function() { const toolTip = { delay: 200, timer: null, active: false, removeDefaultTooltip: function() { $("[title]").hover(function() { const titleValue = $(this).attr("title"); if (titleValue) { // Check if titleValue is defined $(this).attr("custom-title", titleValue).removeAttr("title"); } }); }, getEl: function() { return $(".overlay-popup"); },...") |
No edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
removeDefaultTooltip: function() { | removeDefaultTooltip: function() { | ||
$("[title], [alt]").hover(function() { | |||
const titleValue = $(this).attr("title") || $(this).attr("alt"); | |||
if (titleValue) { | |||
$(this).attr("custom-title", titleValue).removeAttr("title").removeAttr("alt"); | |||
} | |||
}); | |||
}, | |||
getEl: function() { | getEl: function() { | ||
return $(".overlay- | return $(".overlay-popup1"); | ||
}, | }, | ||
getContainer: function() { | getContainer: function() { | ||
return $(".overlay- | return $(".overlay-contents1"); | ||
}, | }, | ||
show: function(element) { | show: function(element) { | ||
console.log("Trying to show tooltip"); | // console.log("Trying to show tooltip"); | ||
const content = $(element).attr("custom-title"); | const content = $(element).attr("custom-title"); | ||
if (content) { | if (content) { | ||
Line 34: | Line 34: | ||
} | } | ||
}, | }, | ||
hide: function() { | hide: function() { | ||
Line 92: | Line 93: | ||
this.removeDefaultTooltip(); | this.removeDefaultTooltip(); | ||
$("body").append('<div class="overlay- | $("body").append('<div class="overlay-popup1"><div class="popup_front1"><div class="tw_bg_tl1"></div><div class="tw_bg_tr1"></div><div class="tw_bg_bl1"></div><div class="tw_bg_br1"></div></div><div class="overlay-contents1"></div></div>'); | ||
this.bindEvents(); | this.bindEvents(); | ||
} | } |
Latest revision as of 18:53, 28 October 2023
$(document).ready(function() {
const toolTip = { delay: 200, timer: null, active: false,
removeDefaultTooltip: function() {
$("[title], [alt]").hover(function() { const titleValue = $(this).attr("title") || $(this).attr("alt"); if (titleValue) { $(this).attr("custom-title", titleValue).removeAttr("title").removeAttr("alt"); } });
},
getEl: function() { return $(".overlay-popup1"); }, getContainer: function() { return $(".overlay-contents1"); },
show: function(element) { // console.log("Trying to show tooltip"); const content = $(element).attr("custom-title"); if (content) { // console.log("Content found: ", content); this.getContainer().html(this.createPopup(content)); this.active = true; this.setTimeout(); } else { // console.log("Content not found for element: ", element); } },
hide: function() { this.getEl().css({ display: "none", top: 0, left: 0 }); this.active = false; this.clearTimeout(); },
setTimeout: function() { this.clearTimeout(); this.timer = window.setTimeout(() => { this.getEl().css("display", "block"); }, this.delay); },
clearTimeout: function() { if (this.timer) { window.clearTimeout(this.timer); } },
createPopup: function(content) {
return `
${content}
`;
},
setPosition: function(event) { const winWidth = $(window).width(); const winHeight = $(window).height(); const popupWidth = this.getEl().outerWidth(); const popupHeight = this.getEl().outerHeight(); const x = event.clientX; const y = event.clientY;
let leftPos = x + popupWidth + 20 > winWidth ? x - popupWidth - 20 + (event.pageX - x) : x + 20 + (event.pageX - x); let topPos = y + popupHeight + 20 > winHeight ? winHeight - popupHeight + (event.pageY - y) : y + 20 + (event.pageY - y); this.getEl().css({ top: topPos, left: leftPos }); },
bindEvents: function() { const tooltipEl = "[custom-title]";
$(document).on("mouseenter", tooltipEl, (event) => { this.show(event.target); this.setPosition(event); });
$(document).on("mouseleave", tooltipEl, () => { this.hide(); });
$(document).on("mousemove", tooltipEl, (event) => { this.setPosition(event); }); },
init: function() { this.removeDefaultTooltip();
$("body").append('
');
this.bindEvents(); } };
toolTip.init();
});