简体   繁体   English

JavaScript递归问题

[英]Javascript recursion problem

I am trying to write a function that will change between an array of images. 我正在尝试编写一个将在图像数组之间更改的函数。 What I have makes sense to me, but when I run it as it is below then the first image loads, then the second image loads at some point (the transparency doesn't change) and then nothing happens. 我的想法对我来说很有意义,但是当我按下面的方式运行它时,第一个图像加载,然后第二个图像在某个点加载(透明度不变),然后什么也没有发生。 Here is the javascript code: 这是JavaScript代码:

        var image1 = new Image();var image2 = new Image(); var image3 = new Image();
        image1.src = "images/website6.jpg"; image2.src = "images/website7.jpg"; image3.src = "images/sunset.jpg";
        var images = new Array("images/website6.jpg","images/website7.jpg","images/sunset.jpg");
        /*document.slide.style.opacity = 1;
        document.slide.stylebg.opacity = 0;*/
        setTimeout(function() { fade(images,0); }, 2000);
        function delay(arr,num)
        {
            document.slide.src = arr[num % 3];
            document.slide.style.opacity = 1;
            document.slidebg.style.opacity = 0;
            document.slidebg.src = arr[(num+1)%3];
            var number = num + 1;
            setTimeout(function() { fade(arr,number); }, 2000);
        }
        function fade(arr,num)
        {
            /*alert('fadebefore ' + (document.slide.style.opacity).toString())*/
            document.slide.style.opacity -= 0.1
            /*alert('fade')*/
            document.slide.stylebg.opacity += 0.1
            if (document.slide.style.opacity == 0)
            {
                setTimeout(function() { delay(arr,num); }, 150);
            }
            else
            {
                setTimeout(function() { fade(arr,num); }, 1500);
            }
        }

The HTML is simple. HTML很简单。 I have two classes; 我有两节课; style and stylebg . stylestylebg style sits in front of stylebg and then I change opacities and images as needed. style位于stylebg前面,然后我根据需要更改不透明度和图像。 The javascript seems logical to me, but it doesn't work as expected. JavaScript对我来说似乎合乎逻辑,但无法按预期工作。 Also worth noting is there are 3 comments. 另外值得注意的是有3条评论。 The first comment (line 3-4) is attemping to set the opacities to what they should be at the beginning. 第一条评论(第3-4行)试图将不透明度设置为开始时的状态。 However, if I do this then I get even less progress than above: the first image loads and nothing else happens. 但是,如果我这样做,那么我得到的进展将比上面还要少:加载第一张图像,然后什么也没有发生。 The second two comments are used for debugging purposes. 后两个注释用于调试目的。 If I uncomment these then the image change occurs between the two alerts, which would seem to say the image change is caused by the opacity change. 如果我不加注释,则图像更改将在两个警报之间发生,这似乎表明图像更改是由不透明度更改引起的。

So can anybody explain why it isn't doing what I expect it to? 那么,谁能解释为什么它没有按照我的期望去做? Thanks. 谢谢。

EDIT: Some more code: 编辑:更多代码:

HTML (This is the only part being affected): HTML(这是唯一受影响的部分):

<div style="position: relative;">
<img src="images/sunset.jpg" id="slide" />
<img src="images/website6.jpg" id="slidebg" />
</div>

CSS: CSS:

#slide{
    display:block;
    margin-left:5;
    margin-right:auto;
    border: 1px solid black;
    z-index: 1;
    top: 0px;
    position: relative;
}
#slidebg{
    display:block;
    margin-left:auto;
    margin-right:auto;
    border: 1px solid black;
    z-index: 0;
    top: 0px;
    position: absolute;
}

You can't just refer to page elements by "id" value like that; 您不能只通过“ id”值来引用页面元素; you have to use "document.getElementById()": 您必须使用“ document.getElementById()”:

 var slideElement = document.getElementById('slide'), slidebgElement = document.getElementById('slidebg');

Then you'd work with "slideElement.style" etc. instead of "document.slide.style". 然后,您将使用“ slideElement.style”等代替“ document.slide.style”。

Answering for formatting's sake. 为格式化而回答。 I have not debugged to see what would work, but this makes more sense to me 我还没有调试看什么可行,但这对我来说更有意义

If you want to use ID, use document.getElementById, if you want to use name, use document.images[imgname] or document.imagename You are mixing names and style too. 如果要使用ID,请使用document.getElementById,如果要使用名称,请使用document.images [imgname]或document.imagename您也要混合使用名称和样式。 document.slide.stylebg.opacity and such document.slide.stylebg.opacity等

This does something. 这样做。 Not sure it is what you want: http://jsfiddle.net/EcShW/2/ 不确定它是否是您想要的: http : //jsfiddle.net/EcShW/2/

<div style="position: relative;">
<img src="images/sunset.jpg" id="slide" name="slide" />
<img src="images/website6.jpg" id="slidebg" name="slidebg" />
</div>


    var images = []; // can be more elegant, but this is simpler
    images[0]=new Image(); images[0].src="images/website6.jpg";
    images[1]=new Image(); images[1].src="images/website7.jpg";
    images[2]=new Image(); images[2].src="images/sunset.jpg"; 
    var tid1,tid2,tid3
    tid1=setTimeout(function() { fade(images,0); }, 2000);
    function delay(arr,num)
    {
        document.slide.src = arr[num % 3].src;
        document.slide.style.opacity = 1;
        document.slidebg.style.opacity = 0;
        document.slidebg.src = arr[(num+1)%3].src;
        var number = num + 1;
        tid2=setTimeout(function() { fade(arr,number); }, 2000);
    }
    function fade(arr,num)
    {
        document.slide.style.opacity -= 0.1
        document.stylebg.style.opacity += 0.1
        if (document.slide.style.opacity == 0)
        {
            tid3=setTimeout(function() { delay(arr,num); }, 150);
        }
        else
        {
            tid3=setTimeout(function() { fade(arr,num); }, 1500);
        }
    }

EDIT2 编辑2

It works for me (chromium) 它对我有用(铬)

var images = new Array ();
images[0] = new Image();
images[0].src = "images/website6.jpg";
images[1] = new Image();
images[1].src = "images/website7.jpg";
images[2] = new Image();
images[2].src = "images/sunset.jpg";

function delay ( arr, num )
{
  slide.src = arr[num % 3].src;
  slide.style.opacity = 1;
  slidebg.style.opacity = 0;
  slidebg.src = arr[(num+1)%3].src;
  var number = num + 1;
  setTimeout(function() { fade(arr,number); }, 2000);
}

function fade(arr, num)
{
  if ( slide.style.opacity == 0 )
  setTimeout(function (){ delay(arr, num); }, 150);
  else
  {
    slide.style.opacity = (slide.style.opacity*10 - 1)/10 ;
    slidebg.style.opacity -= -0.1;
    setTimeout ( function(){fade(arr,num);}, 1500 );
  }
}
fade (images, 0);

AHA! 啊!

<img id="img_id"/> we can get by document.img_id , even in chromium <img id="img_id"/>即使在铬<img id="img_id"/>我们也可以通过document.img_id获得



OK, OK, removed links to this WE TEACH YOU TO FAIL AT WEB DEV site.. 好吧,好吧,删除了指向我们在WEB DEV网站上导致我们失败的链接。

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

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