JS中的图像滑块无法正常工作

时间:2015-08-12 16:05:08

标签: javascript html image

我使用以下代码作为js滑块。但它只显示第一张图片:

在head标签中:

<script>
var slideimages = new Array()
slideimages[0] = new Image() 
slideimages[0].src = 'mini.jpg';
slideimages[1] = new Image() 
slideimages[1].src = 'windows8.1.png';
slideimages[2] = new Image() 
slideimages[2].src = 'jw.jpg';
slideimages[3] = new Image() 
slideimages[3].src = 'aau.jpg';
slideimages[4] = new Image() 
slideimages[4].src = 'f7.jpg';
</script>

在正文标签中: -

<script>
var step = 0
var whichimage = 0

function slideit() {
    if (!document.images) return
    document.getElementById('slide').src = slideimages[step].src
    whichimage = step;
    setTimeout('slideit()', 2500);
    if (step < 4) {
        step++;
    } else {
        step = 0;
    }
}

function slidelink() {
    if (whichimage == 0) {
        window.open('movies.php#mini');
    }
    if (whichimage == 1) {
        window.open('software.php#win8.1');
    }
    if (whichimage == 2) {
        window.open('movies.php#jw');
    }
    if (whichimage == 3) {
        window.open('movies.php#aau');
    }
    if (whichimage == 4) {
        window.open('movies.php#f7');
    }
    slideit(); 
</script>  
<a href='javascript:slidelink()'><img src='mini.jpg' id='slide' width='640px' height='640px' /></a>

1 个答案:

答案 0 :(得分:0)

您的问题是您从未关闭slidelink函数,这会导致错误并使您的代码无效。 在函数末尾添加},它应该可以工作:

function slidelink() {
    if (whichimage == 0) {
        window.open('movies.php#mini');
    }
    if (whichimage == 1) {
        window.open('software.php#win8.1');
    }
    if (whichimage == 2) {
        window.open('movies.php#jw');
    }
    if (whichimage == 3) {
        window.open('movies.php#aau');
    }
    if (whichimage == 4) {
        window.open('movies.php#f7');
    }
    slideit();
}

Fiddle

只需检查您的控制台即可。这样你就可以避免这些错误/问题。