Solved

Javascript to detect the presence of Mouse inside a DIV

Posted on 2007-04-02
8
1,304 Views
Last Modified: 2012-05-05
I have a DIV which scrolls across the screen with JS.
I want to detect a mouseOver event in my JS fun to "pause" the scroll effect while the mouse is inside the DIV area.

How do I detect that the mouse is inside the DIV box?
0
Comment
Question by:MattCoatney
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:cookiej
ID: 18839965
<div id="mydiv" onmouseover="alert('I am in the div');">This will pop up an alert</div>
0
 

Author Comment

by:MattCoatney
ID: 18840082
That won't work well in this case, since Im doing processing in another function, and this other function is what needs to know if the mouse is inside the div.

The function is looped with a timer. Every 10ms the fun is called and moves the DIV. BEFORE it moves the DIV it needs to check to see if the mouse is hovering in the area of the DIV, in which case the DIV would not be moved.
0
 
LVL 6

Accepted Solution

by:
ije earned 500 total points
ID: 18840153
make sure your timing function has a name - ie:

var objTimer = setTimeout('move()', 10);

document.getElementByid('mydiv').onmouseover = function(){
  objTimer.clearInterval();
}
0
 
LVL 2

Expert Comment

by:cookiej
ID: 18840198
Could it be as easy as:

<div id='scrollwindow' onmouseover="scrolling=false" onmouseout="scrolling=true"></div>

And have your recurring function just check to see if scrolling is true/false?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:cookiej
ID: 18840215
Allow me to be more clear and not use reseved keywords:

<div id='scrollwindow' onmouseover="iShouldBeScrolling=false" onmouseout="iShouldBeScrolling=true"></div>

function timedScroller() {
    if(!iShouldBeScrolling) return;
...

[scrolling code goes here]

...
}
0
 

Author Comment

by:MattCoatney
ID: 18840511
Solutions getting close but not quite there. Ok let me clarify a couple points I was too vague about.

first, when I said scroll I meant that the div moves across the screen. Not anything to do with the scroll property or scrollable panels etc.

Second, the div has content. I think this is preventing these solutions from working, because the DIV is actually tightly wrapped around an image and other content so I dont think the mouse is being detected as inside the DIV by these methods.


0
 
LVL 2

Expert Comment

by:cookiej
ID: 18850818
The proposed solution has no relationship to any sort of scrolling property.  By your description, you're using a timed function to animate the div.  This should be relatively straightforward.  Put the "if (!shouldBeScrolling) return" in at the top oif the function that does the animation and I'll bet it will work.  If not, post more of the code here.

The div having content should not affect the  event from triggering correctly unless you're trapping events elsewhere.
0
 

Author Comment

by:MattCoatney
ID: 18874214
I tried the answer that ended up working before I came here but there were complications that prevented it from working. Once I understood those things, the answer was much simpler than I expected so... thanks. Thanks to all for effort.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now