简体   繁体   English

遍历列表中的每个图像并使用jQuery定义悬停状态

[英]Iterating over each image in a list and define a hover state using jQuery

I'm trying to do something very simple: make a border-bottom under an image in a slider when the user hovers over it. 我正在尝试做一些非常简单的事情:当用户将鼠标悬停在滑块上时,在滑块的图像下方创建边框底部。 I'm writing a jQuery function for this because I want to make something hover whose element is outside of a selector I'm selecting with jQuery. 我正在为此编写一个jQuery函数,因为我想做一些悬停的事情,其元素在我正在使用jQuery选择的选择器之外。 My code is as follows: 我的代码如下:

HTML: HTML:

<div class="more-projects-gallery">
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
    <figure class="more-projects-gallery-img-container">
        <a href=""><img src="holder.js/120x120" alt="img"></a>
        <span></span>
    </figure>
</div>

CSS (or SCSS): CSS(或SCSS):

.more-projects-gallery-img-container {
    a {
        cursor: default;
    }
    img {
        cursor: pointer;
    }
    span {
        display: none;
        @include size(120px 5px);
        background-color: $light-blue;
        @include position(absolute, null null -14px 42px);
    }
}

The jQuery: jQuery:

$(function galleryImageHover() {

    var $galleryImageContainer = $('.more-projects-gallery-img-container');
    var $galleryImage = $('.more-projects-gallery-img-container a');
    var $galleryImageSpan = $('.more-projects-gallery-img-container span');

    $galleryImageContainer.each(function(){
        $galleryImage.on("mouseover", function(){
            $galleryImageSpan.fadeIn(300).show();
        });
    });
    $galleryImageContainer.each(function(){
        $galleryImage.on("mouseout", function(){
            $galleryImageSpan.fadeOut(300).hidden();
        });
    });

});

The problem I'm having is that when you hover over any image, the hover state is activated for ALL the images in the slider instead of just the one image the user is currently moused over in. Any help would be greatly appreciated. 我遇到的问题是,当您将鼠标悬停在任何图像上时,将为滑块中的所有图像而不是仅将用户当前鼠标悬停在其中的一个图像激活悬停状态。我们将不胜感激。 I've been at this for a while and just can't seem to get it to work. 我已经有一段时间了,似乎无法正常工作。

You are applying the fadeIn and fadeOut to all the span s in the .more-projects-gallery-img-container , hence hover state is activated for ALL. 您将对.more .more-projects-gallery-img-container中的所有span都应用fadeIn和fadeOut,因此将ALL的悬停状态激活。

Try this (based on the HTML you have provided): 试试看(基于您提供的HTML):

$(function galleryImageHover() {

    var $galleryImageContainer = $('.more-projects-gallery-img-container');
    var $galleryImage = $('.more-projects-gallery-img-container a');
    //var $galleryImageSpan = $('.more-projects-gallery-img-container span'); //You won't need this.

    $galleryImageContainer.each(function(){
        $galleryImage.on("mouseover", function(){
            $(this).next().fadeIn(300).show(); // changed the selector here to select the next span rather than all the spans
        });
    });
    $galleryImageContainer.each(function(){
        $galleryImage.on("mouseout", function(){
            $(this).next().fadeOut(300).hidden(); // changed the selector here to select the next span rather than all the spans
        });
    });

});

You can chain the methods in jQuery, So chain your mouseout and mouover event listeners. 您可以在jQuery中链接方法,因此可以链接mouseout和mouover事件侦听器。 Then You need to loop through the conteiner instead image so that you can target both image and span inside the loop. 然后,您需要遍历约束器而不是图像,以便可以同时定位图像和跨循环。

 $(function galleryImageHover() {

        var $galleryImageContainer = $('.more-projects-gallery-img-container');

        $galleryImageContainer .each(function(){
            $(this).find("a").on("mouseover", function(){
                $(this).find('span').fadeIn(300).show();
            }).on("mouseout", function(){
                $(this).find('span').fadeOut(300).hidden();
            });
        });

    });

I would say, that you should do something like this: 我要说的是,您应该执行以下操作:

$galleryImageContainer.on("mouseover", 'a', function(){
   $(this).parent().children('span').fadeIn(300).show();
});

The reason that jQuery selects all of the elements is, because you tell it to. jQuery选择所有元素的原因是,因为您告诉了它。 In my example only the child with the event is activated. 在我的示例中,只有具有事件的孩子才被激活。

Hope i could help you. 希望我能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM