我的个人网站上有一个图片库滑块(你可以在这里查看:http://www.alexmarvick.com/gallery.html),用以下HTML和Javascript编写:
HTML(使用fontawesome图标和图像在我的目录中)
<section class="body-home" id="body-gallery">
<div id="gallery-outline">
<img id="gallery-enlarged-image" src="image1.jpg">
<p id="caption">Study Abroad Trip in 2014: Weekend Trip to Paris, France. Missing it! February 2014</p>
<div id="gallery-scroll-selectors">
<i class="fa fa-arrow-circle-left" id="left-arrow" aria-hidden="true"></i>
<i class="fa fa-arrow-circle-right" id="right-arrow" aria-hidden="true"></i>
</div>
<div id="gallery-image-options-outline">
<li><img class="small-images" id="1" src="image1.jpg"></li>
<li><img class="small-images" id="2" src="image2.jpg"></li>
<li><img class="small-images" id="3" src="image3.jpg"></li>
<li><img class="small-images" id="4" src="image4.jpg"></li>
<li><img class="small-images" id="5" src="image5.jpg"></li>
<li><img class="small-images" id="6" src="image6.jpg"></li>
<li><img class="small-images" id="7" src="image7.jpg"></li>
<li><img class="small-images" id="8" src="image8.jpg"></li>
<li><img class="small-images" id="9" src="image9.jpg"></li>
</div>
</div>
</section>
的JavaScript
var caption = "";
var imageNo = 1;
var photoChange = function() {
if (imageNo == 0) {
imageNo = 1;
caption = "Study Abroad Trip in 2014: Weekend Trip to Paris, France. Missing it! February 2014";
$('#gallery-enlarged-image').attr("src", "image" + imageNo + ".jpg");
}
else if (imageNo == 10) {
imageNo = 9;
caption = "Standing out in front of Microsoft's campus, taken by a friend. August 2015";
$('#gallery-enlarged-image').attr("src", "image" + imageNo + ".jpg");
}
else if (imageNo == 1) {
caption = "Study Abroad Trip in Spring 2014: Weekend Trip to Paris, France. Missing it! February 2014";
}
else if (imageNo == 2) {
caption = "Our cat, Cosmo";
}
else if (imageNo == 3) {
caption = "Gonzaga University Graduation, May 2016";
}
else if (imageNo == 4) {
caption = "Gonzaga University Graduation Picture 2, May 2016";
}
else if (imageNo == 5) {
caption = "Me with my parents in Burch Bay, WA. June 2016";
}
else if (imageNo == 6) {
caption = "Me striking a pose with my Senior Design Project. April 2016";
}
else if (imageNo == 7) {
caption = "Our cat, Rudy";
}
else if (imageNo == 8) {
caption = "At the Orleans arena in Vegas watching Gonzaga Basketball take the WCC title. March 2016";
}
else if (imageNo == 9) {
caption = "Standing out in front of Microsoft's campus, taken by a friend. August 2015";
}
else {
caption = "No Image Here Yet";
}
$('#caption').html(caption);
};
$('#gallery-enlarged-image').hide();
$('#gallery-enlarged-image').fadeIn(500);
$('.small-images').click(function() {
var bigImageSrc = $(this).attr("src");
$('#gallery-enlarged-image').attr("src", bigImageSrc);
imageNo = $(this).attr("id");
$('#gallery-enlarged-image').hide();
$('#gallery-enlarged-image').fadeIn(500);
photoChange();
});
$('#left-arrow').click(function() {
$('#gallery-enlarged-image').hide();
imageNo -= 1;
$('#gallery-enlarged-image').attr("src", "image" + imageNo + ".jpg");
$('#gallery-enlarged-image').fadeIn(500);
photoChange();
});
$('#right-arrow').click(function() {
$('#gallery-enlarged-image').hide();
imageNo += 1;
$('#gallery-enlarged-image').attr("src", "image" + imageNo + ".jpg");
$('#gallery-enlarged-image').fadeIn(500);
photoChange();
});
首先加载网站时,左右按钮可正常工作。但是,当您单击下方图库中的任何随机图像时,右箭头将变为失效但左箭头仍可正常工作。当我打开控制台时,我会收到错误消息:
“image71.jpg:1 GET http://www.alexmarvick.com/image71.jpg 404(Not Found)”
似乎存在一个解析问题,因为当'#right-arrow'.click函数被激活时,imageNo表现为一个字符串(在这个例子中,我点击了第7个图像,然后我一点击了右箭头在其末尾解析为'1'而不是将其添加到等于8)。但是,左箭头不能执行此操作,左右两侧的代码相同。
答案 0 :(得分:1)
尝试
imageNo = parseInt(imageNo) + 1;
而不是
imageNo += 1;
在$('#right-arrow').click()
- 处理程序中。
问题是imageNo
是一个字符串,而你正试图将它用作int。
编辑:或者(我认为更好的选择)你可以改变这条线
imageNo = $(this).attr("id");
为:
imageNo = parseInt($(this).attr("id"));
来自$('.small-images').click()
-handler。
答案 1 :(得分:0)
在提出实际答案之前,我想提出一些建议:
你应该制作一个对象数组(更容易维护和添加内容或删除一些)
您应该使用%
运算符更改图像(-1%x
等于x-1
)
当您有多个else if
并且条件始终与价值有关时,您必须使用switch
,否则代码会以非常无用的方式放大
从其他任何地方开始,然后0总是比较复杂,然后实际从0开始(记住,一切都开始在0处开始索引)
所以,我们会找到答案(以及一些改进工作以简化未来的工作):
首先我们需要一个构造函数(对象最简单的方法):
function GallerySlide(url, caption){
this.url = url || "#";
this.caption = caption || "No image here yet";/*if you don't pass a caption it will assign the left operand of the OR*/
this.isEqualTo = function(obj){
return this.url == obj.url;
}/*for small image click*/
}
然后我们将使用这些对象的数组来简化控制:
var gallery=[];
你需要记住,数组开始索引为0,但如果你真的想从1开始,你仍然可以将“无效”对象指定为第一个对象,如下所示:
gallery.push( new GallerySlide() );
然后每次添加图像时(如果保留图案:index 1:image1.png),您可以使用:
gallery.push( new GallerySlide("image"+gallery.length+".jpg", /*caption goes here*/) );/*as the left adjacent spot will always be the index gallery.length*/
然后我们需要一个全局变量来保持当前的库位置:
var gallery_index = 1;/*only set this after all images are loaded in array*/
现在这是有趣的部分,重新设计的左箭头(你可以推导出右箭头和初始化):
$('#left-arrow').click(function(){
gallery_index-=1;
gallery_index%=gallery.length;
if(gallery_index == 0){
gallery_index = gallery_length-1;
}
$('#gallery-enlarged-image').fadeOut(500);/*way smoother look*/
$('#gallery-enlarged-image').attr("src",gallery[gallery_index].url);
$('#gallery-enlarged-image').fadeIn(500);
$('#caption').html(gallery[gallery_index].caption);
}
SIDENOTE: 在为gallery_index指定1后立即初始化
现在我们已经左右和初始化了,让我们看看点击一个小图片:
$('.small-images').click(function(){
var si = new GallerySlide( $(this).attr("src") );
var toChange = new GallerySlide();
for(var i = 1 ; i<gallery.length ; i++){
if( gallery[i].isEqualTo(si) ){
toChange = gallery[i];
gallery_index = i;
break;
}
}
$('#gallery-enlarged-image').fadeOut(500);/*the following seems like it could go in a function in which you always pass the array and the current index (or have them global to it)*/
$('#gallery-enlarged-image').attr("src",gallery[gallery_index].url);
$('#gallery-enlarged-image').fadeIn(500);
$('#caption').html(gallery[gallery_index].caption);
}
你去了,你应该尝试一下(也许你会遇到调试的需要,但我非常自信)总而言之,我认为在添加新图像和所有这些东西方面更灵活。