简体   繁体   English

单击时Javascript淡入新图像

[英]Javascript Fade Into New Image When Clicked

Right now I have a code to switch between two images when clicking text, but I'd like to create a function where I can click on the image itself and it fades into the next one, and be able to do this with more than two images. 现在,我有一个代码可以在单击文本时在两个图像之间切换,但是我想创建一个函数,可以单击图像本身,然后淡入下一个图像,并且可以使用两个以上的图像来完成此操作。图片。 Also the images I want to use vary in width and height, but all have a max height and width of 600px. 我想使用的图像的宽度和高度也有所不同,但是所有图像的最大高度和宽度均为600px。 Here's my current code: 这是我当前的代码:

HTML: HTML:

<div id="cf2" class="shadow">
<img class="bottom" src="/images/Cirques.jpg" />
<img class="top" src="/images/Clown%20Fish.jpg" />
</div>
<p id="cf_onclick">Click me to toggle</p>

CSS: CSS:

#cf2 {
    position:relative;
    height:281px;
    width:450px;
    margin:0 auto;
}
#cf2 img {
    position:absolute;
    left:0;
    -webkit-transition: opacity 1s ease-in-out;
    -moz-transition: opacity 1s ease-in-out;
    -o-transition: opacity 1s ease-in-out;
    transition: opacity 1s ease-in-out;
}

#cf2 img.transparent {
    opacity:0;
}
#cf_onclick {
    cursor:pointer;
}

JavaScript: JavaScript:

$(document).ready(function() {
    $("#cf_onclick").click(function() {
        $("#cf2 img.top").toggleClass("transparent");
    });
});

Try adding #cf2 img to selector initiating click event 尝试将#cf2 img添加到选择器中以启动click事件

$("#cf_onclick, #cf2 img")

 $(document).ready(function() { var n = -1 , imgs = $("#cf2 img") , fx = function(i, el) { return (el || imgs.eq(i)).fadeToggle(1000); }; $("#cf_onclick, #cf2 img").click(function() { if (n === (-imgs.length)) { fx(n); n = -1; fx(null, imgs); } else { fx(n); --n }; }); }); 
 #cf2 { position: relative; height: 281px; width: 450px; margin: 0 auto; } #cf2 img { position: absolute; left: 0; } #cf_onclick { cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div id="cf2" class="shadow"> <img class="bottom" src="http://lorempixel.com/400/400/cats" /> <img class="top" src="http://lorempixel.com/400/400/technics" /> <img class="top" src="http://lorempixel.com/400/400/nature" /> <img class="top" src="http://lorempixel.com/400/400/animals" /> </div> <p id="cf_onclick">Click me to toggle</p> 

This will work for multiple images: 这将适用于多个图像:

 $(document).ready(function() { $.fn.nextOrFirst = function (selector) { var next = this.next(selector); return (next.length) ? next : this.prevAll(selector).last(); }; $("#cf2 img").click(function() { $(this) .removeClass('visible') .nextOrFirst() .addClass('visible'); }); }); 
 #cf2 { position:relative; height:281px; width:450px; margin:0 auto; } #cf2 img { position:absolute; left:0; max-height: 600px; max-width: 600px; opacity: 0; z-index: 0; -webkit-transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -o-transition: opacity 1s ease-in-out; transition: opacity 1s ease-in-out; } #cf2 img.visible { opacity: 1; z-index: 1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="cf2" class="shadow"> <img class="visible" src="http://static.ddmcdn.com/gif/storymaker-best-hubble-space-telescope-images-20092-514x268.jpg" alt="1"/> <img src="http://economictimes.indiatimes.com/thumb/msid-45891755,width-640,resizemode-4/nasas-images-of-most-remarkable-events-you-cant-miss.jpg" alt="2"/> <img src="http://i.dailymail.co.uk/i/pix/2013/11/03/article-2486855-192ACC5200000578-958_964x682.jpg" alt="3"/> <img src="http://mstatic.mit.edu/nom150/items/199-HybridImage.jpg" alt="4"/> </div> 

Instead of having an .invisible class, I used a .visible class, which also has a z-index of 1 , greater than that of the default images. 代替具有的.invisible类,我使用了.visible类,它也有一个z-index1 ,比默认图像的大。 So the image that has that class, will be visible, while the others will have opacity: 0 by default. 因此,具有该类的图像将是可见的,而其他图像将具有opacity: 0默认为opacity: 0

I also extended the jQuery object to have a nextOrFirst method (which I stole from here ), so that when the visible image is the last one of the images in the stack, the next one will be the first. 我还扩展了jQuery对象以具有nextOrFirst方法(我从这里偷走 ),以便当可见图像是堆栈中的最后一个图像时,下一个将是第一个。 So now, the order of the images, as they appear, goes from top to bottom. 因此,现在,图像的显示顺序从上到下。

I decided to do it like this in order to keep your CSS transitions, but you can also use jQuery's fadeIn and fadeOut methods. 我决定这样做是为了保持CSS过渡,但您也可以使用jQuery的fadeInfadeOut方法。

Something like this should do it: 像这样的东西应该这样做:

 $(document).ready(function() { $("#cf_onclick").click(function() { var current = $("#cf2 img.top").removeClass('top'); var next = current.next(); next = next.length ? next : current.siblings().first(); next.addClass('top'); }); }); 
 #cf2 { position:relative; height:281px; width:450px; margin:0 auto; } #cf2 img { position:absolute; left:0; opacity: 0; -webkit-transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -o-transition: opacity 1s ease-in-out; transition: opacity 1s ease-in-out; } #cf2 img.top { opacity: 1; } #cf_onclick { cursor:pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="cf_onclick">Click me to toggle</p> <div id="cf2" class="shadow"> <img src="http://icons.iconarchive.com/icons/google/chrome/256/Google-Chrome-icon.png" /> <img class="top" src="http://icons.iconarchive.com/icons/kyo-tux/aeon/256/Apps-Safari-icon.png" /> </div> 

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

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