Solved

Jquery toggle timer auto close

Posted on 2014-09-24
4
1,198 Views
Last Modified: 2014-09-28
I'm using the basic jquery function toggle to click open a sliding menu on a site I'm developing. What I'd like to do is to have the menu appear OPEN when the user comes to the page, but to automatically slide shut (visually) after 5s

As it is now, I created the menu button and when clicked, it opens and closes the div just fine. I actually had the div set to display:none to begin with, which essentially makes the div invisible unless the user clicks the menu button.

But now I'd like to have the menu appear on page-load (as it does when I removed the display:none in the css) but after 5 seconds, auto-slide off the screen just like what happens when a user clicks the menu icon at the top. The menu icon works to toggle it back and forth (as it should) but I don't want the menu to be static unless the user manually clicks the menu icon. The problem is that many won't know how to get the menu to disappear without clicking the button and if I add the auto slide (toggle) to the code after 5s it will visually help them see that it moves.

I tried to add the same DIV that is used in the header div on the page as a button within the menu itself, but it won't display for some reason. I thought maybe it would be a visual "CLOSE" button/img that people could see how to close it without having to use the menu icon in the header.

here's the development page

I'm not sure how to amend the javascript to add a timer function for it to close after 5s. I want it to close after 5s regardless if the user pressed the menu icon to re-open it.
0
Comment
Question by:day6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:foxymoron7
ID: 40342963
Have you tried something like:

$(window).load(function(){
$("div.fnav").delay(500).animate({width:'toggle'}); // set div to slide out 5 sec after page load
$("#navimg").click(function () {
    $("div.fnav").animate({width: 'toggle'}).delay(500).animite({width:'toggle'}); // set div to slide out 5 sec after click
});
});

Open in new window

0
 
LVL 1

Author Comment

by:day6
ID: 40347606
foxymoron7 the script definitely slides it out after 5 seconds of page loading, but doesn't autoclose if I've clicked the menu button. It just stays open. Any thoughts? I'll give you credit for this but it would be great to know how to make it autoclose after the menu clicks. I think your timer is based on window .load isn't it?
0
 
LVL 1

Accepted Solution

by:
foxymoron7 earned 500 total points
ID: 40348124
My bad, the .animate() function takes an argument called "complete" which is a function that fires after the animation effect completes.  Put the delayed close in an anonymous function there.  See below.  The number inside delay is the number of milliseconds to wait, so 1000 = 1 sec. The second argument of animate() (400 in this case) is the duratin of the animation in milliseconds.  400 is the default, a higher number means slower.

see http://api.jquery.com/animate/

$(window).load(function(){
$("div.fnav").delay(8000).animate({width:'toggle'}); // set div to slide out 8 sec after page load
$("#navimg").click(function () {
    $("div.fnav").animate({width: 'toggle'}, 400, function(){ $(this).delay(8000).animate({width:'toggle'})}); // set div to slide out 8 sec after click 
	});

Open in new window

0
 
LVL 1

Author Closing Comment

by:day6
ID: 40348585
Nice work!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSS Formatting Getting Stripped 1 35
WordPress Creating scrolling box at top of page 1 32
css messed up 8 30
Submit button if clicked twice submits twice 1 24
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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…

752 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