473,385 Members | 1,693 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,385 software developers and data experts.

Hideable Menu

Hello. I'm relatively new to Javascript, and I hope someone out there can help me. I have a menu in a <div> that I am trying to animate in such a way that it can be opened and closed by clicking (moving it on or off the left side of the screen.) The whole thing is activated by onclick="choice()"
I can get it to close fine, but when I click it again to open it, it flickers open and then closed again. AND: this is only in IE - in Firefox, clicking it does nothing. What follows is the code, any help would be greatly appreciated. Thanks!

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. var xpos=null;
  4. var xposr=null;
  5. var chooser=1;
  6. function choice() {
  7. if(chooser==1){
  8. chooser=2;
  9. hideMenu();
  10. } else {
  11. chooser=1
  12. revealMenu();
  13. }
  14. }
  15. function hideMenu() {
  16. document.getElementById('menudiv').style.left=xpos;
  17. menuHider();}
  18. function menuHider() {
  19. if(xpos<-160){
  20. clearInterval(hideMenu);
  21. } else {
  22. xpos=xpos-1;
  23. setInterval(hideMenu,120);
  24. }
  25. }
  26.  
  27. function revealMenu() {
  28. document.getElementById('menudiv').style.left=xposr;
  29. menuRevealer();}
  30. function menuRevealer() {
  31. if(xposr>=0){
  32. clearInterval(revealMenu);
  33. } else {
  34. xposr=xposr+1;
  35. setInterval(revealMenu,120);
  36. }
  37. }
  38.  
  39. </script>
Oct 27 '07 #1
5 2033
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

You need to add units when setting the style.left value, e.g. "px".

To use clearInterval, you will need to pass a valid ID used when setting the interval - see link.
Oct 29 '07 #2
OK, so that helped somewhat, but I'm a little simple it seems...
Perhaps if you could point out where I'm going wrong here?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. var xpos=0;
  4. var xposr=-160;
  5. var chooser=1;
  6. var interval1 = window.setInterval(animate, 500);
  7. var interval2 = window.setInterval(animate, 500);
  8. function choice() {
  9. if(chooser==1){
  10. hideMenu();
  11. chooser=2;
  12. } else {
  13. revealMenu();
  14. chooser=1
  15. }
  16. }
  17. function hideMenu() {
  18. document.getElementById('menudiv').style.left=xpos+'px';
  19. menuHider();}
  20. function menuHider() {
  21. if(xpos<=-160){
  22. clearInterval(interval1);
  23. } else {
  24. clearInterval(interval2);
  25. xpos=xpos-1;
  26. interval1 = setInterval(hideMenu,120);
  27. }
  28. }
  29.  
  30. function revealMenu() {
  31. document.getElementById('menudiv').style.left=xposr+'px';
  32. menuRevealer();}
  33. function menuRevealer() {
  34. if(xposr>=0){
  35. clearInterval(interval2);
  36. } else {
  37. clearInterval(interval1);
  38. xposr=xposr+1;
  39. interval2 = setInterval(revealMenu,120);
  40. }
  41. }
  42.  
  43.  
  44. </script>
It closes fine, but when I use the onclick to call the choice() and reopen it, it flickers on and off like mad.
Oct 30 '07 #3
acoder
16,027 Expert Mod 8TB
Why are you setting and clearing intervals continuously? Just set it once, let it run to completion and then clear it. You're probably better off combining the hide functions into one and the reveal functions into one function.
Oct 30 '07 #4
I'm calling the function continuously in order to get an animated scrolling effect - to make the menu hide over time.
Nov 2 '07 #5
acoder
16,027 Expert Mod 8TB
setInterval does this for you already. There's no need to clear the interval and set another interval each time. See the link I gave in post #2.
Nov 2 '07 #6

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

Similar topics

2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
1
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.