多个图像淡入onLoad(同时)

发布于 2024-10-19 07:32:20 字数 1257 浏览 1 评论 0原文

我想在页面加载时同时淡入多个图像。就像这个网站所做的那样: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-10-26 07:32:20

简单的数组和循环就足够了。

首先,在代码顶部添加这样的数组:(

var images = [ "thephoto1", "thephoto2", "thephoto3" ];

包含所有所需图像的 ID)

接下来将函数名称更改为 initImages 以反映它将初始化多个图像的事实,最后添加该循环:

function initImages() {
    for (var i = 0; i < images.length; i++) {
        imageId = images[i];
        image = document.getElementById(imageId);
        setOpacity(image, 0);
        image.style.visibility = "visible";
        fadeIn(imageId, 0);
    }
}

就是这样,无需触摸其他功能。

可爱的猫的现场测试用例:http://jsfiddle.net/yahavbr/e863X/ :-)

Simple array and loop are all you need.

First, add such array on top of the code:

var images = [ "thephoto1", "thephoto2", "thephoto3" ];

(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:

function initImages() {
    for (var i = 0; i < images.length; i++) {
        imageId = images[i];
        image = document.getElementById(imageId);
        setOpacity(image, 0);
        image.style.visibility = "visible";
        fadeIn(imageId, 0);
    }
}

That's it, no need to touch the other functions.

Live test case with cute cats: http://jsfiddle.net/yahavbr/e863X/ :-)

无需解释 2024-10-26 07:32:20

您可以将所有图像包装在一个容器中,如下所示:

<div id="imageContainer">
    <img src="img1.jpg">
    <img src="img2.jpg">
    <img src="img2.jpg">
</div>

将 CSS 更改为:

<style type='text/css'>#imageContainer {visibility:hidden;}</style>

将第一个函数更改为:

function initImage() {
    containerId = 'imageContainer'
    container = document.getElementById(containerId);
    setOpacity(container, 0);
    container.style.visibility = "visible";
    fadeIn(containerId,0);
}

通过在容器上运行淡入淡出效果,您可以向容器添加尽可能多的内容,这一切都会一起淡入,你永远不需要更新你的代码。

You could just wrap all of your images in a single container like this:

<div id="imageContainer">
    <img src="img1.jpg">
    <img src="img2.jpg">
    <img src="img2.jpg">
</div>

Change your CSS to this:

<style type='text/css'>#imageContainer {visibility:hidden;}</style>

Change your first function to this:

function initImage() {
    containerId = 'imageContainer'
    container = document.getElementById(containerId);
    setOpacity(container, 0);
    container.style.visibility = "visible";
    fadeIn(containerId,0);
}

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.

神爱温柔 2024-10-26 07:32:20

他们的做法是使用 jQuery(一个优秀的实现)。所有图像都位于同一个容器中,并使用 jQuery 类选择器进行选择。然后它们会淡入可见区域内的所有元素。他们的 js 文件没有最小化,因此您可以对大部分功能进行逆向工程。需要注意的重要一点不是它一次显示每一行,而是显示适合查看区域的每个元素。它们的关键函数如下所示:

var elTop = $(el).offset().top - $(window).scrollTop();
var elHeight = $(el).height();
// if between top of footer and top of window
if (elTop + elHeight > 40 && elTop < $(window).height()) {

  if ($.inArray($(el).attr("data-unique-id"), elementsInView) < 0) {
    addToView(el);
  }

} else {

  if ($.inArray($(el).attr("data-unique-id"), elementsInView) >= 0) {
    removeFromView(el);
  }

}

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:

var elTop = $(el).offset().top - $(window).scrollTop();
var elHeight = $(el).height();
// if between top of footer and top of window
if (elTop + elHeight > 40 && elTop < $(window).height()) {

  if ($.inArray($(el).attr("data-unique-id"), elementsInView) < 0) {
    addToView(el);
  }

} else {

  if ($.inArray($(el).attr("data-unique-id"), elementsInView) >= 0) {
    removeFromView(el);
  }

}

addToView and removeFromView add and remove the element from an array, then fade is executed on the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文