Difficulty in Understanding Slideshow script

后端 未结 1 804
名媛妹妹
名媛妹妹 2021-02-02 05:10

I have taken slide show script from net. But There some functions i cannot understand here is script



        
相关标签:
1条回答
  • 2021-02-02 05:31

    Ok I figured it out. My code had many errors.

    imageItem is a constructor function designed to preload images. It returns the src (location) of the image that it preloads.

    However the function get_ImageItemLocation() assumes that imageItem has returned an object.

    The whole thing should simplify to:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
        >
    <html lang="en">
    <head>
    <title></title>
    <script>
    var interval = 1500;
    var random_display = 0;
    var imageDir = "my_images/";
    var imageNum = -1;
    var place;//For convenience this is best as a global
    function imageItem(url) {
        this.img = new Image();
        this.img.src = url;
        this.url = url;
    }
    var imageArray = [
        new imageItem(imageDir + "01.jpg"),
        new imageItem(imageDir + "02.jpg"),
        new imageItem(imageDir + "03.jpg"),
        new imageItem(imageDir + "04.jpg"),
        new imageItem(imageDir + "05.jpg")
    ];
    function randNum(x, y) {
        var range = y - x + 1;
        return Math.floor(Math.random() * range) + x;
    }
    function getNextImage() {
        imageNum = (random_display) ? randNum(0, imageArray.length-1) : ((imageNum+1) % imageArray.length);
        return imageArray[imageNum];
    }
    function getPrevImage() {
    //  imageNum = (imageNum-1) % imageArray.length;//This is not correct
        imageNum = (imageNum-1 >= 0) ? (imageNum-1) : (imageArray.length-1);
        return imageArray[imageNum];
    }
    function prevImage() {
        var image_item = getPrevImage();
        place.src = place.alt = image_item.url;
    }
    function nextImage() {
        var image_item = getNextImage();
        place.src = place.alt = image_item.url;
    }
    function play() {
        nextImage();
        timerID = setTimeout(play, interval);
    }
    window.onload = function(){
        place = document['slideImg'];
        play();
    };
    </script>
    </head>
    
    <body>
    
    <img name="slideImg" src="27.jpg" width="500" height="375" border="0"><br />
    <a href="#" onclick="play()">Play slide show</a>&nbsp;|&nbsp;
    <a href="#" onclick="clearTimeout(timerID)">Pause</a>&nbsp;|&nbsp;
    <a href="#" onclick="clearTimeout(timerID); prevImage();">Previous</a>&nbsp;|&nbsp;
    <a href="#" onclick="clearTimeout(timerID); nextImage();">Next</a> &nbsp;
    
    </body>
    </html>
    

    Well If you want imageArray to be an array of image urls, then the simplest way is: ...

    function imageItem(url) {
        var img = new Image();
        img.src = url;
        return url;
    }
    for(var i=0; i<5; i++){
        imageArray[i] = imageItem(imageDir + i + ".jpg");
    }
    

    Note how imageItem is called without new in this case. Hope you will understand it now. Let me know if you have further problem with this code.

    0 讨论(0)
提交回复
热议问题