jQuery图像旋转器

时间:2010-07-13 16:48:34

标签: jquery

我正在尝试制作这个简单的图像旋转器但是,像往常一样,我的代码不起作用。我想在2张图片之间交换(“image1”和“image2”)

$x = 0;
function rotateImage(){
    if($x == 0){  //if function is being called for the first time
        $x = 1;
    }
    $(".window").html("<img src=\"../images/image" + $x + ".png\" />");
    $x = $x + 1;
    if($x > 2)  
        $x = 1;  //reset x to 1 if last image has been reached
}

rotateImage();  //first function call
setInterval("rotateImage()", 1000);  //call function every second

Image1显示但没有进行交换。

提前致谢,

马特

1 个答案:

答案 0 :(得分:2)

你不需要javascript中的变量美元符号,x作为整数不能很好地添加字符串。试试这个,如果您有任何问题,请告诉我:

var x = '0'; 
function rotateImage(){ 
    if (x == '0') { x = '1'; } else { x = '0'; }
    $(".window").html("<img src=\"../images/image" + x + ".png\" />"); 
} 

rotateImage();  //first function call 
setInterval("rotateImage()", 1000);  //call function every second 

另外..你确定“.window”是用于图像容器的正确选择器吗?我建议在带有ID的DIV中使用“#theID”作为选择器。

相关问题