/    Sign up×
Community /Pin to ProfileBookmark

Mouse Event Issue

Hello,

I am having trouble setting the style.top and style.left of a box with the current coordinates of the mouse. The box is hidden at first, but then when you click a link it displays, I want the box to show up where the mouse currently is. This is important so that I can use this script on other links and have the box be positioned relevant to the link.

Here is the code, let me know what I’m doing wrong:

[code=html]// Setup hooks
function loadRequestPop() {
if (!document.getElementById) return false;

if (document.getElementById(‘requestPopLink’)) {
var x = document.getElementById(‘requestPopLink’);
x.onclick = function() { overlay(this, ‘requestPop’); return false; }
}

if (document.getElementById(‘iconHpRequest’)) {
var icon = document.getElementById(‘iconHpRequest’);
icon.onclick = function() { overlay(this, ‘requestPop’); return false; }
}

if (document.getElementById(‘joinNewsletter’)) {
var test = document.getElementById(‘joinNewsletter’);
test.onclick = function() { overlay(this, ‘joinNewsletterBox’); return false; }
}

if (document.getElementById(‘requestPopClose’)) {
var y = document.getElementById(‘requestPopClose’);
y.onclick = function() { overlayclose(‘requestPop’); return false; }
}

if (document.getElementById(‘whathappenedClose’)) {
var z = document.getElementById(‘whathappenedClose’);
z.onclick = function() { overlayclose(‘whathappened’); return false; }
}

if (document.getElementById(‘joinNewsletterClose’)) {
var b = document.getElementById(‘joinNewsletterClose’);
b.onclick = function() { overlayclose(‘joinNewsletterBox’); return false; }
}

}

//Get Mouse Position
function getMouse(e)
{
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY)
{
this.posx = e.pageX;
this.posy = e.pageY;
}
else if (e.clientX || e.clientY)
{
this.posx = e.clientX + document.body.scrollLeft;
this.posy = e.clientY + document.body.scrollTop;
}
}

// Create box and display
function overlay(curobj, subobj) {
if (document.getElementById) {
var subobj = document.getElementById(subobj);
subobj.style.left = posx+”px”; // here is my issue
subobj.style.top = posy+”px”;
subobj.style.display = “block”;
return false;
}
else
return true;
}

// Close box
function overlayclose(subobj) {
document.getElementById(subobj).style.display = “none”;
}

addLoadEvent(loadRequestPop);[/code]

Thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 19.2006 — Have you tested that in NS7+ or Firefox? It seems that this will work there. As for IE:

this.posx = e.clientX + document.body.scrollLeft;

this.posy = e.clientY + document.body.scrollTop;

That will only work if you're not running in Strict mode. Otherwise, I would change this:
<i>
</i>function getMouse(e)
{
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY)
{
this.posx = e.pageX;
this.posy = e.pageY;
}
else if (e.clientX || e.clientY)
{
this.posx = e.clientX + document.body.scrollLeft;
this.posy = e.clientY + document.body.scrollTop;
}
}

to this:
<i>
</i>function getMouse(e)
{
var posx = 0;
var posy = 0;
if (window.Event)
{
this.posx = e.pageX;
this.posy = e.pageY;
}
else if (document.documentElement &amp;&amp; document.documentElement.scrollTop)
{
this.posx = event.clientX + document.documentElement.scrollLeft;
this.posy = event.clientY + document.documentElement.scrollTop;
}
else
{
this.posx = event.clientX + document.body.scrollLeft;
this.posy = event.clientY + document.body.scrollTop;
}
}
Copy linkTweet thisAlerts:
@mididelightauthorMar 19.2006 — The issue I am getting is not with the getMouse function but implementing that into the overlay function. I pass to parameters to that function, the first one is the link that is clicked and the second one is the id of the link that was clicked. How do I implement the mouse coordinates into the overlay function? I get an error saying that e is not defined when I try to use it.
Copy linkTweet thisAlerts:
@phpnoviceMar 19.2006 — Which line in which function generates this error?
×

Success!

Help @mididelight 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...