How to check items in Hover?

Asked

Viewed 162 times

1

I have a list of elements belonging to a gallery, where the elements outside the Hover will have a new class. The problem is when Hover occurs directly from one element to another and the class is not added to the new element in Hover.

$(".carousel-gallery-item").hover(

function () {
    $(".carousel-gallery-item").each(function (index, el) {
        var isHovered = $(el).is(":hover");
        if (!isHovered) {
            $(el).addClass("carousel-item-hover");
        }
    });
},

function () {
    $(".carousel-gallery-item").each(function (index, el) {
        var isHovered = $(el).is(":hover");
        if (!isHovered) {
            $(el).removeClass("carousel-item-hover");
        }
    });
});
.carousel-gallery-item {
    float: left;
    padding: 3px;
    -webkit-filter: grayscale(0);
    -webkit-filter: grayscale(0%);
    filter: grayscale(0%);
    filter: url("#greyscale");
    filter: #808080;
    opacity: 1;
    -webkit-transition: all 0.2s ease;
    -moz-transition: all 0.2s ease;
    -ms-transition: all 0.2s ease;
    -o-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
.carousel-gallery-item img {
    vertical-align: middle;
}
.carousel-item-hover {
    -webkit-filter: grayscale(1);
    -webkit-filter: grayscale(100%);
    filter: grayscale(100%);
    filter: url("#greyscale");
    filter: #808080;
    opacity: 0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="carousel-content">
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="1">
        <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 1">
    </div>
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="2">
        <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 2">
    </div>
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="3">
        <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 3">
    </div>
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
        <filter id="greyscale">
            <feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0
			0.3333 0.3333 0.3333 0 0
			0.3333 0.3333 0.3333 0 0
			0      0      0      1 0"></feColorMatrix>
        </filter>
    </svg>
</div>

1 answer

2


I will propose an alternative solution only with css:

.carousel-gallery-item {
    float: left;
    padding: 3px;
    -webkit-filter: grayscale(0);
    -webkit-filter: grayscale(0%);
    filter: grayscale(0%);
    filter: url("#greyscale");
    filter: #808080;
    opacity: 1;
    -webkit-transition: all 0.2s ease;
    -moz-transition: all 0.2s ease;
    -ms-transition: all 0.2s ease;
    -o-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
.carousel-gallery-item img {
    vertical-align: middle;
}
.carousel-gallery:hover .carousel-gallery-item:not(:hover) {
    -webkit-filter: grayscale(1);
    -webkit-filter: grayscale(100%);
    filter: grayscale(100%);
    filter: url("#greyscale");
    filter: #808080;
    opacity: 0.5;
}
<div class="carousel-content">
  <div class="carousel-gallery">
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="1">
      <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 1">
    </div>
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="2">
      <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 2">
    </div>
    <div class="column-medium-2 column-small-4 column-extra-small-6 carousel-gallery-item" data-id="3">
      <img src="https://placeholdit.imgix.net/~text?txtsize=20&bg=b20000&txtclr=ffffff&txt=100%C3%97100&w=100&h=100" class="img-responsive" ondragstart="return false" alt="Foto 3">
    </div>
  </div>
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <filter id="greyscale">
      <feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0
                                           0.3333 0.3333 0.3333 0 0
                                           0.3333 0.3333 0.3333 0 0
                                           0      0      0      1 0"></feColorMatrix>
    </filter>
  </svg>
</div>

In the above example I created a wrapper for the images, in case the div.carousel-gallery, so I added a css to all the div.carousel-gallery-item who are not with hover when there is a hover on their wrapper.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.