Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make this jQuery more succinct?
    primarykey
    data
    text
    <p>I'm fairly new to jQuery and would like some pointers on my following block of jQuery code, I'm interested in writing better code. </p> <p>Basically, the code just slides content in from the left when you click on one of two menu items and then on a further click slides it out. I guess I'm trying to combine it into one code block rather than two functions.</p> <pre><code>$('#menu-item-91').click(function(e) { e.preventDefault(); //console.log("Clicked"); //$('.about').css('display','inherit').slideDown(3000); var sliding = $('.sliding'); var sliding1 = $('.sliding1'); if( sliding.css('display') == "none" &amp;&amp; sliding1.css('display') == "none" ) { //show it sliding.fadeIn(0); sliding.animate({"left": 0},800); }else if ( sliding.css('display') == "none" &amp;&amp; sliding1.css('display') == "block" ) { sliding1.animate({"left": -300},500 ); sliding1.delay(500).fadeOut(0); sliding.fadeIn(0); sliding.animate({"left": 0},800); } else { sliding.animate({"left": -300},500 ); sliding.delay(500).fadeOut(0); } }); $('#menu-item-17').click(function(e) { e.preventDefault(); //console.log("Clicked"); //$('.about').css('display','inherit').slideDown(3000); var sliding = $('.sliding1'); var sliding1 = $('.sliding'); if( sliding.css('display') == "none" &amp;&amp; sliding1.css('display') == "none" ) { //show it sliding.fadeIn(0); sliding.animate({"left": 0},800); }else if ( sliding.css('display') == "none" &amp;&amp; sliding1.css('display') == "block" ) { sliding1.animate({"left": -300},500 ); sliding1.delay(500).fadeOut(0); sliding.fadeIn(0); sliding.animate({"left": 0},800); } else { sliding.animate({"left": -300},500 ); sliding.delay(500).fadeOut(0); } }); </code></pre> <p>That's it! This is my first post so I hope I haven't broke any rules and someone out there can help me be better!</p> <p>Regards Tom</p> <p><em><strong>EDIT</em></strong></p> <p>Thanks Guys! Didn't want to mark an answer just yet as I think I probably missed out a bit of detail that's relevant for some of the answers.</p> <p>The two classes .sliding and .sliding1 are added to separate divs - one called portfolio and one called about. </p> <p>So the idea is that if you click the about menu item the about div slides in, then if you were to click the portfolio menu item then the about would slide out and portfolio would slide in. But if no items were displayed then the item you clicked would slide in.</p> <p>Hope this clarifies things, some of the answers just slide in the same content when either menu item was clicked.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload