I am working on a website where clicking on a certain link will slide down a login panel. I am using event.preventDefault()
to stop the site from redirecting as well as an animation event to slide the panel down. When the link is clicked, the panel slides down and the url remains unchanged.
What I want to happen when the link is clicked is for the panel to animate as normal, but for the href attribute of the link to be shown in the url. In this case it would be something like this: http://domain_name/#login
.
Here is the code I've got going right now:
$("#login_link").click(function (e) {
e.preventDefault();
$("#login").animate({ 'margin-top': 0 }, 600, 'linear');
window.location.hash = $(this).attr('href');
});
This code successfully adds the '#login' to the url as desired, but it skips the animation of the login panel. When clicking the link the panel just appears instantly. I would like to keep both the animation and the updated url behaviors. Is this possible?
preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form. Prevent a link from following the URL.
preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
The easiest way to do this is to do $("#new_form")[0]. submit() , it will bypass any jQuery bound handlers and submit the form.
use below code . just call the hash in animation completed event.
$("#login_link").click(function (e) {
e.preventDefault();
$("#login").animate({ 'margin-top': 0 }, 600, 'linear', function(){ window.location.hash = $(this).attr('href'); });
});
I am not sure but you may bee looking for this feature
1> when some body clicks login, url changes to abc.com/#login, and login panel animation occurs
or
2 > when some body just types abc.com/#login in url, you expect the same behavior (animation of login panel).
What you are doing currently will work only for case1 and wont work for case2. If you want animation (or whatever) to work both, when clicked , or just url typed. You need some mechanism to detect hash changes.
There is a famous plugin http://benalman.com/projects/jquery-bbq-plugin/
Go and see this plugin and, look how things are done. If you find anything tough to understand we are here!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With