Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I improve my Javascript/jQuery logic? Example included
    primarykey
    data
    text
    <p>This isn't a direct question, but more a question on how I can improve my javascript by condensing my code and writing in a more logical way. </p> <p>Basically I just wrote some javascript, using jQuery, in order to create some portfolio animations on my website. You can view the portfolio and animations here: </p> <p><a href="http://www.saelstrom.com/portfolio/full-portfolio" rel="nofollow">http://www.saelstrom.com/portfolio/full-portfolio</a></p> <p>Everything I wanted to accomplish seems to work. However I just have this feeling that it could have been written with a lot less code. I could be wrong, but I thought I'd put the question out there just in case there's any experts who'd like to point me in the right direction for the next time I write some javascript.</p> <p>Here's my scripts:</p> <pre><code>$(document).ready(function() { // ------------------------------------------------------ // - Create jQuery Function - Fade Then Slide Toggle // ------------------------------------------------------ jQuery.fn.fadeThenSlideToggle = function(speed, callback) { if (this.is(":visible")) { return this.fadeTo(speed, 0).slideUp(speed, callback); } else { return this.slideDown(speed).fadeTo(speed, 1, callback); } }; // ------------------------------------------------------ // - Portfolio Item Collapse // ------------------------------------------------------ $('.folio-item').click(function(){ collapse = $(this).find('.folio-collapse'); collapse.slideToggle('slow',function(){ if($(this).is(':visible')){ $(this).parent().addClass('open'); } else { $(this).parent().removeClass('open'); } }); }); // ------------------------------------------------------ // - Portfolio Item Hover // ------------------------------------------------------ $('.folio-item').hover(function(){ hoveritem = $(this).find('.hover-item'); hoveritem.fadeIn('fast'); }, function(){ hoveritem = $(this).find('.hover-item'); hoveritem.fadeOut('fast'); }); $('.folio-screen').hover(function(){ hoveritem = $(this).find('.launch'); hoveritem.fadeIn('fast'); }, function(){ hoveritem = $(this).find('.launch'); hoveritem.fadeOut('fast'); }); // ------------------------------------------------------ // - Portfolio Show/Hide Section Wrappers // ------------------------------------------------------ // - Web Design - Click Function $('li.port-web').click(function(){ if($('.web-wrapper').is(':visible')){ // Do Nothing } else if($('.marketing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-marketing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-web').addClass('active'); // Hide Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.branding-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-branding').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-web').addClass('active'); // Hide Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow',function(){ // Show Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.landing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-landing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-web').addClass('active'); // Hide Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow'); }); } }); // - Internet Marketing - Click Function $('li.port-marketing').click(function(){ if($('.marketing-wrapper').is(':visible')){ // Do Nothing } else if($('.web-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-web').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-marketing').addClass('active'); // Hide Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow',function(){ // Show Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.branding-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-branding').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-marketing').addClass('active'); // Hide Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow',function(){ // Show Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.landing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-landing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-marketing').addClass('active'); // Hide Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow'); }); } }); // - Branding - Click Function $('li.port-branding').click(function(){ if($('.branding-wrapper').is(':visible')){ // Do Nothing } else if($('.web-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-web').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-branding').addClass('active'); // Hide Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow',function(){ // Show Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.landing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-landing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-branding').addClass('active'); // Hide Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.marketing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-marketing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-branding').addClass('active'); // Hide Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow'); }); } }); // - Landing Pages - Click Function $('li.port-landing').click(function(){ if($('.landing-wrapper').is(':visible')){ // Do Nothing } else if($('.web-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-web').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-landing').addClass('active'); // Hide Web Wrapper $('.web-wrapper').fadeThenSlideToggle('slow',function(){ // Show Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.branding-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-branding').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-landing').addClass('active'); // Hide Branding Wrapper $('.branding-wrapper').fadeThenSlideToggle('slow',function(){ // Show Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow'); }); } else if($('.marketing-wrapper').is(':visible')){ // Remove 'active' Class $(this).parent().find('li.port-marketing').removeClass('active'); // Add Class 'active' $(this).parent().find('li.port-landing').addClass('active'); // Hide Marketing Wrapper $('.marketing-wrapper').fadeThenSlideToggle('slow',function(){ // Show Landing Wrapper $('.landing-wrapper').fadeThenSlideToggle('slow'); }); } }); }); </code></pre>
    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.
    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