[英]Giving fadeIn fadeOut effect on changing the image src
I was working with responsive web design and I wanted to slide some images in a page. 我当时正在使用自适应网页设计,我想在页面中滑动一些图像。 I tried some plugins but the problem with plugin is it uses width and height property and some also assign position absolute.
我尝试了一些插件,但是插件的问题是它使用width和height属性,还有一些还分配了绝对位置。 So I thought of changing the src of image myself using js and it worked fine, but can I give some transition effect to it?
因此,我想到了自己使用js更改图片的src的方法,效果很好,但是我可以给它一些过渡效果吗?
What I have done is: 我所做的是:
var i =0;
var total =2;
window.setInterval(function(){
show_hide();
}, 1000);
function show_hide()
{
var img=$('.image-holder img, .image-holder2 img');
//alert(img.length);
if(i%2==0)
{
img[0].src='http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png';
img[1].src='http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png';
i=0;
}
else
{
img[0].src='http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834';
img[1].src='http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834';
}
i++;
}
my html 我的HTML
<div class="image-holder" >
<img src="http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834" />
</div>
<div class="image-holder2" >
<img src="http://healthystartups.com/storage/600px-MA_Route_1.png?__SQUARESPACE_CACHEVERSION=1319542839834" />
</div>
Answering the title, giving a fadeIn/fadeOut while changing the src is easy, just let the element fadeOut, change the src and let it fade in again. 回答标题,在更改src的同时提供fadeIn / fadeOut很容易,只需让元素fadeOut,更改src并使其再次淡入即可。 Also, I would like to point out that with jQuery, iterating through a class like that, ruins the purpose of using it's own selector "
.each()
" 另外,我想指出的是,使用jQuery遍历这样的类,破坏了使用它自己的选择器“
.each()
”的目的。
$('.image-holder img, .image-holder2 img').each(function() {
$(this).fadeOut(200,function() {
$(this).attr('src', 'http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png');
$(this).fadeIn(200);
});
});
Use jQuery with animate()
. 将jQuery与
animate()
。 You can also run show(N ms)
on one of the images and hide(N ms)
on the other. 您也可以在其中一幅图像上运行
show(N ms)
在另一幅图像上运行hide(N ms)
。 You can also choose how to show/hide the images using effects (like fadein and fadeout). 您还可以选择使用效果(例如淡入和淡出)显示/隐藏图像的方式。
Also, have a look at http://api.jquery.com/fadeIn/ and http://api.jquery.com/fadeOut/ 另外,请访问http://api.jquery.com/fadeIn/和http://api.jquery.com/fadeOut/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.