简体   繁体   中英

how to make a transparent image in jQuery?

I want to make the A image transparently when the mouse pointer is over the B image so that I can see either image.

How should I change this?

在此处输入图片说明

You can use CSS opacity property to make image transparent

$("img").bind("mouseover", function() {
  $("img").css("opacity", "0.5");
  $(this).css("opacity", "1");
});

Here is link to JSFiddle: https://jsfiddle.net/8hbmyu9k/2/

You can change the opacity value of image on the hover event of image B.

img.transparent
{
   opacity: 0.3; filter: alpha(opacity=30); /* For IE 8 & 9 (more valid) */
}

Now at the hover event of image B (img tag with class B)

$("img.B").hover(function(){
   $("img.A").addClass("transparent");    
},function(){
   $("img.A").removeClass("transparent");    
});

If you are having trouble with posting code here, read this . Make sure you include this with any jQuery code you use:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

This demo is simplified to show some possibilities.

Snippet

 $(function() { $('.B').mouseover(function() { $('.A').css('visibility', 'hidden'); $('.C').hide(); $('.D').addClass('invisible'); }); $('.B').mouseleave(function() { $('.A').css('visibility', 'visible'); $('.C').show(); $('.D').removeClass('invisible'); }); }); 
 .invisible { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="http://placehold.it/100x100/000/fff?text=A" class="A"> <img src="http://placehold.it/100x100/00e/fc1?text=B" class="B"> <img src="http://placehold.it/100x100/fc1/00e?text=C" class="C"> <img src="http://placehold.it/100x100/e0e/fcf?text=D" class="D"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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