简体   繁体   English

JavaScript单击带有按钮导航的图像

[英]JavaScript Click images with button navigation

New to web development and trying to create a gallery of images that cycle through with the click of a "previous" and "next" button. Web开发的新功能,尝试创建单击“上一个”和“下一个”按钮循环显示的图像库。 Does anybody know how to do this? 有人知道怎么做这个吗? I don't think I've done anything right but I'll include what I've done so far. 我认为我没有做对任何事情,但是我将包括到目前为止所做的事情。 The idea is to make this adaptable to n indefinite number of images. 想法是使它适应于n个不确定数量的图像。

code: 码:

 <img src= "photos/1.jpg" id="currentImage"  height="288"/>
 <button id= "prev" onclick="prevImage()" class="portfolioNavigation">Previous</button>
 <button id= "next" onclick="nextImage()" class="portfolioNavigation">Next</button>
     <script type= "text/javascript">
   var counter = 2;
   var 1 = "photos/1.jpg";
   var 2 = "photos/2.jpg";
   var 3 = "photos/3.jpg";

prev.onclick = function(){
        document.getElementById("currentImage").src = counter - 1;
        counter--;
    }
next.onclick = function(){
        document.getElementById("currentImage").src = counter + 1;
        counter++;
    }

if(counter == 3){
        counter = 0;
    };
</script>

There is no need of using "onclick" inside button tags. 不需要在按钮标签内使用“ onclick”。

<script type= "text/javascript">
   var counter = 2;
   var sourceUrl = "photos/" + counter + ".jpg";
   var prev = document.getElementById("prev");
   var next = document.getElementById("next");

    prev.onclick = function(){
        counter--;
        document.getElementById("currentImage").src = sourceUrl;    
    }
    next.onclick = function(){
        counter++;
        document.getElementById("currentImage").src = sourceUrl;        }

    if(counter == 3){
        counter = 0;
    };
</script>

There are couple of issues in your code right now. 您的代码中现在有几个问题。 One of them is that your variables names are plain numbers. 其中之一是您的变量名称是纯数字。 You cannot have variable names starting with numbers in JavaScript. 在JavaScript中,变量名不能以数字开头。 Although not a great example, _1 is valid, image1 is a valid and even a more descriptive variable number. 尽管不是一个很好的例子,但是_1是有效的,而image1是有效的,甚至是更具描述性的变量号。

Another issue is, when you are setting the src attribute of currentImage you are also setting that attribute to a value of plain number. 另一个问题是,当您设置currentImagesrc属性时,您也将该属性设置为纯数字值。 That is not a valid image url. 这不是有效的图片网址。

You can put the photo URLs in an array and cycle through those images when next and previous buttons are pressed. 您可以将照片URL放入一个数组中,并在按下下一个和上一个按钮时循环浏览这些图像。

Try something like this (warning untested code): 尝试这样的事情(警告未经测试的代码):

var images = ["photos/1.jpg", "photos/2.jpg", "photos/3.jpg"];
var numImages = images.length;

prev.onclick = function(){
    document.getElementById("currentImage").src = images[(counter - 1) % numImages];
    counter--;
}

The significance of the % operator here is that it wraps around the value of [0, numImages). 这里的%运算符的意义在于它包装了[0,numImages)的值。

As others have posted, you cannot use an int as a variable name. 正如其他人所发表的那样,您不能将int用作变量名。

You also had "onclick" events on each button and events set via jQuery, you just need one or the other. 您还通过jQuery在每个按钮上设置了“ onclick”事件,并通过jQuery设置了事件,您只需要其中一个即可。

Storing each src string in an array and accessing it by using "counter" will work for you. 将每个src字符串存储在数组中并使用“计数器”访问它将为您工作。 It will also allow you to use a variable amount of images. 它还将允许您使用数量可变的图像。

HTML HTML

 <img src= "photos/1.jpg" id="currentImage"  height="288"/>
 <button id= "prev" class="portfolioNavigation">Previous</button>
 <button id= "next" class="portfolioNavigation">Next</button>

JavaScript JavaScript的

var counter = 0;
   var srcArray = ["photos/1.jpg", "photos/2.jpg", "photos/3.jpg"];

prev.onclick = function(){
        document.getElementById("currentImage").src = srcArray[--counter];
    }
next.onclick = function(){
        document.getElementById("currentImage").src = srcArray[++counter];
    }

if(counter == 3){
        counter = 0;
    };

If you inspect the image element in this JSFiddle , you'll see the src updating correctly. 如果您在此JSFiddle中检查image元素,则会看到src正确更新。

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

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