这不是一个直接的问题,而是一个关于如何通过压缩我的代码并以更合乎逻辑的方式编写来改进我的 javascript 的问题。
基本上,我只是使用 jQuery 编写了一些 javascript,以便在我的网站上创建一些投资组合动画。您可以在此处查看作品集和动画:
http://www.saelstrom.com/portfolio/full-portfolio
我想完成的一切似乎都奏效了。但是我只是觉得它可以用更少的代码编写。我可能是错的,但我想我会把问题放在那里,以防万一有任何专家愿意在我下次编写一些 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
}
还不错。我会从那里开始。您的评论占用了很多空间,这可能会给人一种冗长的错觉:)
关于javascript - 如何改进我的 Javascript/jQuery 逻辑?示例包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631409/