你好我正在尝试创建一个在线的photalbum。因此,我们的想法是拥有一张图片和两个带有onclick事件的元素来加载相册中的下一张和上一张图片。 我为此创建了一个函数,但它只能运行一次。如果按下上一个或下一个按钮,则执行onclick,但onclick属性不会更改,并且一个简单的警告框告诉我第二次不执行代码。
我很抱歉我的代码中的荷兰语foto应该是Photo!
我的HTML看起来像这样。
<div id="fotolarge">
<span class="arrow arrowright"></span>
<span class="arrow arrowleft"></span>
<span id="fotoloading"></span>
<img id="foto"/>
</div>
我的javascript看起来像这样
function loadPhoto(src){
$("#fotoloading").css("z-index","3");
$currentIndex = jQuery.inArray(src, $photoArray);
//next image
$nextIndex = $currentIndex + 1;
if($nextIndex < $photoArrayLength){
$(".arrowright").attr("onclick", "loadPhoto('" + $photoArray[$nextIndex] + "')");
}
else{
$(".arrowright").attr("onclick", "loadPhoto('" + $photoArray[0] + "')");
}
//previous image
$previousIndex = $currentIndex - 1;
if($previousIndex >= 0){
$(".arrowleft").attr("onclick", "loadPhoto('" + $photoArray[$previousIndex] + "')");
}
else{
$(".arrowleft").attr("onclick", "loadPhoto('" + $photoArray[$photoArrayLength -1] + "')");
}
var loader = new ImageLoader(src);
loader.loadEvent = function(url, image){
$("#fotoloading").css("z-index","2");
$("#foto").replaceWith(image);
$("#foto").css("display", "block");
calcmiddle();
}
loader.load();
}
变量$ photoArray和$ photoArrayLength是全局变量。 $ photArray包含图像源的列表。 Imageloader不是问题所在,因为它不起作用,那部分是注释掉的。
我希望有人知道如何解决这个问题。我试过像这样的jquery .click()
$(".arrowright").click(function(){
loadPhoto($photoArray[0]);
});
然后它部分工作。它在执行loadPhoto();
的无限循环中创建答案 0 :(得分:0)
您最好使用click事件来判断您要去哪个方向(1或-1)并跟踪当前位置,而不是每次都重写click事件。