Floating SlideIn Menu with Anchor Link using jQuery

Advertisement
Post Pic

Introduction:

This article will help you in developing simple Floating Slide-In Menu using jQuery. This article is very useful for beginners, by looking at the code anybody can understand the logic behind the same. For this I have used CSS Sprite (Cascading Style Sheets) for floating the menu & for toggle button accordingly and used basic level of jQuery to animate the menu accordingly.

Prerequisite knowledge:

In order to understand the code, one should know basic level of CSS & jQuery, a bit HTML.

Ingredients Used:

  • jQuery
  • Cascading Style Sheets (CSS)
  • HTML
  • Photoshop (Just for simple designing toggle button)

jQuery SlideIn Floating Menu

jQuery SlideIn Floating Menu

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
body{
	font-family:Georgia, "Times New Roman", Times, serif;
	margin-left:20px;
	padding-left:10px;
	border-left:1px solid #999999;
}
 
#slideMenu.closed{
	left:-300px;
}
 
#slideMenu{
	position:fixed;
	left:0;
	top:20px;
	width:300px;
	height:300px;
	border:5px solid #416888;
	border-left:0px;
	background-color:#FFFFFF;
	z-index:20;
}
 
#slideMenu a.toggleBtn{
	position:absolute;
	right:0;
	top:0;
	outline:none;
	display:block;
	height:87px;
	width:27px;
	border-width:1px 1px 1px 0px;
	margin:0;
	padding:0 5px 0;
	color:#000;
	text-decoration:none;
	font:12px/25px Verdana, Arial, Helvetica, sans-serif;
	background:url(images/arrow.gif) no-repeat;
	z-index:20;
}
 
#slideMenu a.toggleBtnHighlight{
	position:absolute;
	right:0;
	top:0;
	outline:none;
	display:block;
	height:87px;
	width:27px;
	border-width:1px 1px 1px 0px;
	margin:0;
	padding:0 5px 0;
	color:#000;
	text-decoration:none;
	font:12px/25px Verdana, Arial, Helvetica, sans-serif;
	background:url(images/arrow.gif) no-repeat 0px -88px;
	z-index:20;
}
 
.content{
	padding:5px;
	z-index:20;
}

HTML Code:

1
2
3
4
5
6
7
8
9
10
11
<div id="slideMenu" class="closed">
	<div class="content">
	<strong>Quick Links</strong>
		<ul>
			<li><a href="#myAnchor" class="anchorLink" title="Anchor Link">Anchor Link</a></li>
			<li><a href="http://www.developersnippets.com/about/" title="About Me">About Me</a> </li>
			<li>Back to Article</li>
		</ul>
	</div>
	<a style="top:100px; right: -42px;" href="#" class="toggleBtn" id="toggleLink"></a>
</div>

jQuery Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
*****************************************
Author: Vivekanand
Website: http://www.developersnippets.com
*****************************************
*/
$(document).ready(function(){
	$(".toggleBtn").click(function(e){
                        e.preventDefault(); // prevent the anchor tag from sending the user off to the link that is href
			if($("#slideMenu").hasClass('closed')){
				$("#slideMenu").animate({left:0}, 500, function(){
					$(this).removeClass('closed').addClass('opened');
					$("a#toggleLink").removeClass('toggleBtn').addClass('toggleBtnHighlight');
				});
			}//if close
 
			$('#slideMenu').bind("mouseleave",function(){
				$("#slideMenu").animate({left:-300}, 500, function(){
					$(this).removeClass('opened').addClass('closed');
					$("a#toggleLink").removeClass('toggleBtnHighlight').addClass('toggleBtn');
				});
			});//bind close
	});//toggleBtn click close
 
	$("a.anchorLink").click(function () {	
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 );
		return false;
	})
 
	/* Mouse Enter Event (that is mouseover event)
	$(".toggleBtn").bind("mouseenter",function(e){
                        e.preventDefault(); // prevent the anchor tag from sending the user off to the link that is href
			if($("#slideMenu").hasClass('closed')){
				$("#slideMenu").animate({left:0}, 500, function(){
					$(this).removeClass('closed').addClass('opened');
					$("a#toggleLink").removeClass('toggleBtn').addClass('toggleBtnHighlight');
				});
			}//if close
 
			$('#slideMenu').bind("mouseleave",function(){
				$("#slideMenu").animate({left:-300}, 500, function(){
					$(this).removeClass('opened').addClass('closed');
					$("a#toggleLink").removeClass('toggleBtnHighlight').addClass('toggleBtn');
				});
			});//bind close
	});//toggleBtn click close*/
 
});//ready close

Explanation of the Code:

1
2
3
4
5
6
7
8
$(document).ready(function(){
	$(".toggleBtn").click(function(){
			if($("#slideMenu").hasClass('closed')){
				$("#slideMenu").animate({left:0}, 500, function(){
					$(this).removeClass('closed').addClass('opened');
					$("a#toggleLink").removeClass('toggleBtn').addClass('toggleBtnHighlight');
				});
			}//if close

Here is the brief description about the code which I have written, in the above snippet if you look at the very first line, it depicts that when the document is ready call a function, here I have written function directly. From point 2, actual concept starts that is here in point 2, on click of “toggleBtn” I am checking for the class whether “slideMenu” div has “closed” class (or) not, if yes! it will animate to left and after that I am removing class of “closed” and adding a new class named “opened” accordingly. Similarly, I am removing and adding respective classes to “toggleLink” id. I am continuing the process Vice Versa… The below code depicts the same.

1
2
3
4
5
6
$('#slideMenu').bind("mouseleave",function(){
				$("#slideMenu").animate({left:-300}, 500, function(){
					$(this).removeClass('opened').addClass('closed');
					$("a#toggleLink").removeClass('toggleBtnHighlight').addClass('toggleBtn');
				});
			});//bind close

Below code is for anchoring, when you click on “Anchor Link” provided in the menu, it will scroll to the respective anchor link by sliding the page content accordingly very smoothly.

1
2
3
4
5
6
$("a.anchorLink").click(function () {	
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 );
		return false;
	})

Download and Live Preview:

You can download the source code of the same here – download

Here is the preview of the same – Live Preview

Feedback:

Need your valuable feedback on this article, please do post your comments below for this article.

Related Entries...

You Might Like:

Advertisement

Please let us know your feedback about the article through the comments section below, we want to hear from you about our website. And your comment / advice may help us to serve you better in upcoming articles.

Thank You!
Vivekanand [Founder of DeveloperSnippets]

37 Responses

[...] This post was Twitted by devblogzone [...]

04.25.09

Works in Opera, Chrome, FF and Netscape, but not in IE 6. In IE 7, the script works, but when you click at the arrow to open the menu, the page is scrolled to top. Any way to fix that? :D Thx! Nice job :)

04.25.09

Hi Luk,

Thanks for your comment, will check and will let you know ASAP.

Thanks,
Vivek

04.25.09

Hi Vivek,

Nice Job, Great work…… :)

For Luk question, I would like to share one solution.

Please use ” javascript:void(0) ” instead of ” # ” link for the toggle button.

Keep posting nice Article.

-Abhi

04.25.09

Hey Abhi,

Thanks for your Valuable comment, and thanks for your appreciation – this will help me a lot to post some new things….. :)

Thanks,
Vivek

04.25.09

Hi Vivek,

A good simple slide menu.

But, I seem to be having problems in FF3 as well as IE7. The first one or two clicks for opening and closing the SlideMenu seems to be ok, after that, it does not seem to open even if I click a couple of times on the button.

Also, getting a strange behavior sometimes, like the SlideMenu opens up and closes immediately, no matter where the mouse is. Not sure if anyone else is getting this behavior.

Thanks and Good work.

Best,
Andy

04.25.09

Now it works in IE 7, but still not working in IE 6 :)

04.25.09

Hi Luk,

Thanks for your comment on this article, actually I am not having IE6 in my system to test, will check the same… and will let you know on this one, hope this might be ok with you…

Thanks,
Vivek

04.25.09

Hi Luk,

Here is the solution for your query, while onclick of the arrow – we need to prevent the respective href from going the user off the link, that is when we click on the anchor link, it is scrolling down, and when again when we click on arrow, the respective href url that ‘#’ is going off – if you look at address bar you will get an idea.

Below is the solution, just prevent it by adding the below line:
event.preventDefault();
——————————-
$(document).ready(function(){
$(“.toggleBtn”).click(function(e){
e.preventDefault();
if($(“#slideMenu”).hasClass(‘closed’)){
$(“#slideMenu”).animate({left:0}, 500, function(){
$(this).removeClass(‘closed’).addClass(‘opened’);
$(“a#toggleLink”).removeClass(‘toggleBtn’).addClass(‘toggleBtnHighlight’);
});
}//if close

$(‘#slideMenu’).bind(“mouseleave”,function(){
$(“#slideMenu”).animate({left:-300}, 500, function(){
$(this).removeClass(‘opened’).addClass(‘closed’);
$(“a#toggleLink”).removeClass(‘toggleBtnHighlight’).addClass(‘toggleBtn’);
});
});//bind close
});//toggleBtn click close

—————————
Please check even, I have updated the article code as well, I could not update the example page, as my FTP is down, will update once it is up… :)

Please check and let me know your feedback.

Thanks,
Vivek

Floating SlideIn Menu with Anchor Link using jQuery…

Thank you for submitting this cool story – Trackback from DotNetShoutout…

04.25.09

使用jquery实现一个浮动的带有Anchor Link的SlideIn Menu…

9efish.感谢你的文章 – Trackback from 9eFish…

[...] jQuery – Floating SlideIn Menu with Anchor Link using jQuery – Vivekanand (Suggested by Elijah Manor) [...]

04.25.09

Still not working… Take a look at this print: http://img35.imageshack.us/img35/9097/ie6bug.jpg

Any idea? :/

04.25.09

Hi Luk,

Actually, I thought you are getting “#” in the address bar when you click on the respective anchor link. so, I have given a solution for that. I am sorry for disappointing you, I am not having IE for test this application. I will try my best to give you a solution.

And I would like to thank you for sending a screen shot of the issue… Cheers!

Thanks,
Vivek

[...] Floating SlideIn Menu with Anchor Link using jQuery [...]

[...] Floating SlideIn Menu with Anchor Link using jQuery [...]

04.25.09

Hey, thanks for that ;)

just a small correction, u shouldn’t have $(‘#slideMenu’).bind(“mouseleave” inside $(“.toggleBtn”).click, but outside..
inside is buggy

04.25.09

Hi Johnny,

Thanks a lot for this…

Thanks,
Vivek

04.25.09

Hi,

Great script..
how to floating on the right page?

Please advice..

Thank you,

04.25.09

For floating right we need to change the CSS and jQuery code accordingly. Need to work on it…. Once I get time – I will work on it and will let you know….

Thanks,
Vivek

04.25.09

Cool. I used it in one of the software I made. Thank you a lot.

04.25.09

I am happy to hear that it has helped you :)

Thanks,
Vivek

拜读了!

[...] jQuery Floating Slide-In Menu with Anchor Link [...]

[...] jQuery Floating Slide-In Menu with Anchor Link [...]

04.25.09

Hi

I was seeing your code and has been very useful.

On IE6 the ‘fixed’ attribute for position is not supported. (line 12)
#slideMenu{
position:fixed;

Changing it to ‘absolute’ will solve the problem for I6. But unfortunadely the menu will not move as you scroll on all browsers.

Maybe I am asking to much, but I was wondering if it would be possible to add to your javascript code a conditional that if IE6 is used the sytle css should use absolute positioning instead of fixed?

Anyway thanks for the code

04.25.09

Thanks for your cool code.
One point I would like to change .. but can’t find the solution, hope you can help.

Your code with opening the slider winds up the page so the top of the page is displayed.
For my case I would like to have no change to the current positioning of the main page, get the slider in for further actions on it.

How to change the code for that?
Thanks
Günter

04.25.09

Hi Gunter,

First of all, I would like to thank you for your valuable comment which you have posted – regarding your query, I will just have a look at it and will let you know. Hope this might be ok with you.

Thanks,
Vivek

04.25.09

Yes, thanks for the direct response … it’s OK with me … and will be much better to see your solution. ;-) )
HAND
Günter

04.25.09

OK, the problem seems to be the href=”#” with the a link:
The quick&dirty is to omit it with

but for a clean solution I think the a link has to be changed to something else.
Thoughts?
Günter

04.25.09

changing the a link with id=”toggleLink” to a ‘div’ with all the other files also, it seems to work. Click/hover on the ‘slider’ works without going to the top of page.

Change to $(“.toggleBtn”).hover(function(){ works also.

Thanks

04.25.09

Is this available for a right hand side menu yet?

Many Thanks

04.25.09

Your demo is faulty…i’ll see if your code is realy corrected :D … Great script anyway.. thx allot :)

Nice work Vivek, you were 4th on Google search for the keyword “floating div using jquery”

04.25.09

Being kept in the 21st century as we are now, there is hardly a person who has never watched free streaming movies online in one point or the other in his life. YouTube immediately comes to mind whenever anyone mentions free streaming movies online. The red-colored seek bar in the bottom of YouTube videos is remarkably familiar to almost everyone. But here is something that will surprise you – streaming movies is not truly that modern as it have been around for nearly a century!

[...] Mais Ajax artigos Tagged with: adicionar • Como • esta • importante • jQuery • método [...]

[...] Nós da Equipe venceu em Truveo Concurso Developer Challenge – TechVideoBytes   Escrito por devblogzone Eu sou um Blogger, assim como um desenvolvedor de JavaScript bom. Eu moro em Hyderabad, Índia. [...]

Leave Your Response

* Name, Email, Comment are Required

Write For Us

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor


Switch to our mobile site