Александр М.
50 сообщений
#15 лет назад
Ребят, помогите, пожалуйста, укоротить этот код.
Что он делает:
Есть 4 картинки. При наведении мыши на одну из них, остальные исчезают. При отведении мыши, появляются.


$(document).ready(function(){
$("#splsh_fem").hover(function(){
$("#splsh_man").stop(true, true).fadeOut();
$("#splsh_vin").stop(true, true).fadeOut();
$("#splsh_int").stop(true, true).fadeOut();
},
function(){
$("#splsh_man").stop(true, true).fadeIn();
$("#splsh_vin").stop(true, true).fadeIn();
$("#splsh_int").stop(true, true).fadeIn();
});

$("#splsh_man").hover(function(){
$("#splsh_fem").stop(true, true).fadeOut();
$("#splsh_vin").stop(true, true).fadeOut();
$("#splsh_int").stop(true, true).fadeOut();
},
function(){
$("#splsh_fem").stop(true, true).fadeIn();
$("#splsh_vin").stop(true, true).fadeIn();
$("#splsh_int").stop(true, true).fadeIn();
});

$("#splsh_vin").hover(function(){
$("#splsh_fem").stop(true, true).fadeOut();
$("#splsh_man").stop(true, true).fadeOut();
$("#splsh_int").stop(true, true).fadeOut();
},
function(){
$("#splsh_fem").stop(true, true).fadeIn();
$("#splsh_man").stop(true, true).fadeIn();
$("#splsh_int").stop(true, true).fadeIn();
});

$("#splsh_int").hover(function(){
$("#splsh_fem").stop(true, true).fadeOut();
$("#splsh_man").stop(true, true).fadeOut();
$("#splsh_vin").stop(true, true).fadeOut();
},
function(){
$("#splsh_fem").stop(true, true).fadeIn();
$("#splsh_man").stop(true, true).fadeIn();
$("#splsh_vin").stop(true, true).fadeIn();
});
});
Алексей С.
115 сообщений
#15 лет назад
Немного фантазии, а лучше доверять JS профессионалам.

var blocks = $('#block img');

blocks
.hover(function(){
blocks.stop().fadeOut();
$(this).stop().fadeIn();
}, function(){
blocks.stop().fadeIn();
});


<div id="block">
<img ... />
<img ... />
<img ... />
<img ... />
</div>
Александр М.
50 сообщений
#15 лет назад
Этот код не будет работать, к сожалению.

Специально для JS профессионалов объясняю:
у нас 4 блока: 1, 2, 3, 4 Должно быть соответственно схематично:
1.hover{
2.hide
3.hide
4.hide
}

2.hover{
1.hide
3.hide
4.hide
}

3.hover{
1.hide
2.hide
4.hide
}

4.hover{
1.hide
2.hide
3.hide
}


а ваш код делает следующее (схематично)
1.hover{
1.hide
2.hide
3.hide
4.hide
}


и после этого fadeIn() даже больше не срабатывает.
Алексей С.
115 сообщений
#15 лет назад

var blocks = $('#block img');

blocks
.hover(function(){
$(this).prevAll().stop().fadeOut();
$(this).nextAll().fadeOut();
}, function(){
blocks.fadeIn();
});

<div id="block">
<img src="userpic.gif" alt="" width="100" height="100" />
<img src="userpic.gif" alt="" width="100" height="100" />
<img src="userpic.gif" alt="" width="100" height="100" />
<img src="userpic.gif" alt="" width="100" height="100" />
</div>
Евгений Б.
5330 сообщений
#15 лет назад
Записал в записнушку пригодится
Алексей С.
115 сообщений
#15 лет назад
Есть ещё другие варианты, все зависит от расположения картинок в DOM.