简体   繁体   English

我的JavaScript图片库滑块的右滑块不起作用,而我的左滑块是

[英]My JavaScript image gallery slider's right slider isn't working and my left one is

I have an image gallery slider on my personal website (you can check it out here for a visual: http://www.alexmarvick.com/gallery.html ), written with the following HTML and Javascript: 我的个人网站上有一个图库滑块(您可以在此处查看图像的滑块: http : //www.alexmarvick.com/gallery.html ),它使用以下HTML和Javascript编写:

HTML (Uses fontawesome icons and images are in my directory) HTML(在我的目录中使用了超棒的图标和图像)

    <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 的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();

});

When you load the site at first, the left and right buttons work correctly. 最初加载站点时,左右按钮可以正常工作。 However, when you click on any random image in the gallery below, the right arrow becomes defunct but the left arrow still works just fine. 但是,当您单击下面的图库中的任何随机图像时,右箭头会失效,但左箭头仍然可以正常工作。 When I open up the console, I will get an error saying: 当打开控制台时,我会收到一条错误消息:

"image71.jpg:1 GET http://www.alexmarvick.com/image71.jpg 404 (Not Found)" “ image71.jpg:1 GET http://www.alexmarvick.com/image71.jpg 404(未找到)”

It appears that there is a parsing issue, as when the '#right-arrow'.click function is activated, imageNo behaves as a string (in this example, I clicked on the 7th image, then as soon as I clicked the right arrow it parsed '1' at the end of it as opposed to adding it to equal 8). 似乎存在解析问题,因为当激活#right-arrow'.click函数时,imageNo表现为字符串(在此示例中,我单击了第7张图像,然后单击了右箭头它在末尾解析为“ 1”,而不是将其等于8)。 It doesn't do this with the left arrow, however, and the code is identical for both the left and right. 但是,它不使用左箭头来执行此操作,并且左右代码均相同。

Try 尝试

imageNo = parseInt(imageNo) + 1;

instead of 代替

imageNo += 1;

in your $('#right-arrow').click() -handler. 在您的$('#right-arrow').click()程序中。

The problem is that imageNo is a string, while you're trying to use it as an int. 问题是imageNo是一个字符串,而您试图将其用作int。

EDIT : Or (I think a better option) you could just change the line 编辑 :或者(我认为是一个更好的选择),您可以只更改行

imageNo = $(this).attr("id");

to: 至:

imageNo = parseInt($(this).attr("id"));

from the $('.small-images').click() -handler. 来自$('.small-images').click()程序。

Before proposing an actual answer i'd like to make a few suggestion : 在提出实际答案之前,我想提出一些建议:

  • you should make an array of objects (easier to maintain and add things in or remove some) 您应该制作一个对象数组(更易于维护和添加或删除某些对象)

  • you should really work something with the % operator to change images ( -1%x is equal to x-1 ) 您应该真正使用%运算符来更改图像( -1%x等于x-1

  • when you have multiple else if and condition is always in regard of value you must use a switch or else code will enlarge in a very useless way 当您有多个else if和condition始终是关于值的条件时,则必须使用switch ,否则代码将以非常无用的方式扩大

  • starting from anything else then 0 is always trickier then actually starting from 0 (remember, quite everything starts indexing at 0) 从其他任何东西开始,然后0总是比从0开始更棘手(请记住,相当多的东西都从0开始索引)

So there we go for the answer (and a bit of rework to ease future work on it): 因此,我们找到了答案(并做了一些重做以简化以后的工作):

First we need a constructor function (easiest way with objects) : 首先,我们需要一个构造函数(使用对象的最简单方法):

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*/

}

Then we'll use an array of those objects to ease control : 然后,我们将使用这些对象的数组来简化控制:

var gallery=[];

You do need to keep in mind that arrays start indexing at 0 but if you really want to start at 1 you can still assign a "nullified" object as the first object as follows: 您确实需要记住,数组从0开始索引,但是如果您真的想从1开始,您仍然可以将“空”对象分配为第一个对象,如下所示:

gallery.push( new GallerySlide() );

Then each time you add an image (if you keep the pattern : index 1 : image1.png) you can use : 然后,每次添加图像(如果保留模式: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*/

Then we need a global variable to hold the current gallery position : 然后,我们需要一个全局变量来保持当前画廊位置:

var gallery_index = 1;/*only set this after all images are loaded in array*/

Now here's the fun part, the reworked left arrow (you can deduce right arrow and initialization from it) : 现在这是有趣的部分,重新​​设计的左箭头(您可以推断出右箭头并从中进行初始化):

$('#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: initialize right after assigning 1 to gallery_index SIDENOTE: 在为gallery_index分配1之后 立即 初始化

Now that we have left and right and initialization, let's give a look at click on a little image : 现在我们已经有了左右和初始化,让我们看一下单击一个小图像:

$('.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);
}

There you go, you should try it out (maybe you'll encounter a need of debug but I'm pretty confident) and all in all I think it's way more flexible in terms of adding new images and all this stuff. 在这里,您应该尝试一下(也许您会遇到调试的需要,但是我很有信心),总的来说,我认为在添加新图像和所有这些方面它更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM