简体   繁体   English

普通图像和所选图像? 需要jQuery还是CSS?

[英]Normal image and selected image? jQuery or css required?

Sorry if this is already posted, but I really need your help. 抱歉,如果已经发布,但我确实需要您的帮助。

What I want is following: 我想要的是:

Let's say I have this image as normal image that can be clicked. 假设我将此图像作为可以单击的普通图像。 http://i.imgur.com/SOd1W.png http://i.imgur.com/SOd1W.png

Now let's say, when I click on that image it will fade this image: http://i.imgur.com/QW15C.png 现在说,当我单击该图像时,它将使该图像褪色: http : //i.imgur.com/QW15C.png

Can anyone tell me how to code this on click image selection ? 谁能告诉我如何在点击图片选择时对此进行编码? Or if it cannot be onclick then how else, please? 或者,如果不能为onclick,那还如何?

EDIT: Also I forgot to mention. 编辑:我也忘了提。 If I have 2 selecting choices... And if I click one choice it fades in and if I select choice 2, choice 1 should fade out and choice 2 fade in like it should... 如果我有2个选择选项...并且如果单击一个选项,它会淡入,如果我选择选项2,则选项1应该会淡出,而选项2应该像它一样淡入...

Presuming by fade, you mean an animated fade, you can use the following code: 假定淡入淡出,是指动画淡入淡出,可以使用以下代码:

HTML: HTML:

<div id="something" class="theImage" style="height: 242px; width: 194px; background: url(http://i.imgur.com/QW15C.png);">
  <img src="http://i.imgur.com/SOd1W.png" />
</div>

<div id="somethingElse" class="theImage" style="height: 242px; width: 194px; background: url(http://i.imgur.com/QW15C.png);">
  <img src="http://i.imgur.com/SOd1W.png" />
</div>

jQuery: jQuery的:

$(document).ready(function(){
    $('.theImage img').click(function(){
      var current = ($(this).parent().attr('id') == 'something') ? 'somethingElse' : 'something';console.log(current);
      $(this).fadeOut();
      $('#'+current + ' img').fadeIn();
    });
});

This will show the default image to start off with, then fade out to show the background image of it's container when clicked. 这将显示默认图像,然后单击以淡出以显示其容器的背景图像。 You may decide to use different HTML elements depending on your situation. 您可以根据情况决定使用不同的HTML元素。

One way (there are several): 一种方式(有几种):

Include both images as tags (not background images) wrapped inside a div: 将两个图像都作为标签(不是背景图像)包装在div中:

<div id="wrapper">
    <img src="http://i2.cdn.turner.com/cnn/dam/assets/120423012508-merkel-sarkozy-c1-main.jpg" id="img1" />
    <img src="http://d2o307dm5mqftz.cloudfront.net/1005866/1334968885891/Perfect%20Gift%20No%20Pink_300x250.jpg" id="img2" />
</div>

CSS: CSS:

#wrapper{
    position:relative;
    height:242px;
    width:194px;
}      
#wrapper img{
    position:absolute;
    left:0px;
    top:0px;
    height:242px;
    width:194px;
} 
#img1{display:none;}

jQuery: jQuery的:

$('#wrapper').on('click', function (event){
    $('#wrapper img').fadeToggle();
});
$(document).ready(function() {
    var imagelist = ["http://i.imgur.com/SOd1W.png","http://i.imgur.com/QW15C.png"];

    $('.img1').click(function(e) {   
             var myimage = imagelist [Math.floor(Math.random()*imagelist .length)];
             $('.img1').attr('src', myimage );

    });
});

See the demo ... Source simple image randomizer with jQuery by Brian Cray 观看演示 ... Brian Cray的jQuery简单图像随机化器

Alternatively you can use CSS3 as well. 另外,您也可以使用CSS3。 See more about A Simple Fade with CSS3 进一步了解CSS3的简单淡入淡出

Use This Javascript 使用此JavaScript

function Image(imageId)
{
    var obj = document.getElementById(imageId);
    if(obj.alt=='Fade'){
       obj.src="image1.jpg";     
       obj.alt = "Unfadde";
     }
    else
     {
       obj.src="image2.jpg";     
       obj.alt = "Fade";
      }  
 }

and HTML 和HTML

<img src="image1.jpg" onclick=Image("image") id="image" name="image" alt="Fade" />

Looks like job for jQuery click() 看起来像jQuery click()的工作

Here is code http://jsfiddle.net/ZzDJ8/ 这是代码http://jsfiddle.net/ZzDJ8/

It's very simple in CSS, you need to give the image an active pseudoclass: 这在CSS中非常简单,您需要为图像提供一个活动的伪类:

.img {background: url(firstimg.png);} .img {background:url(firstimg.png);}

.img:active {background: url(clickimg.png);} .img:active {background:url(clickimg.png);}

Here's an example: http://jsfiddle.net/J6kJh/ 这是一个示例: http : //jsfiddle.net/J6kJh/

<img src="a.png" id="MyImg" />

jQuery的:

$("#MyImg").click(function() { $(this).attr("src","b.png") });

您是否正在寻找这样的东西?

<img src="http://i.imgur.com/SOd1W.png" onclick="$(this).fadeOut().attr('src','http://i.imgur.com/QW15C.png').fadeIn();">   

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

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