473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

showing and hiding an element

i am trying to show and hide a div when onmouseover and onmouseover another div element.
i am setting a setTimeout duration on onmouseout to delay the hiding of div for around two second
The problem is that when i mouseover an element and then onmouseout it and then back again mouseovers that element before the timeout, the element still gets hidden
so i put a flag=1 when i mouseover the element and flag=0 at mouseout and checked the value of the flag when calling the hiding function

still there was a problem
if i repeated mouseover and mouseout the element, at the time when the hiding function is called, if value of flag was 0, it hides the element before the timeout of last onmouseout even called
dont know if i made it any clear

here is my code
[HTML]<div id="checking" style="width:80px; height:20px; background-color:#666666; color:#FFFF00">hover here</div>[/HTML]
[HTML]<div id="mydiv" style="position:absolute; top:40px; left:300px; width:500px; height:400px; background-color:#33CCFF; z-index:1000; display:none " ></div>[/HTML]
Expand|Select|Wrap|Line Numbers
  1. var flag=0;
  2. var d=document.getElementById('mydiv');
  3. var s=document.getElementById('checking');
  4. function hiding()
  5. {
  6.     if(flag!=0)
  7.     return
  8.     d.style.display='none';    
  9. }
  10. s.onmouseover=function(){
  11.     d.style.display='';
  12.     flag=1;
  13. }
  14. s.onmouseout=function(){
  15.     flag=0;
  16.     setTimeout('hiding()',2000);
  17. }
  18.  
Feb 19 '08 #1
17 1969
i think i got it working but not too sure
I dont know if it can be achieved by any other way as well

i changed my code to include a timestamp in flag
and checked for the difference in current time and timestamp

Expand|Select|Wrap|Line Numbers
  1. var flag=(new Date()).getTime();
  2. var d=document.getElementById('mydiv');
  3. var s=document.getElementById('checking');
  4. function hiding()
  5. {
  6.     if((new Date()).getTime()-flag<1999)
  7.     return
  8.     d.style.display='none';
  9. }
  10. s.onmouseover=function(){
  11.  
  12.     d.style.display='';
  13.  
  14.     flag=(new Date()).getTime();
  15. }
  16. s.onmouseout=function(){
  17.     flag=(new Date()).getTime();
  18.     setTimeout('hiding()',2000);
  19. }
  20.  
Feb 19 '08 #2
acoder
16,027 Expert Mod 8TB
i think i got it working but not too sure
I dont know if it can be achieved by any other way as well

i changed my code to include a timestamp in flag
and checked for the difference in current time and timestamp
I think that should work. More robust than a simple flag.
Feb 19 '08 #3
yes i want to wait for a few seconds before hiding the div element if user has moved his mouse away from the hover element kinda putting a delayed hide but if the user gets back on the hover element before timeout of hiding of div, the div should not get hidden and a new timeout be set when the user hovers out of the hover element again
Feb 19 '08 #4
hehe where did your last post go
Feb 19 '08 #5
acoder
16,027 Expert Mod 8TB
hehe where did your last post go
Since you posted before I posted, it answered my question, so I deleted it. Didn't think you'd notice :p Still, it's good to confirm.
Feb 19 '08 #6
yea my last post was reply to your question and after posting i saw ur post not there :P
Feb 19 '08 #7
there is a little problem as well
when i put
Expand|Select|Wrap|Line Numbers
  1. if((new Date()).getTime()-flag<2000)
insted of
Expand|Select|Wrap|Line Numbers
  1. if((new Date()).getTime()-flag<1999)
the div sometimes stays there even when at timeout, not a big problem but just a difference of a millisecond removes it
but since i am checking for value less than 2000 ms, it should work every time as i am calling setTimeout function after 2000 ms
Feb 19 '08 #8
acoder
16,027 Expert Mod 8TB
there is a little problem as well
when i put
Expand|Select|Wrap|Line Numbers
  1. if((new Date()).getTime()-flag<2000)
insted of
Expand|Select|Wrap|Line Numbers
  1. if((new Date()).getTime()-flag<1999)
the div sometimes stays there even when at timeout, not a big problem but just a difference of a millisecond removes it
but since i am checking for value less than 2000 ms, it should work every time as i am calling setTimeout function after 2000 ms
I'm not sure of the timing issues. You should also test on a slower machine and see if that affects anything.
Feb 19 '08 #9
there is another problem now

i have put the functions i called at onmouseover and onmouseout on both the div elements now
and when i hover on 1st div, it opens the 2nd div
now if i put my 2nd div somewhat above the 1st div and take my mouse from 1st div to 2nd div without getting out from 1st div, the onmouseover event of 2nd div gets fired, and when i continue moving the mouse inside 2nd div and get out of 1st div, the onmouseout event of 1st div gets fired and that hides my 2nd div after 2 seconds even when i am hovering my mouse on 2nd div

here is my code
[HTML]<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<div id="checking" style="width:80px; height:20px; background-color:#666666; color:#FFFF00" onmouseover="show()" onmouseout="delayhide()">hover here</div>
<div id="mydiv" style="position:absolute; top:20px; left:60px; width:500px; height:400px; background-color:#33CCFF; z-index:1000; display:none " onmouseover="show()" onmouseout="delayhide()" ></div>
<script type="text/javascript">
var flag=(new Date()).getTime();
var d=document.getElementById('mydiv');
function hiding()
{
if((new Date()).getTime()-flag<1998)
return
d.style.display='none';
}
function show(){
d.style.display='';
flag=(new Date()).getTime();
}
function delayhide(){
flag=(new Date()).getTime();
setTimeout('hiding()',2000);
}
</script>
</body>
</html>[/HTML]
Feb 19 '08 #10
acoder
16,027 Expert Mod 8TB
there is another problem now

i have put the functions i called at onmouseover and onmouseout on both the div elements now
and when i hover on 1st div, it opens the 2nd div
now if i put my 2nd div somewhat above the 1st div and take my mouse from 1st div to 2nd div without getting out from 1st div, the onmouseover event of 2nd div gets fired, and when i continue moving the mouse inside 2nd div and get out of 1st div, the onmouseout event of 1st div gets fired and that hides my 2nd div after 2 seconds even when i am hovering my mouse on 2nd div
So what do you want it to do instead?
Feb 19 '08 #11
i want that when my mouse is over any of my divs with show and hide functions, the div should not hide
but what happens is that if i mouseover a div which shows the 2nd div, and if the 2nd div overlaps the 1st div from any sides and i move my mouse to the 2nd div without going out of 1st div, it fires the onmouseover event of 2nd div and while still moving inside the 2nd div, if the boundaries of 1st div are crossed, the mouseout event from 1st div gets fired and my 2nd div gets hidden even when i am hovering over it
The html code i gave in my last post is a working demo
u can try it out and see the problem
Feb 20 '08 #12
acoder
16,027 Expert Mod 8TB
This link has a lot of useful information on event orders, bubbling and capturing. You need to stop the propagation of events with stopPropagation() or setting cancelBubble to false (the last one for IE).
Feb 20 '08 #13
i tried to rework the code today and i think the problem is solved
i will also look into stopPropogation and cancelBubble as well after this

this is the code i am using

Expand|Select|Wrap|Line Numbers
  1. var flaga=new Array();
  2. var flagb=new Array();
  3. var d=document.getElementById('mydiv');
  4. function hiding()
  5. {
  6.     for(var i=0;i<flaga.length;i++)
  7.     {
  8.         if((new Date()).getTime()-flaga[i]<1998)
  9.             return
  10.         else if(flagb[i]==1)
  11.             return
  12.     }
  13.     d.style.display='none';
  14. }
  15. function show(val){
  16.     flaga[val]=(new Date()).getTime();
  17.     flagb[val]=1;
  18.     d.style.display='';
  19. }
  20. function delayhide(val){
  21.     flaga[val]=(new Date()).getTime();
  22.     flagb[val]=0;
  23.     setTimeout('hiding()',2000);
  24. }
[HTML]<div id="checking" style="width:80px; height:20px; background-color:#666666; color:#FFFF00" onmouseover="show(0)" onmouseout="delayhide(0)">hover here</div>
<div id="mydiv" style="position:absolute; top:20px; left:60px; width:500px; height:400px; background-color:#33CCFF; z-index:1000; display:none; text-align:center; vertical-align:middle " onmouseover="show(1)" onmouseout="delayhide(1)" ></div>
[/HTML]
Feb 20 '08 #14
acoder
16,027 Expert Mod 8TB
i tried to rework the code today and i think the problem is solved
i will also look into stopPropogation and cancelBubble as well after this
It might work, but it looks very hack-ish. You'll find cancelling the event propagation is cleaner.
Feb 20 '08 #15
i tried using stopPropagation in mozilla
but it is not working properly

can u check my code and test page
Expand|Select|Wrap|Line Numbers
  1.  
  2. var elems=document.getElementsByTagName("div");
  3. var d=document.getElementById("mydiv");
  4. var flag=(new Date()).getTime();
  5. function show(val)
  6. {
  7.     flag=(new Date()).getTime();
  8.     if(d.style.display!='')
  9.     {
  10.         d.style.display='';
  11.     }
  12.     for(var i=0;i<elems.length;i++)
  13.     {
  14.         if(elems[i].id!=val)
  15.         {
  16.             elems[i].addEventListener("mouseout",stopev,false);
  17.         }
  18.         else
  19.         {
  20.             elems[i].addEventListener("mouseout",hide,false);
  21.         }
  22.     }
  23. }
  24. function hide()
  25. {
  26.     flag=(new Date()).getTime();
  27.     setTimeout('hiding()',2000);
  28. }
  29. function stopev(evt)
  30. {
  31.     evt.stopPropagation();
  32. }
  33. function hiding()
  34. {
  35.     if((new Date()).getTime()-flag<1998)
  36.     return
  37.     d.style.display='none';
  38. }
  39.  
here is the test page
[HTML]
<html>

<body>
<div id="checking" style="width:80px; height:20px; background-color:#666666; color:#FFFF00" onmouseover="show('checking')" onmouseout="hide()">hover here</div>
<div id="mydiv" style="position:absolute; top:20px; left:60px; width:500px; height:400px; background-color:#33CCFF; z-index:1000; display:none; text-align:center; vertical-align:middle " onmouseover="show('mydiv')" onmouseout="hide()" >sdfsdf</div>
<script type="text/javascript">

var elems=document.getElementsByTagName("div");
var d=document.getElementById("mydiv");
var flag=(new Date()).getTime();
function show(val)
{
flag=(new Date()).getTime();
if(d.style.display!='')
{
d.style.display='';
}
for(var i=0;i<elems.length;i++)
{
if(elems[i].id!=val)
{
alert(elems[i].id+"\'s mouseout disabled");
elems[i].addEventListener("mouseout",stopev,false);
}
else
{
elems[i].addEventListener("mouseout",hide,false);
}
}
}
function hide()
{
flag=(new Date()).getTime();
setTimeout('hiding()',2000);
}
function stopev(evt)
{
evt.stopPropagation();
}
function hiding()
{
if((new Date()).getTime()-flag<1998)
return
d.style.display='none';
}
</script>
</body>
</html>
[/HTML]
Feb 22 '08 #16
rnd me
427 Expert 256MB
i don't know how this ever got so complicated!

i could not follow.
i added two clearTimeouts to the code from the first post, and everything seems to be working for me in ie7 now.



Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <div id="checking" style="width:80px; height:20px; background-color:#666666; color:#FFFF00">hover here</div>
  4.  
  5.  
  6. <div id="mydiv" style="position:absolute; top:40px; left:300px; width:500px; height:400px; background-color:#33CCFF; z-index:1000; display:none " ></div> 
  7.  
  8. <script type='text/javascript'>
  9.  
  10.  
  11.  
  12. var flag=0;
  13. var d=document.getElementById('mydiv');
  14. var s=document.getElementById('checking');
  15. function hiding()
  16. {     clearTimeout(d.to);
  17.     if(flag!=0)
  18.     return
  19.     d.style.display='none';    
  20. }
  21. s.onmouseover=function(){
  22.     d.style.display='';
  23.     flag=1;
  24. }
  25. s.onmouseout=function(){
  26.     clearTimeout(d.to);
  27.     flag=0;
  28.     d.to=setTimeout(hiding,2000);
  29. }
  30. </script>    
  31.  
  32.  
  33. </html>

am i missing something, or does this work?
it wont vanish until 2 secs after the last mouseout.
Feb 22 '08 #17
acoder
16,027 Expert Mod 8TB
i added two clearTimeouts to the code from the first post, and everything seems to be working for me in ie7 now.
Good point about the clearTimeout, but the second div now overlaps - see post #14.
Feb 22 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dré | last post by:
Hi, I'm making a FAQ-page. To keep it a little synoptic, only the questions are visible. When you klick on a certain question, the answer appears. You can find an example at:...
8
by: mcheu | last post by:
Hi, I'm trying to figure out how to hide text in a DIV block and then hide and unhide it using javascript. In the code that follows, the message is initially invisible, and when you click on...
4
by: Christopher Benson-Manica | last post by:
Obviously, compiliant browsers that encounter <script type="text/javascript"> <!-- // your script here // --> </script> will either execute the script or ignore it completely. However, we...
18
by: ashkaan57 | last post by:
Hi, how can I hide / show a section of a page when a link is clicked? Click on "show section" would show the section and change the link to "hide section", clicking on "hide section" would hide...
2
by: c.anandkumar | last post by:
Hi All - I have some problems getting a small piece of javascript working correctly for Firefox. Here is what I am trying to do - 1. I have a form (like a search form) 2. I have many groups...
2
by: EventListener | last post by:
I have a folder/file tree that is dynamically generated from an xml file. The way I've written it seems to work. Since I'm a fairly novice javascript programmer, I'm concerned that there may be a...
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
3
by: Charlie Dison | last post by:
Hi There, Should I be able to show and hide panels in an asp.net page without requiring a postback? I thought there was a way to do this using java script. Can anyone give me an example? I...
2
by: =?Utf-8?B?Sm9zaCBTY2htaWR0?= | last post by:
I have a gridview that is being used for managing inventory. The default view shows the stock currently available. When editing I don't want the stock to be directly edited, rather the user will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.