如何改进Javascript/jQuery逻辑?例子包括

如何改进Javascript/jQuery逻辑?例子包括,javascript,jquery,html,css,function,Javascript,Jquery,Html,Css,Function,这不是一个直接的问题,而是一个如何通过压缩代码和以更合理的方式编写来改进javascript的问题 基本上,我只是使用jQuery编写了一些javascript,以便在我的网站上创建一些公文包动画。您可以在此处查看公文包和动画: 我想完成的每件事似乎都奏效了。然而,我有一种感觉,它可以用更少的代码编写。我可能错了,但我想我应该把这个问题提出来,以防下次我写javascript时,有专家会给我指出正确的方向 以下是我的脚本: $(document).ready(function() { //

这不是一个直接的问题,而是一个如何通过压缩代码和以更合理的方式编写来改进javascript的问题

基本上,我只是使用jQuery编写了一些javascript,以便在我的网站上创建一些公文包动画。您可以在此处查看公文包和动画:

我想完成的每件事似乎都奏效了。然而,我有一种感觉,它可以用更少的代码编写。我可能错了,但我想我应该把这个问题提出来,以防下次我写javascript时,有专家会给我指出正确的方向

以下是我的脚本:

$(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');
        });
    }

});

});

首先,您可以使用更多链接:

// ------------------------------------------------------
// - Portfolio Item Collapse
// ------------------------------------------------------
$('.folio-item').click(function(){
    collapse = $(this).find('.folio-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').fadeIn('fast');
},
function(){
    hoveritem = $(this).find('.hover-item').fadeOut('fast');
});

$('.folio-screen').hover(function(){
    hoveritem = $(this).find('.launch').fadeIn('fast');
},
function(){
    hoveritem = $(this).find('.launch').fadeOut('fast');
});
您可以删除冗余代码:

/* ALL THIS CODE CAN GO AWAY */

if($('.marketing-wrapper').is(':visible')){
    // Do Nothing
}

if($('.web-wrapper').is(':visible')){
    // Do Nothing
}

if($('.branding-wrapper').is(':visible')){
    // Do Nothing
}

if($('.landing-wrapper').is(':visible')){
    // Do Nothing
}

还不错。我从那里开始。您的评论占用了大量空间,可能会产生冗长的错觉:)

谢谢!我感谢你的帮助:)