【问题标题】:Using previous and next buttons to cycle through images in javascript使用上一个和下一个按钮循环浏览 javascript 中的图像
【发布时间】:2017-09-25 19:40:03
【问题描述】:

我正在使用 Javascript 编写一个小型移动应用程序。

我正在调用一个远程 API,该 API 响应一些 JSON 格式的数据。在该数据中是我需要在我的应用程序中显示的产品图像的 URL。我已经将 JSON 解析为一个 JS 数组,现在有了一个包含所有图像 URL 的数组。当我第一次加载我的应用程序时,我有一个画布,用于显示数组中的第一张图像。我有两个按钮,一个上一个和一个下一个。我希望能够通过使用数组中的 URL 在画布上绘制来使用这些按钮循环浏览图像。

有什么想法吗?

【问题讨论】:

  • 也发布网址代码。

标签: javascript jquery arrays json image


【解决方案1】:

您正在寻找类似以下的内容吗?

  // Image URLs
  var imageUrls = ['http://emojipedia-us.s3.amazonaws.com/cache/8b/bd/8bbd3405b0a197214e229428c23dbe60.png', 'http://emojipedia-us.s3.amazonaws.com/cache/05/d1/05d1ba284ee1a3bfe4e0f68988baafb9.png', 'http://emojipedia-us.s3.amazonaws.com/cache/99/4c/994c5997c7a509703cc53ec2000bb258.png', 'http://emojipedia-us.s3.amazonaws.com/cache/59/0c/590c832e73a472c416bf9d8bfdd02a4a.png'];

  // Keep track of the index of the image URL in the array above
  var imageShownIndex = 0;

  // Create a canvas
  var canvas = document.createElement('canvas');
  var canvasContext = canvas.getContext('2d');
  canvas.height = 150;
  canvas.width = 150;

  // Create a button that will load the previous image on the canvas when clicked
  var previousButton = document.createElement('button');
  previousButton.innerHTML = 'Previous Image';
  previousButton.onclick = function () {
      // Show images in a cycle, so when you get to the beginning of the array, you loop back to the end
      imageShownIndex = (imageShownIndex==0) ? imageUrls.length-1 : imageShownIndex-1;
      updateImage();
   };

  // Do same for the next button
  var nextButton = document.createElement('button');
  nextButton.innerHTML = 'Next Image';
  nextButton.onclick = function () {
    imageShownIndex = (imageShownIndex == imageUrls.length-1) ? 0 : imageShownIndex+1;
    updateImage();
   };

  document.body.appendChild(previousButton);
  document.body.appendChild(canvas);
  document.body.appendChild(nextButton);
  
  // Show the first image without requiring a click
  updateImage();

  function updateImage() {

    // Create the Image object, using the URL from the array as the source
    // You could pre-load all the images and store them in the array, rather than loading each image de novo on a click
    var img = new Image();
    img.src = imageUrls[imageShownIndex];

    // Clear the canvas
    canvasContext.clearRect(0, 0, 150, 150);

    // After the image has loaded, draw the image on the canvas
    img.onload = function() {
      canvasContext.drawImage(img, 0, 0);
    }

  }

编辑:或者如果您已经制作了 HTML 元素,您可以使用 JavaScript 来控制画布。例如:

<html>
  <body>
    <button id="previous_button" onclick="goToPreviousImage()">Previous Image</button>
    <canvas id="image_canvas" height=150, width=150></canvas>
    <button id="next_button" onclick="goToNextImage()">Next Image</button>
  </body>

  <script>
    var imageUrls = ['http://emojipedia-us.s3.amazonaws.com/cache/8b/bd/8bbd3405b0a197214e229428c23dbe60.png', 'http://emojipedia-us.s3.amazonaws.com/cache/05/d1/05d1ba284ee1a3bfe4e0f68988baafb9.png', 'http://emojipedia-us.s3.amazonaws.com/cache/99/4c/994c5997c7a509703cc53ec2000bb258.png', 'http://emojipedia-us.s3.amazonaws.com/cache/59/0c/590c832e73a472c416bf9d8bfdd02a4a.png'];

    var imageShownIndex = 0;

    var canvas = document.getElementById('image_canvas');
    var canvasContext = canvas.getContext('2d');

    function goToPreviousImage() {
        imageShownIndex = (imageShownIndex==0) ? imageUrls.length-1 : imageShownIndex-1;
        updateImage();
     };

    function goToNextImage() {
      imageShownIndex = (imageShownIndex == imageUrls.length-1) ? 0 : imageShownIndex+1;
      updateImage();
     };
    
    updateImage();

    function updateImage() {

      var img = new Image();
      img.src = imageUrls[imageShownIndex];

      canvasContext.clearRect(0, 0, 150, 150);

      img.onload = function() {
        canvasContext.drawImage(img, 0, 0);
      }

    }

  </script>
</html>

【讨论】:

  • 太棒了!谢谢!
  • 我实际上已经在一个单独的 HTML 文件中创建了按钮和画布,所以我需要对其进行调整,以便它只是一个在单击按钮时调用的 JavaScript 函数。我对 JavaScript 很陌生,但我应该能够理解它。这样做有可能吗?
  • 是的。我已经更新了答案以提供说明。您需要将 onclick 事件处理程序分配给按钮。在这里它是在 html 中完成的,但您也可以通过 JavaScript 来完成。您还需要在 JavaScript 中检索 canvas 元素——这里由 document.getElementById 完成。
  • @BIGJOHN - 不要忘记▲ Upvote任何有用的答案,这会奖励作者加上您的反馈形式投票有利于整个Stack Overflow 社区!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多