4
\$\begingroup\$

The following responsive navigation on JSfiddle works well, however I am just wondering whether there is any way to improve what I have done. I am new at JQuery so all help appreciated: JSFiddle here

$(document).ready(function () {
 $(".menu").click(function () {
 $('#menu').animate({
 'left': '0px'
 });
 });
 $("#close").click(function () {
 $('#menu').animate({
 'left': '-100px'
 });
 });
 $(".menu").click(function () {
 $('#container').animate({
 'left': '100px'
 });
 });
 $("#close").click(function () {
 $('#container').animate({
 'left': '0px'
 });
 });
});
Quill
12k5 gold badges41 silver badges93 bronze badges
asked Nov 25, 2014 at 16:03
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Instead of attaching multiple click handlers for the same element, you should just combine them into one function:

$(document).ready(function () {
 $(".menu").click(function () {
 $('#menu').animate({
 'left': '0px'
 });
 $('#container').animate({
 'left': '100px'
 });
 });
 $("#close").click(function () {
 $('#menu').animate({
 'left': '-100px'
 });
 $('#container').animate({
 'left': '0px'
 });
 });
});

Other than that, i don't see anything to optimize.

answered Nov 25, 2014 at 18:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.