简体   繁体   English

jQuery显示和隐藏图片

[英]jquery show and hide pictures

Hi hope someone can help me with this i want pictures to hide and show when hovering over them.... all divs have the same classes and the pictures it toggles has the same classes aswell example: 嗨,希望有人可以帮我解决这个问题,我希望将图片悬停在它们上方时可以隐藏和显示。...所有div都具有相同的类,并且切换后的图片也具有相同的类,例如:

<div class="square"><img class = "imagesbw" src="images/image3bw.png"  alt="" width="91" height="76">
<img class = "imagescol" src="images/image3.png"  alt="" width="91" height="76">
</div>

<div class="square"><img class = "imagesbw" src="images/image3bw.png"  alt="" width="91" height="76">
<img class = "imagescol" src="images/image3.png"  alt="" width="91" height="76">
</div>

<div class="square"><img class = "imagesbw" src="images/image3bw.png"  alt="" width="91" height="76">
<img class = "imagescol" src="images/image3.png"  alt="" width="91" height="76">
</div>

<div class="square"><img class = "imagesbw" src="images/image3bw.png"  alt="" width="91" height="76">
<img class = "imagescol" src="images/image3.png"  alt="" width="91" height="76">
</div>

<div class="square"><img class = "imagesbw" src="images/image3bw.png"  alt="" width="91" height="76">
<img class = "imagescol" src="images/image3.png"  alt="" width="91" height="76">
</div>

so there a black and white picture above a color picture..i want to mousein over one of them and hid that one to show the one behind... 所以在彩色图片上方有一个黑白图片..我想将鼠标悬停在其中一个上并将其隐藏起来以显示后面的一个...

Heres what i did in jquery 这是我在jQuery中所做的

$(document).ready(function() {   
  $(".imagesbw").mouseover(function(){
  $(".imagesbw").hide();



  })
    $(".imagescol").mouseout(function(){
  $(".imagesbw").show();
});
})

Only problem is that there are multiple of them and all of them show and hide at the same time while over one... i just want the one the mouse is over to hide and show and so on.... 唯一的问题是它们有多个,并且它们都同时显示和隐藏,而一个以上...我只希望鼠标悬停的那个可以隐藏和显示,等等。

im new to jquery hope this make sense 我是jQuery的新手,希望这有意义

Here's a sample of what i want to do under the heading LATEST RELEASES and SNEAKY PEAKS 是我想在最新发布和偷偷摸摸的峰值下做一个示例

thanks 谢谢

Execute the code using this context, In the event handler this refers to element which invoked the event. 使用this上下文执行代码。在事件处理程序中, this是指调用事件的元素。

$(document).ready(function() {   
    $(".imagesbw").mouseover(function(){
        $(this).hide();
    })
    $(".imagescol").mouseout(function(){
        //Here use the prev() method in conjection with this 
        $(this).prev(".imagesbw").show(); 
    });
})

you just need to use this . 您只需要使用this In you method the first class will pickup every time and with this the current element event will occur . 在您的方法中,第一类将每次拾取,并this发生当前元素事件。 this is a reference to the member that invokes the current function this是对调用当前函数的成员的引用

$(document).ready(function() {   
  $(".imagesbw").mouseover(function(){
  $(this).hide();



  })
    $(".imagescol").mouseout(function(){
  $(this).show();
});
})

You are providing a generic solution. 您正在提供通用解决方案。 You should be particular to use this this way: 你应该特别使用this这种方式:

$(document).ready(function() {   
    $(".imagesbw").mouseover(function(){
        $(this).hide();
    })
    $(".imagescol").mouseout(function(){
        $(this).prev(".imagesbw").show();
    });
});

Update: Just saw even Satpal has given the same answer! 更新:甚至看到Satpal都给出了相同的答案! :)

Changes in your code: 代码更改:

$(document).ready(function() {
  // hide all bw images (you can also do this by css - display:none)
  $('.imagesbw').hide();

  // on mouseover event - hide current color image and show b&w image
  $(".imagescol").mouseover(function(){
     $(this).hide();
     $(this).prev().show();
  });

  // on mouseout event - hide current b&w image and show color image
  $(".imagesbw").mouseout(function(){
     $(this).hide();
     $(this).next().show();
  });
});

JSFiddle for your example is here 您的示例的JSFiddle 在这里

You can also achieve the same effect by using css only 您也可以通过仅使用CSS来达到相同的效果

.imagescol:hover {
   -webkit-filter: grayscale(100%); 
   filter: grayscale(100%);
}

Then you don't need two different images (color and b&w) and you also don't need javascripts. 然后,您就不需要两个不同的图像(彩色和黑白),也不需要JavaScript。 JSFiddle is here . JSFiddle在这里

you have to use this in this scenario 您必须在这种情况下使用此

$(document).ready(function() {   
$(".imagesbw").mouseover(function(){
    $(this).hide();
})
$(".imagescol").mouseout(function(){
    $(this).parent().find(".imagesbw").show();
});

}) })

http://plnkr.co/edit/1vDNHnaRKkAIfxhzSr3e?p=preview http://plnkr.co/edit/1vDNHnaRKkAIfxhzSr3e?p=preview

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

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