1

我对javascript很陌生。以下代码工作正常。当我单击“下一步”按钮时,幻灯片会停止它应该怎么做,但是当单击显示上一张图片时,它只显示一张图片并停止?我不明白出了什么问题。请帮帮我

window.onload = function () {

    newImg = new Array("images/image1.jpg", "images/image2.jpg", "images/image3.jpg", "images/image4.jpg");

    var iImage = 1;

    var imgTimer = new Timer();
    imgTimer.Interval = 5000;
    imgTimer.Tick = function () {
        document.slideshow.src = newImg[iImage];
        iImage++
        if (iImage >= newImg.length) {
            iImage = 0;
        }

        document.getElementById('next').onclick = function () {
            if (iImage < newImg.length) {
                document.slideshow.src = newImg[iImage];
                iImage++
                if (iImage = newImg.length) {
                    iImage = 0;

                }
            }
            imgTimer.Stop();
        }
        document.getElementById('prev').onclick = function () {
            if (iImage > 0) {
                document.slideshow.src = newImg[iImage];
                iImage--
                if (iImage = 0) {
                    iImage = newImg.length;
                }
            }
            imgTimer.Stop();
        }


    };
    imgTimer.Start();
};
4

1 回答 1

1

此 javascript 循环浏览图像,但如果用户单击下一个或上一个按钮,则自动循环停止。

window.onload = function () {
    var newImg = ["images/image1.jpg",
                  "images/image2.jpg",
                  "images/image3.jpg",
                  "images/image4.jpg"],
        iImage = 0,
        slide = document.getElementById('slideshow'),
        next = document.getElementById('next'),
        prev = document.getElementById('prev'),
        interval = 3200,
        t = null;

    next.onclick = function () {
        iImage++;
        if (iImage >=  newImg.length) {
            iImage = 0;
        }
        slide.src = newImg[iImage];
        if (t !== null) {
            clearInterval(t);
            t = null;
        }
    };

    prev.onclick = function () {
        iImage--;
        if (iImage < 0) {
            iImage = newImg.length - 1;
        }
        slide.src = newImg[iImage];
        if (t !== null) {
            clearInterval(t);
            t = null;
        }
    };

    t = setInterval( function () {
        iImage++;
        if (iImage >= newImg.length) {
            iImage = 0;
        }
        slide.src = newImg[iImage];
    }, interval);

    slide.src = newImg[iImage];
};

这假设 html 是这样的:

<input id='prev' value='prev' type='button'/> &nbsp;
<input id='next' value='next' type='button'/> <br/>
<img id='slideshow' src='' style='width:300px;'/>
于 2012-04-28T04:44:13.660 回答