简体   繁体   English

交换图片Src jQuery不起作用?

[英]Swap Image Src JQuery Not Working?

...but for some reason it won't work. ...但是由于某种原因,它将无法正常工作。 Beginner here, so if someone could explain what I'm doing wrong, I'd really appreciate it. 初学者在这里,所以如果有人可以解释我做错了什么,我将非常感激。 xD xD

http://codepen.io/DylanEspey/pen/xgwJr An example Codepen I threw together really quick to try to illustrate my problem. http://codepen.io/DylanEspey/pen/xgwJr一个示例Codepen我很快就拼凑起来,试图说明我的问题。

I hate that I've had to ask so many questions, but this is my first project where I've kind of had my own leeway, and I've been struggling to get it done before deadlines so I can impress people. 我讨厌不得不问很多问题,但这是我的第一个项目,我有自己的回旋余地,而且我一直在努力在截止日期之前完成工作,以便给人们留下深刻的印象。 xD That and college has made my schedule a mess, so I've been making really dumb mistakes. xD那和大学使我的日程变得一团糟,所以我一直犯着非常愚蠢的错误。 That, and I just started learning Javascript a few weeks ago... 这样,几周前我才开始学习Javascript ...

Try this: 尝试这个:

$('.thumbnails img').click(function () {
    var thmb = this;
    var src = this.src;
    $('.project-img img').fadeOut(400, function () {
        thmb.src = this.src;
        $(this).attr('src', src).fadeIn(400);
    });
});

Fiddle Demo 小提琴演示

The problem is that the overlay used for your fade effect is preventing you from clicking on the image which is underneath it. 问题在于,用于淡入淡出效果的叠加层会阻止您单击其下方的图像。 The solution is to, instead of listening for a click on the image, listen for it on the overlay, and adjust the references accordingly. 解决方案是,不要在图像上听一听,而在覆盖图上听它,然后相应地调整参考。

I have forked your codepen to reflect this: http://codepen.io/anon/pen/qlkob 我已将您的Codepen分叉以反映此情况: http ://codepen.io/anon/pen/qlkob

JS: JS:

$(document).ready(function() {
    $('.thumbnails .overlay').click(function() {
        var img = $(this).find('img')[0];
        var thmb = img;
        var src = img.src;
        $('.project-img img').fadeOut(400,function(){
            thmb.src = this.src;
            $(this).attr('src', src).fadeIn(400);
        });
    });
});

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

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