多个图像淡入onLoad(同时)
我想在页面加载时同时淡入多个图像。就像这个网站所做的那样:http://www.struckaxiom.com/work。我的脚本只能在一张图像上执行此操作,但我想包含更多图像。 这是单张照片脚本。请帮忙。
document.write("<style type='text/css'>#thephoto {visibility:hidden;}</style>");
function initImage() {
imageId = 'thephoto'
image = document.getElementById(imageId);
setOpacity(image, 0);
image.style.visibility = "visible";
fadeIn(imageId,ImageId2,0);
}
function fadeIn(objId, opacity) {
if (document.getElementById) {
obj = document.getElementById(objId);
if (opacity <= 100) {
setOpacity(obj, opacity);
opacity += 10;
window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
}
}
}
function setOpacity(obj, opacity) {
opacity = (opacity == 100)?99.999:opacity;
// IE/Win
obj.style.filter = "alpha(opacity:"+opacity+")";
// Safari<1.2, Konqueror
obj.style.KHTMLOpacity = opacity/100;
// Older Mozilla and Firefox
obj.style.MozOpacity = opacity/100;
// Safari 1.2, newer Firefox and Mozilla, CSS3
obj.style.opacity = opacity/100;
}
window.onload = function() {initImage()}
// -->
</script>
谢谢!
I want to fade in multiple images at the same time as the page loads. Just like this website does it: http://www.struckaxiom.com/work. I have the script to do it only on one image, but I want to have more images included.
This is the single photo script. Please help.
document.write("<style type='text/css'>#thephoto {visibility:hidden;}</style>");
function initImage() {
imageId = 'thephoto'
image = document.getElementById(imageId);
setOpacity(image, 0);
image.style.visibility = "visible";
fadeIn(imageId,ImageId2,0);
}
function fadeIn(objId, opacity) {
if (document.getElementById) {
obj = document.getElementById(objId);
if (opacity <= 100) {
setOpacity(obj, opacity);
opacity += 10;
window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
}
}
}
function setOpacity(obj, opacity) {
opacity = (opacity == 100)?99.999:opacity;
// IE/Win
obj.style.filter = "alpha(opacity:"+opacity+")";
// Safari<1.2, Konqueror
obj.style.KHTMLOpacity = opacity/100;
// Older Mozilla and Firefox
obj.style.MozOpacity = opacity/100;
// Safari 1.2, newer Firefox and Mozilla, CSS3
obj.style.opacity = opacity/100;
}
window.onload = function() {initImage()}
// -->
</script>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的数组和循环就足够了。
首先,在代码顶部添加这样的数组:(
包含所有所需图像的 ID)
接下来将函数名称更改为 initImages 以反映它将初始化多个图像的事实,最后添加该循环:
就是这样,无需触摸其他功能。
可爱的猫的现场测试用例:http://jsfiddle.net/yahavbr/e863X/ :-)
Simple array and loop are all you need.
First, add such array on top of the code:
(With the ID of all desired images)
Next change the function name to initImages to reflect the fact it will initialize more than one image and finally add that loop:
That's it, no need to touch the other functions.
Live test case with cute cats: http://jsfiddle.net/yahavbr/e863X/ :-)
您可以将所有图像包装在一个容器中,如下所示:
将 CSS 更改为:
将第一个函数更改为:
通过在容器上运行淡入淡出效果,您可以向容器添加尽可能多的内容,这一切都会一起淡入,你永远不需要更新你的代码。
You could just wrap all of your images in a single container like this:
Change your CSS to this:
Change your first function to this:
By running the fading effect on the container you can then add as much content to the container and it will all fade in together and you never have to update your code.
他们的做法是使用 jQuery(一个优秀的实现)。所有图像都位于同一个容器中,并使用 jQuery 类选择器进行选择。然后它们会淡入可见区域内的所有元素。他们的 js 文件没有最小化,因此您可以对大部分功能进行逆向工程。需要注意的重要一点不是它一次显示每一行,而是显示适合查看区域的每个元素。它们的关键函数如下所示:
addToView和removeFromView从数组中添加和删除元素,然后在数组上执行fade。
The way they are doing is using jQuery (an excellent implementation). All of the images are in the same container and are selected using the jQuery class selector. Then they fade in all elements that fit within the viewable area. Their js file is not minimized so you could reverse engineer most of that functionality. The important thing to note is not that it is showing each row at a time but every element that fits in the viewing area. Their key function looks like this:
addToView and removeFromView add and remove the element from an array, then fade is executed on the array.