Jump to: content

Tooltip.js: Difference between revisions

From The-West Wiki
Jump to:navigation Jump to:search
No edit summary
No edit summary
 
Line 6: Line 6:
          
          
removeDefaultTooltip: function() {
removeDefaultTooltip: function() {
$("[title]").hover(function() {
    $("[title], [alt]").hover(function() {
const titleValue = $(this).attr("title");
        const titleValue = $(this).attr("title") || $(this).attr("alt");
if (titleValue) { // Check if titleValue is defined
        if (titleValue) {
$(this).attr("custom-title", titleValue).removeAttr("title");
            $(this).attr("custom-title", titleValue).removeAttr("title").removeAttr("alt");
}
        }
});
    });
},
},
         getEl: function() {
         getEl: function() {
Line 22: Line 22:
         },
         },


show: function(element) {
show: function(element) {
    const content = $(element).attr("custom-title");
// console.log("Trying to show tooltip");
    if (content) {
const content = $(element).attr("custom-title");
        console.log("Content found: ", content);
if (content) {
        this.getContainer().html(this.createPopup(content));
// console.log("Content found: ", content);
        console.log("Display before changing: " + this.getEl().css("display"));
this.getContainer().html(this.createPopup(content));
        this.active = true;
this.active = true;
        this.setTimeout();
this.setTimeout();
        console.log("Display after changing: " + this.getEl().css("display"));
} else {
    } else {
// console.log("Content not found for element: ", element);
        console.log("Content not found for element: ", element);
}
    }
},
},





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();

});