【问题标题】:JavaScript Image Gallery 2JavaScript 图片库 2
【发布时间】:2017-10-28 23:02:52
【问题描述】:

我不知道以前是否有人问过这个问题。但是……

我的 HTML 文件中有以下 JavaScript 代码:

<img id="imageDisplay">

<script type="text/javascript">
   var displayImage = document.querySelector( '#imageDisplay' );

   var imageArray = [ 'http://www.joongcho.com/Images/30th-Bday-Party-01.jpg',
                      'http://www.joongcho.com/Images/30th-Bday-Party-02.jpg',
                      'http://www.joongcho.com/Images/30th-Bday-Party-03.jpg',
                      'http://www.joongcho.com/Images/30th-Bday-Party-04.jpg',
                      'http://www.joongcho.com/Images/30th-Bday-Party-05.jpg',
                      'http://www.joongcho.com/Images/30th-Bday-Party-06.jpg' ];
   var imageCount = 0;
   var imageLength = imageArray.length;

   displayImage.setAttribute( 'src', imageArray[ imageCount ] );

   function nextImage(imageCount) {
   }

   function previousImage(imageCount) {
   }
</script>

我正在尝试为此 HTML 放置下一个图像和上一个图像函数。

对于下一个图像,如果有超过 1 个图像并且该图像不是最后一个图像,那么应该有一个链接,允许用户单击数组向前。此外,如果图像是数组中的最后一个图像,则隐藏链接。

对于前一个图像,如果该图像不是数组的第一个图像,那么应该有一个链接允许用户向后单击数组。如果图像是第一张图像,则应隐藏上一个链接。

感谢任何帮助。

【问题讨论】:

    标签: javascript html arrays image image-gallery


    【解决方案1】:

    您必须将事件侦听器附加到按钮或链接(我使用按钮):

    var displayImage = document.getElementById('imageDisplay');
    var buttonPrev = document.getElementById('button-prev');
    var buttonNext = document.getElementById('button-next');
    
    var imageArray = [ '../Images/30th-Bday-Party-01.jpg',
                          '../Images/30th-Bday-Party-02.jpg',
                          '../Images/30th-Bday-Party-03.jpg',
                          '../Images/30th-Bday-Party-04.jpg',
                          '../Images/30th-Bday-Party-05.jpg',
                          '../Images/30th-Bday-Party-06.jpg' ];
    var imageCount = 0;
    var imageLength = imageArray.length;
    
    function nextImage () {
      imageCount++;
      if (imageCount >= imageLength - 1) {
        buttonNext.style.display = 'none';
        imageCount = imageLength - 1;
      } 
      if (imageCount < imageLength) {
        displayImage.setAttribute('src', imageArray[imageCount]);
      }
      if (imageCount > 0) {
        buttonPrev.style.display = 'inline';
      }
    }
    
    function previousImage () {
      imageCount--;
      if (imageCount <= 0) {
        buttonPrev.style.display = 'none';
        imageCount = 0;
      } 
      if (imageCount >= 0) {
        displayImage.setAttribute('src', imageArray[imageCount]);
      }
      if (imageCount < imageLength) {
        buttonNext.style.display = 'inline';
      }
    }
    
    displayImage.setAttribute('src', imageArray[imageCount]);
    displayImage.textContent = imageCount;
    buttonPrev.style.display = 'none';
    buttonNext.addEventListener('click', nextImage);
    buttonPrev.addEventListener('click', previousImage);
    <img id="imageDisplay" />
    <button id="button-prev">Back</button>
    <button id="button-next">Forward</button>

    当然,你还不能看图片。

    【讨论】:

    • 当我在我的一端运行此代码时,它工作正常,但是当您单击上一个按钮时,下一个按钮将完全消失,没有机会再次前进。另外,您不能转到我这边的第一张图片。此外,您可以将以下内容添加到图像部分:'joongcho.com/Images' 以查看它的工作原理。谢谢。
    • 现在可以进行一些调整,但它不适用于所有浏览器。例如,它在 Google Chrome 中运行良好,但由于某种原因,它在 IE 11+ 中无法正常运行。下一个按钮可以正常工作,但上一个按钮不会显示在 Internet Explorer 中。 [链接]joongcho.com/downloads/random-a.htm[/link] 就是一个例子。谢谢。
    • 是的,你是对的。好像 IE 不支持initial。我编辑了我的答案,现在应该可以了。
    • 在上面的例子中,我可以使用超链接作为按钮而不是使用按钮作为导航来显示下一张/上一张图片吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多