简体   繁体   English

Jquery之后悬停效果不起作用

[英]hover effect not working after Jquery

I am currently making a website for an architecture firm, HLArchitects. 我目前正在为一家建筑公司HLArchitects建立一个网站。 In the projects page I have created an HTML / Javascript image gallery. 在项目页面中,我创建了一个HTML / Javascript图库。 When you hover over a smaller thumbnail the opacity changes from 0.5 to 1. It can be viewed here for reference: http://www.hla.co.za/projects/Hyuandai_Training_Centre/ 当您将鼠标悬停在较小的缩略图上时,不透明度会从0.5变为1.可在此处查看以供参考: http//www.hla.co.za/projects/Hyuandai_Training_Centre/

My problem is that after you click on one of the smaller thumbnails, which changes the bigger picture above it, and then try to hover over another smaller thumbnail, it no longer changes opacity. 我的问题是,在你点击其中一个较小的缩略图后,它会改变它上面的大图,然后尝试将鼠标悬停在另一个较小的缩略图上,它不再改变不透明度。 I used a simple CSS :hover to change the opacity along with transition: opacity 0.2s . 我使用了一个简单的CSS :hover以改变不透明度以及transition: opacity 0.2s Here is the Javascript / Jquery for the on click event of he thumbnail: 这是他缩略图的点击事件的Javascript / Jquery:

var imageFlow = document.getElementById("imageFlow");
var img = imageFlow.getElementsByTagName("img");

$("#imageFlow img").click(function(){
    var src = $(this).attr("src");
    if (src != $('#displayImg img').attr("src")){
        $('#displayImg img').fadeOut(200);
        setTimeout(function(){$("#displayImg img").attr("src",src);}, 200);
        $('#displayImg img').fadeIn(200);
    }

    for (var i = 0; i < img.length; i++) {
        $(img[i]).css("opacity","0.5");         
    }
    $(this).css("opacity","1");

})

HTML: HTML:

<div id="displayImg">
        <img src="images/095.jpg">
    </div>

    <div id="imageFlow">
        <img src="images/095.jpg" alt="095" width="" height="" />
        <img src="images/105.jpg" alt="105" width="" height="" />
        <img src="images/106.jpg" alt="106" width="" height="" />
        <img src="images/110.jpg" alt="110" width="" height="" />
        <img src="images/133.jpg" alt="133" width="" height="" />
        <img src="images/137.jpg" alt="137" width="" height="" />
        <img src="images/138.jpg" alt="138" width="" height="" />
        <img src="images/141.jpg" alt="141" width="" height="" />
        <img src="images/145.jpg" alt="145" width="" height="" />
        <img src="images/149.jpg" alt="149" width="" height="" />
        <img src="images/160.jpg" alt="160" width="" height="" />
        <img src="images/DSC_0077.jpg" alt="DSC_0077" width="" height="" />
        <img src="images/DSC_0091.jpg" alt="DSC_0091" width="" height="" />
        <img src="images/DSC_0092.jpg" alt="DSC_0092" width="" height="" />
        <img src="images/DSC_0093.jpg" alt="DSC_0093" width="" height="" />
        <img src="images/DSC_0252.jpg" alt="DSC_0252" width="" height="" />
        <img src="images/DSC_0357.jpg" alt="DSC_0357" width="" height="" />
        <img src="images/DSC_0360.jpg" alt="DSC_0360" width="" height="" />
        <img src="images/DSC_0380.jpg" alt="DSC_0380" width="" height="" />
    </div>

I would really appreciate a solution to this so that the hover effect works even after you click on one of the thumbnails. 我真的很感激这个解决方案,以便即使你点击其中一个缩略图后悬停效果也能正常工作。
Thank you in advance 先感谢您

var images = $("#imageFlow img");

images.on('click', function(){
    var src = $(this).attr("src");

    if (src != $('#displayImg img').attr("src")){
        $('#displayImg img').fadeOut(200, function() {
            $(this).attr("src", src).fadeIn(200);
        });
    }

    images.removeAttr('style');
    this.style.opacity = '1';
});

FIDDLE 小提琴

Also, your site has two opening body tags and strange invalid markup. 此外,您的网站有两个开放的正文标记和奇怪的无效标记。

CSS: CSS:

#imageFlow img:hover,
#imageFlow img.active {
    opacity: 1;
}

JS: JS:

var $img = $("#imageFlow").find("img");

$img.click(function(){
    var src = this.src;
    if (src != $('#displayImg img')[0].src){
        $(this).addClass('active').siblings('img').removeClass('active');
        $('#displayImg img').stop().fadeTo(200,0, function(){
             this.src = src;
             $(this).fadeTo(200, 1);
        });
    }
});

Take a good look at your HTML elements and fix unclosed tags, duplicate tags, duplicate ID elements etc... , and on other JS errors that pop out in console in your page. 仔细查看HTML元素并修复未关闭的标记,重复的标记,重复的ID元素等,以及页面中控制台中弹出的其他JS错误。 Otherwise you'll be running in loops ;) 否则你将在循环中运行;)

Initially when I run this in the console I get this error: 最初,当我在控制台中运行它时,我收到此错误:

TypeError: projs is null
var img = projs.getElementsByTagName('img');

It seems that your script is looking for something in the DOM that hasn't loaded yet. 您的脚本似乎正在寻找尚未加载的DOM中的内容。

Currently proj.js is looking for elements that haven't loaded yet. 目前proj.js正在寻找尚未加载的元素。

Your script should run at the bottom of your page like the imageFlow.js. 您的脚本应该在页面底部运行,如imageFlow.js。

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

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