你好,我正在嘗試創建一個在線照片集。因此,這個想法是有一個圖片和兩個元素與onclick事件加載專輯中的下一個和前一個圖像。 我爲此創建了一個函數,但它只能使用一次。如果按下上一個或下一個按鈕,則會執行onclick,但onclick屬性不會更改,並且一個簡單的警報框會告訴我該代碼不會再次執行。jquery change onclick
我很抱歉在我的代碼foto中的荷蘭語應該是照片!
我的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()的無限循環中創建。
對於它的價值,已經有很好的插件可以爲你做這項工作。對於幻燈片:http://jquery.malsup.com/cycle/對於輪播:http://www.gmarwaha.com/jquery/jcarousellite/ – Blazemonger
你確定$ currentIndex設置爲某些東西? – Kreker
你可以給我完整的代碼與$ photoArray,$ photoArrayLength和ImageLoader,以便我可以做一個完整的工作示例? – Usman