JQuery selectors in the <li> list

Can someone advise me with jQuery selectors.

I have an HTML list (see below) with four flag images in it. When the user flips a flag, for example, italy, I want him to reduce all the other three flags to 50% opacity.

<ul id="flags">
    <li id="German"><img src="images/flag_german.jpg" /></li>
    <li id="Italian"><img src="images/flag_italian.jpg" /></li>
    <li id="Spanish"><img src="images/flag_spanish.jpg" /></li>
    <li id="French"><img src="images/flag_french.jpg" /></li>
</ul>
+3
source share
4 answers

Try the following:

$(function() {
    $('#flags li').hover(function() {
        $(this).siblings().stop(true).animate({opacity:'0.5'},1000);
    }, function() {
        $(this).siblings().stop(true).animate({opacity:'1'},1000);
    });
});

By using stop, you help prevent a state in which you rolled many elements and they animated the animation in the queue. It clears all current animations in the queue for the item and assigns a new one.

, , fadeIn fadeOut, fadeIn/fadeOut hover. , .

+11

( )

   $("#flags li").mouseover(function(){
      var id = this.id;
      $("#flags li").not("#"+id).animate({opacity:'0.5'},1000);
      $(this).animate({opacity:'1'},1000);
    });
+5
$('#flags li').bind('mouseover', function() {
    $('#flags li').not($(this)).fadeTo('fast', 0.5);
}).bind('mouseout', function() {
    $('#flags li').show();
});
+1
source

You might want to study the siblings selector .

0
source

Source: https://habr.com/ru/post/1716919/


All Articles