當您單擊縮略圖時,它會顯示更大的圖片,然後您可以使用箭頭鍵(左和右)更改上一張/下一張圖片。箭頭鍵更改圖片Javascript
我不知道如何編碼右箭頭鍵以避免NaN值。
當您按住右箭頭鍵時,它將輸出NaN值。我怎樣才能避免這種情況?我怎麼能期望值1,2,3,4 ....當我繼續按下右箭頭鍵?這裏是我的HTML代碼
<div class="col-md-4 col-xs-6 thumb"> <a class="thumbnail" href="#" data-image-id="5" data-toggle="modal" data-title="Breast Revision Implant Exchange" data-caption="Before and After: Previous breast implants removed and exchanged with larger smooth round silcone implants followed by liposuction of the armpit/axillary and side of chest area" data-image="http://www.afbplasticsurgery.com/before-after-images/breast-revision-lateral-view-b-a.jpg" data-target="#image-gallery">
<div class="ba-thumb">
<table>
<tbody>
<tr>
<td><img class="ba-thumbnail" src="http://www.afbplasticsurgery.com/before-after-images/thumb/breast-revision-b-lateral-view-150.jpg"></td>
<td><img class="ba-thumbnail" src="http://www.afbplasticsurgery.com/before-after-images/thumb/breast-revision-a-lateral-view-150.jpg"></td>
</tr>
<tr>
<td class="ba-note" colspan="2">Breast Revision Implant Exchange</td>
</tr>
</tbody>
</table>
</div>
</a></div>
<div class="clr"></div>
<!-- end pic element -->
<div class="modal fade" id="image-gallery" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="image-gallery-title"></h4>
</div>
<div class="modal-body">
<center>
<img id="image-gallery-image" class="img-responsive" src="">
</center>
<div class="modal-body-button">
<div class="col-md-6">
<span id="show-previous-image" class="previous-page"></span>
</div>
<div class="col-md-6">
<span class="pull-right">
<span id="show-next-image" class="next-page"></span>
</span>
</div>
</div>
</div>
<div class="modal-footer">
<div class="text-justify" id="image-gallery-caption">This text will be overwritten by jQuery </div>
</div>
</div>
</div>
</div>
這裏是我的Javascript
$(document).ready(function() {
loadGallery(true, 'a.thumbnail');
function disableButtons(counter_max, counter_current) {
$('#show-previous-image, #show-next-image').show();
if (counter_max == counter_current) {
$('#show-next-image').hide();
} else if (counter_current == 1) {
$('#show-previous-image').hide();
}
}
function loadGallery(setIDs, setClickAttr) {
var current_image, selector, counter = 0;
$(document).keydown(function(e) {
var code = e.keyCode || e.which;
switch (code) {
case 37:
if (current_image == 1) {
current_image = 1;
} else {
current_image--;
}
console.log(current_image);
break;
case 39:
current_image++;
console.log(current_image);
break;
default:
return;
}
selector = $('[data-image-id="' + current_image + '"]');
updateGallery(selector);
e.preventDefault();
});
$('#show-next-image, #show-previous-image').click(function() {
if ($(this).attr('id') == 'show-previous-image') {
current_image--;
} else {
current_image++;
}
selector = $('[data-image-id="' + current_image + '"]');
updateGallery(selector);
console.log(selector);
});
function updateGallery(selector) {
var $sel = selector;
current_image = $sel.data('image-id');
$('#image-gallery-caption').text($sel.data('caption'));
$('#image-gallery-title').text($sel.data('title'));
$('#image-gallery-image').attr('src', $sel.data('image'));
disableButtons(counter, $sel.data('image-id'));
}
if (setIDs == true) {
$('[data-image-id]').each(function() {
counter++;
$(this).attr('data-image-id', counter);
});
}
$(setClickAttr).on('click', function() {
updateGallery($(this));
});
}
});
或採取http://jsfiddle.net/8o0L4e2f/
你有沒有得到檢查了這一點? – epoch
我剛剛做了,它完美的作品非常感謝@epoch! – tamnguyen
你可以接受或upvote :) – epoch