/    Sign up×
Community /Pin to ProfileBookmark

follow mouse problem

I have a funtion that lets a div follow the mouse on a tooltip. The thing is my tooltips appear above what they hover and if there longer then 2 lines they over lap the content.

it’s sorta hard to explain but I need this function to postion using “bottom” not “top” so that no matter how big the tooltip gets it will always sit above the content it is hovering.

Thanks sooo much for anyone who can help.

[code=html]function followMouse(e){
// offset from the cursor
var offsetx = -27;
var offsety = -60;
var posx = 0;
var posy = 0;
var tooltip = document.getElementById(‘tooltip’);
if (!e) {
var e = window.event;
}
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.documentElement.scrollLeft;
posy = e.clientY + document.documentElement.scrollTop;
}
if (window.innerWidth || window.innerHeight){
docwidth = window.innerWidth;
docheight = window.innerHeight;
} else if (document.documentElement.clientHeight || document.documentElement.clientWidth) {
docwidth = document.documentElement.clientWidth;
docheight = document.documentElement.clientHeight;
} else if (document.body.clientWidth || document.body.clientHeight){
docwidth = document.body.clientWidth;
docheight = document.body.clientHeight;
}
// if tooltip goes beyond visible area, move it higher by it’s width
if ((docheight – (posy + offsety + tooltip.offsetHeight)) < 0 ) {
offsety *= -1;
offsety -= (tooltip.offsetHeight+offsety);
}
// same for the width
if ((docwidth – (posx + offsetx + tooltip.offsetWidth)) < 0 ) {
offsetx *= -1;
offsetx -= (tooltip.offsetWidth+offsetx);
}

tooltip.style.left = posx + offsetx + ‘px’;
tooltip.style.top = posy + offsety + ‘px’;
}

// the tooltip div
document.write(‘<div id=”tooltip” style=”display: none; position: absolute; top: 0; left; 0;”></div>’);[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinFeb 06.2006 — see what happens when you set the div position to relative instead of absolute
Copy linkTweet thisAlerts:
@SelrachFeb 06.2006 — You could also position with style attribute of bottom.

style.bottom = posX;

and in the div's declaration set bottom: 0px

you'll have to adjust offsets and things, but i think it will do what you want.
Copy linkTweet thisAlerts:
@chris9902authorFeb 07.2006 — I can't seem to work it out so I just move it to the left.

It works ok now but not how I wanted it.

thanks anyway
×

Success!

Help @chris9902 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...