Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:11:59+00:00 2026-06-01T08:11:59+00:00

I have the following bit of code which is responsible for displaying a tooltip.

  • 0

I have the following bit of code which is responsible for displaying a tooltip. I am unhappy with this code for two reasons:

  • I use pageXOffset and pageYOffset ‘magic numbers’ to correct the visual state per-browser.
  • The dialog window must remain stationary for the numbers to be correct.

I have tried binding to the dialog window’s mousemove event instead of the document. The results were identical to my current implementation which binds to document’s mousemove.

var shouldDisplay = false;
$(document).mousemove(AdjustToolTipPosition);

function DisplayTooltip(tooltip_text) {
    shouldDisplay = (tooltip_text != "") ? true : false;
    if (shouldDisplay) {
        $('#CustomTooltip').html(tooltip_text);
        $('#CustomTooltip').show();
    }
    else {
        //Sometimes the tooltip hasn't finished fading in before we ask to hide it. This causes it to hide, then fade back in.
        $('#CustomTooltip').hide();
    }
}

function AdjustToolTipPosition(e) {
    if (shouldDisplay) {
        //msie e.page event should be standardizes, but seems to go awry when working inside of a modal window.
        var pageYOffset = $.browser.msie ? 260 : 572; //-314
        var pageXOffset = $.browser.msie ? 474 : 160; //+314

        $('#CustomTooltip').css('top', e.pageY - pageYOffset + 'px');
        var offsetLeft = e.pageX - pageXOffset;
        var isOutsideViewport = $("#HistoricalChartDialog").width() - $("#CustomTooltip").width() - offsetLeft < 0;

        //Prevent the tooltip from going off the screen by changing the offset when it would go off screen.
        if (isOutsideViewport) {
            offsetLeft = $("#HistoricalChartDialog").width() - $("#CustomTooltip").width();
        }

        $('#CustomTooltip').css('left', offsetLeft + 'px');
    }
}

// Initialize the Historical Chart dialog.        
$('#HistoricalChartDialog').dialog({
    autoOpen: false,
    buttons: {
        'Close': function() {
            $(this).dialog('close');
        }
    },
    hide: 'fold',
    modal: true,
    draggable: false,
    resizable: false,
    position: 'center',
    title: 'Historical Charts',
    width: 700,
    height: 475
});

I provide the jQuery dialog initializer just for the sake of it. The tooltip only displays inside of this dialog window — but the coordinates are for the entire page. Is it possible to retrieve coordinates relative to the dialog window so that I can leverage the fact that jQuery’s mousemove standardizes coordinates with the pageX and pageY properties?

EDIT solution:

//Seperate file because the offsets are different for the image under MVC.
var shouldDisplay = false;
$("#HistoricalChartDialog").mousemove(AdjustToolTipPosition);

function DisplayTooltip(tooltip_text) {
    shouldDisplay = (tooltip_text != "") ? true : false;
    if (shouldDisplay) {
        $('#CustomTooltip').html(tooltip_text);
        $('#CustomTooltip').show();
    }
    else {
        //Sometimes the tooltip hasn't finished fading in before we ask to hide it. This causes it to hide, then fade back in.
        $('#CustomTooltip').hide();
    }
}

function AdjustToolTipPosition(e) {
    if (shouldDisplay) {
        var xPos = e.pageX - $(this).closest('.ui-dialog').offset().left + 15;
        var widthDifference = $(this).width() - $("#CustomTooltip").width();
        //Prevent the tooltip from going off the screen by changing the offset when it would go off screen.
        xPos = (widthDifference - xPos < 0) ? widthDifference : xPos;
        $('#CustomTooltip').css('left', xPos + 'px');

        var yPos = e.pageY - $(this).closest('.ui-dialog').offset().top - 10;
        $('#CustomTooltip').css('top', yPos + 'px');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:12:00+00:00Added an answer on June 1, 2026 at 8:12 am

    To get the position of the mouse relative to a specific div, not the viewport, you take the eventX/Y and subtract the left/top position of the div:

    $("#example").mousemove(function(e) {
        var xPos = e.pageX - $(this).position().left;
        var yPos = e.pageY - $(this).position().top;
        $("#pos").text("x: " + xPos + " / y: " + yPos);
    });
    

    Example fiddle

    Given your example, this should work. You may need to look at your isOutsideViewport logic though.

    function AdjustToolTipPosition(e) {
        if (shouldDisplay) {
            var xPos = e.pageX - $(this).position().left;
            var yPos = e.pageY - $(this).position().top;
    
            var isOutsideViewport = $("#HistoricalChartDialog").width() - $("#CustomTooltip").width() - xPos < 0;
            if (isOutsideViewport) {
                offsetLeft = $("#HistoricalChartDialog").width() - $("#CustomTooltip").width();
            }
    
            $('#CustomTooltip').css({
                'top': yPos + 'px',
                'left': xPos + 'px'
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation in code, which I suspect may be a bit
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
i have the following bit of C code which reads from a pipe and
I have the following bit of code which I'm using to delete all of
I have the following bit of code which runs a SQL statement: int rowsEffected
Hello I have the following bit of code which seems to work, but I'm
i have the following code which draws nothing. If i use glBegin(GL_POINTS) it draws
I have the following bit of code which I'm just trying out by running
I have the following bit of ruby code which works fine require 'WIN32OLE' excel
Hi all i have the following bit of code, which is used to generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.