我想使用键盘和鼠标导航图像(单击左右箭头图像)。
我正在使用 Jquery 来执行此操作,但共享的 imgIndex 似乎与 .keydown 函数和 .click 函数无关...每当 .keydown 函数或 ++ imgIndex 时,不是也使用了更改的索引在点击功能?那么它们不应该总是在同一个索引上吗?
按键功能:
<script type="text/javascript">
var imgArray = [<?php echo implode(',', getImages($site)) ?>];
$(document).ready(function() {
var img = document.getElementById("showimg");
img.src = imgArray[<?php echo $imgid ?>];
var imgIndex = <?php echo $imgid ?>;
alert(imgIndex);
$(document).keydown(function (e) {
var key = e.which;
var rightarrow = 39;
var leftarrow = 37;
var random = 82;
if (key == rightarrow)
{
imgIndex++;
if (imgIndex > imgArray.length-1)
{
imgIndex = 0;
}
img.src = imgArray[imgIndex];
}
if (key == leftarrow)
{
if (imgIndex == 0)
{
imgIndex = imgArray.length;
}
img.src = imgArray[--imgIndex];
}
});
点击功能:连接左右可点击图片
$("#next").click(function() {
imgIndex++;
if (imgIndex > imgArray.length-1)
{
imgIndex = 0;
}
img.src = imgArray[imgIndex];
});
$("#prev").click(function() {
if (imgIndex == 0)
{
imgIndex = imgArray.length;
}
img.src = imgArray[--imgIndex];
});
});
只是为了让您对 getImages php 函数有一些了解:
<?php
function getImages($siteParam) {
include 'dbconnect.php';
if ($siteParam == 'artwork') {
$table = "artwork";
}
else {
$table = "comics";
}
$catResult = $mysqli->query("SELECT id, title, path, thumb, views, catidFK FROM $table");
$img = array();
while($row = $catResult->fetch_assoc())
{
$img[] = "'" . $row['path'] . "'";
}
return $img;
}
?>
非常感激!
脚本在“view image.php”上的快照