How do I shorten this jQuery?

I am very new to jQuery, so please be careful.

I found two articles on the Internet:

I used both of these examples and was able to get them to work together:

http://www.marccohen.co.uk/dev/menu_example.htm

Fading convolutions in one list also activate animated freezes in another list, and vice versa. The problem is that as a result of jQuery I have a “very” long and so new for this, I have no idea how I can shorten it:

$(function() {
$(".menuicon1")
.find("span")
.hide()
.end()
.hover(function() {
    $(this).find("span").stop(true, true).fadeIn({duration:300});
    $('ul.hover_block li.slide1').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
    $(this).find("span").stop(true, true).fadeOut({duration:300});
    $('ul.hover_block li.slide1').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon2")
.find("span")
.hide()
.end()
.hover(function() {
    $(this).find("span").stop(true, true).fadeIn({duration:300});
    $('ul.hover_block li.slide2').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
    $(this).find("span").stop(true, true).fadeOut({duration:300});
    $('ul.hover_block li.slide2').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon3")
.find("span")
.hide()
.end()
.hover(function() {
    $(this).find("span").stop(true, true).fadeIn({duration:300});
    $('ul.hover_block li.slide3').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
    $(this).find("span").stop(true, true).fadeOut({duration:300});
    $('ul.hover_block li.slide3').find('img').animate({top:'276px'},{queue:false,duration:150});
}); 
$(".menuicon4")
.find("span")
.hide()
.end()
.hover(function() {
    $(this).find("span").stop(true, true).fadeIn({duration:300});
    $('ul.hover_block li.slide4').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
    $(this).find("span").stop(true, true).fadeOut({duration:300});
    $('ul.hover_block li.slide4').find('img').animate({top:'276px'},{queue:false,duration:150});
}); 
$(".menuicon5")
.find("span")
.hide()
.end()
.hover(function() {
    $(this).find("span").stop(true, true).fadeIn({duration:300});
    $('ul.hover_block li.slide5').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
    $(this).find("span").stop(true, true).fadeOut({duration:300});
    $('ul.hover_block li.slide5').find('img').animate({top:'276px'},{queue:false,duration:150});
}); 
    $('ul.hover_block li.slide1').hover(function(){
    $(this).find('img').animate({top:'192px'},{queue:false,duration:150});
    $('.menuicon1').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
    $(this).find('img').animate({top:'276px'},{queue:false,duration:150});
    $('.menuicon1').find("span").stop(true, true).fadeOut({duration:300});
});
    $('ul.hover_block li.slide2').hover(function(){
    $(this).find('img').animate({top:'192px'},{queue:false,duration:150});
    $('.menuicon2').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
    $(this).find('img').animate({top:'276px'},{queue:false,duration:150});
    $('.menuicon2').find("span").stop(true, true).fadeOut({duration:300});
});         
    $('ul.hover_block li.slide3').hover(function(){
    $(this).find('img').animate({top:'192px'},{queue:false,duration:150});
    $('.menuicon3').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
    $(this).find('img').animate({top:'276px'},{queue:false,duration:150});
    $('.menuicon3').find("span").stop(true, true).fadeOut({duration:300});
});             
    $('ul.hover_block li.slide4').hover(function(){
    $(this).find('img').animate({top:'192px'},{queue:false,duration:150});
    $('.menuicon4').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
    $(this).find('img').animate({top:'276px'},{queue:false,duration:150});
    $('.menuicon4').find("span").stop(true, true).fadeOut({duration:300});
});             
    $('ul.hover_block li.slide5').hover(function(){
    $(this).find('img').animate({top:'192px'},{queue:false,duration:150});
    $('.menuicon5').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
    $(this).find('img').animate({top:'276px'},{queue:false,duration:150});
    $('.menuicon5').find("span").stop(true, true).fadeOut({duration:300});
});                                 
});

Really appreciate any suggestions ...

+3
1

@Dave-Newton , , , , , .

( ) .

$(".menuicon1")
  .find("span")
  .hide()
  .end()
  .hover(function() {
    $(this)
      .find("span")
      .stop(true, true)
      .fadeIn({ duration: 300 });
    $('ul.hover_block li.slide1')
      .find('img')
      .animate({ top: '192px' },{ queue:false, duration: 150 });
  }, function() {
    $(this)
      .find("span")
      .stop(true, true)
      .fadeOut({ duration: 300 });
    $('ul.hover_block li.slide1')
      .find('img')
      .animate({ top: '276px' },{ queue: false, duration: 150 });
  });

-, css- . , , :

function createBlockHover(blockNumber) {
  $(".menuicon" + blockNumber)
    .find("span")
    .hide()
    .end()
    .hover(function() {
      $(this)
        .find("span")
        .stop(true, true)
        .fadeIn({ duration: 300 });
      $('ul.hover_block li.slide' + blockNumber)
        .find('img')
        .animate({ top: '192px' },{ queue:false, duration: 150 });
    }, function() {
      $(this)
        .find("span")
        .stop(true, true)
        .fadeOut({ duration: 300 });
      $('ul.hover_block li.slide' + blockNumber)
        .find('img')
        .animate({ top: '276px' },{ queue: false, duration: 150 });
    });
}

, . (In/Out) :

function fadeBlocks(blockNumber, fadeIn, top) {
  // fade the first block
  $('.menuicon' + blockNumber)
    .find("span")
    .stop(true, true)
    [fadeIn ? 'fadeIn' : 'fadeOut']({ 
      duration: 300 
    });

  // move the second
  $('ul.hover_block li.slide' + blockNumber)
    .find('img')
    .animate({ top: top + 'px' }, { queue: false, duration: 150 })
}

function createBlockHover(blockNumber) {
  $(".menuicon" + blockNumber)
    .find("span")
    .hide()
    .end()
    .hover(function() {
      fadeBlocks(blockNumber, true, 192);
    }, function() {
      fadeBlocks(blockNumber, false, 276);
    });
}

, , , .

See the DRY principle for more information .

0
source

All Articles