图像淡入淡出Javascript(不是JQuery)中的动画

时间:2014-04-06 04:55:11

标签: javascript html animation

我试图制作图片库,图片淡入淡出。 它在很大程度上起作用,但它做了这个奇怪的事情,循环在一个元素上徘徊永远使它做一个奇怪的闪烁。 我觉得逻辑很好,但是我有一些东西可以忽略它成功停止。

var d = document;
var images = new Array();
function rotate(id,delay)
{
    var obj = d.getElementById(id);
    var imageCell = images.length;
    images[imageCell] = obj.children;
    if(images[imageCell].length < 1)
        return 0;
    var gs = images[imageCell].length;
    var srcs = new Array();
    var rng = 0;
    var ln = -1;
    var curImg;
    for(var i = 1; i < gs; i++)
    {
            images[imageCell][i].style.opacity=0;
    }
    fadeOut(obj,curImg,imageCell,rng,ln,gs,delay);
}
function fadeOut(obj,curImg,imageCell,rng,ln,gs,delay)
{
    setTimeout
    (
        function()
        {
            devanimate(images[imageCell][rng],"opacity",250,1,0);
            ln = rng;
            do
            {
                rng = Math.floor(Math.random() * gs);
            }while(ln == rng);
            nextImage(obj,curImg,imageCell,rng,ln,gs,delay);
        },
        delay
    );
}
function nextImage(obj,curImg,imageCell,rng,ln,gs,delay)
{
            images[imageCell][rng].style.left= (obj.clientWidth/2 - images[imageCell][rng].clientWidth/2)+"px";             
            devanimate(images[imageCell][rng],"opacity",250,0,1);
            fadeOut(obj,curImg,imageCell,rng,ln,gs,delay);
}
function devanimate(obj,cssStyle,time,a,b)
{
    if(typeof obj === 'undefined') return;
    var frame = 24;
    var second = 1000;
    var fps = (second/frame);
    var distance = parseInt(b) - parseInt(a);
    var rate = ((distance/frame)*second)/time;
    setTimeout
    (
        function()
        {
            a += rate;
                    obj.style.opacity=a;
            newTime = time-fps;
            devanimate(obj,cssStyle,newTime,a,b);
        }
        ,fps
    );
    if((parseInt(a) >= parseInt(b) && distance >= 0) || (parseInt(a) <= parseInt(b) && distance < 0))
        obj.style.opacity=b;
    return;
}

编辑:

我明白了。 这是HTML的工作代码。

<div id="fpImage" class="border gallery" style="padding:0px;">
    <img src="images/g20.jpg" alt=""/>
    <img src="images/g1.jpg" alt=""/>
    <img src="images/g2.jpg" alt=""/>
    <img src="images/g3.jpg" alt=""/>
    <img src="images/g4.jpg" alt=""/>
    <img src="images/g5.jpg" alt=""/>
    <img src="images/g6.jpg" alt=""/>
    <img src="images/g7.jpg" alt=""/>
    <img src="images/g8.jpg" alt=""/>
    <img src="images/g9.jpg" alt=""/>
    <img src="images/g10.jpg" alt=""/>
    <img src="images/g11.jpg" alt=""/>
    <img src="images/g12.jpg" alt=""/>
    <img src="images/g13.jpg" alt=""/>
    <img src="images/g14.jpg" alt=""/>
    <img src="images/g15.jpg" alt=""/>
    <img src="images/g16.jpg" alt=""/>
    <img src="images/g18.jpg" alt=""/>
    <img src="images/g19.jpg" alt=""/>
    <img src="images/g17.jpg" alt=""/>
</div>
<script type="text/javascript">
    rotate("fpImage",3000);
</script>

var d = document;
var images = new Array();//Set Array so you can have multiple galleries on a page at a time
function rotate(id,delay)
{
    var obj = d.getElementById(id);
    var imageCell = images.length;
    images[imageCell] = obj.children;//set specific gallery into images array
    if(images[imageCell].length < 1)
        return 0;
    var gs = images[imageCell].length;
    var rng = 0;//Initialize rng
    var ln = -1;//initialize Last Number. This is used so the rng doesn't bring hte same number twice in a row
    setTimeout//This set Timeout centers each image horizontally.
    (
        function()
        {
            for(var i = 0; i < gs; i++)
                images[imageCell][i].style.left= (obj.clientWidth/2 - images[imageCell][i].clientWidth/2)+"px";
        },delay
    );
    for(var i = 1; i < gs; i++)//this makes all the images initially 100% transparent
    {
        images[imageCell][i].style.opacity=0;
        images[imageCell][i].style.filter='alpha(opacity=0)';
    }
    nextImage(imageCell,rng,ln,gs,delay);
}
function nextImage(imageCell,rng,ln,gs,delay)
{
    setTimeout
    (
        function()
        {
            devanimate(images[imageCell][rng],250,100,0);//Fade out current image
            ln = rng;//Remember previous RNG Number
            do
            {
                rng = Math.floor(Math.random() * gs);//
            }while(ln == rng);//Loop until new RNG Number is found
            devanimate(images[imageCell][rng],250,0,100);//Fade in new image
            nextImage(imageCell,rng,ln,gs,delay);//call this function again
        },
        delay
    );
}
function devanimate(obj,time,a,b)
{
    var frame = 30;//FPS Rate, so this is currently set to 30 fps
    var second = 1000;
    var fps = (second/frame);
    var distance = parseInt(b) - parseInt(a);
    var rate = ((distance/frame)*second)/time;
    setTimeout
    (
        function()
        {
            a += rate;
            obj.style.opacity=(a/100);
            obj.style.filter='alpha(opacity='+a+')';
            newTime = time-fps;
            if((parseInt(a) >= parseInt(b) && distance >= 0) || (parseInt(a) <= parseInt(b) && distance < 0))
            {
                obj.style.opacity=(b/100);
                obj.style.filter='alpha(opacity='+b+')';
            }
            else
                devanimate(obj,newTime,a,b);
        }
        ,fps
    );
}

1 个答案:

答案 0 :(得分:0)

您应该可以从以下链接获取代码。只需查看swissarmy.js链接:

http://www.dynamicdrive.com/dynamicindex14/swissarmy/index.htm

脚本正在做的是将旧版IE的过滤器属性应用于图像。在其他浏览器上,它使用opacity属性。这是了解不透明度的更好链接:

http://www.scriptiny.com/2011/01/javascript-fade-in-out/