简体   繁体   English

更改图片上的图片文件路径:悬停

[英]Changing image file path on image:hover

I'm currently working on a portfolio website, where I have to insert a series of reference logos in a couple rows, just like: 我目前正在投资组合网站上工作,在这里我必须在几行中插入一系列参考徽标,就像:

<div class="row">
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/adelie.png" alt="Adelie"></a>
    </div>
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/amageme.png" alt="Amageme"></a>
    </div>
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/bakey.png" alt="Bakey"></a>
    </div>
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/byensdepot.png" alt="Byensdepot"></a>
    </div>
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/dialoogle.png" alt="Dialoogle"></a>
    </div>
    <div class="col-sm-2 reference">
        <a href="#"><img class="img-responsive" src="img/logo section/final/faded/drachmann.png" alt="Drachmann"></a>
    </div>
</div>

But when hovering over a logo, I'd like to change the file path, somehow like: 但是当将鼠标悬停在徽标上时,我想更改文件路径,就像这样:

var fadedpath = "img/logo section/final/faded/";
var colorpath = "img/logo section/final/color/";
$(".reference").hover(function(){
    $(this).attr(colorpath, filename);
}, function(){
    $(this).attr(fadedpath, filename);
});

What is the correct syntax to do so? 正确的语法是什么? Because talking about multiple files, it's not effective to change it in css one by one. 因为谈论多个文件,所以在css中一一更改它并没有效果。

Just use replace prototype method from string object. 只需使用replace string对象中的原型方法即可。 So, you need to change your code just like: 因此,您需要像下面那样更改代码:


REV 1 : 修订版1:

  • ADD $(this).find('img') : Sorry I am miss-reading your HTML format 添加$(this).find('img') :对不起,我想念您的HTML格式

var fadedpath = "/faded/";
var colorpath = "/color/";
var oldPath = '';

$(".reference").hover(function() {
    oldPath = $(this).find('img').attr('src');
    $(this).find('img').attr(src, oldPath.replace(fadedpath, colorpath));

}, function() {
    oldPath = $(this).find('img').attr('src');
    $(this).find('img').attr(src, oldPath.replace(colorpath, fadedpath));
});

Your code is almost correct however your user of attr() is incorrect. 您的代码几乎正确,但是您的attr()用户不正确。 You need to use the getter to retrieve the current src and then replace() the values. 您需要使用getter来检索当前的src ,然后使用replace()值。 You also need to change the attribute on the image within .reference , not on the .reference element itself. 您还需要更改.reference图像的属性,而不是.reference元素本身。 Try this: 尝试这个:

var fadedpath = "img/logo section/final/faded/";
var colorpath = "img/logo section/final/color/";

$(".reference").hover(function(){
    $(this).find('img').attr('src', function(i, src) {
        return src.replace(fadedpath, colorpath);
    });
}, function(){
    $(this).find('img').attr('src', function(i, src) {
        return src.replace(colorpath, fadedpath);
    });
});

Also note that you shouldn't put spaces in folder names used in URLs. 另请注意,您不应在URL中使用的文件夹名称中放置空格。

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

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