【问题标题】:Previous function not working以前的功能不起作用
【发布时间】:2016-04-17 18:56:56
【问题描述】:

我正在制作一个 javascript 幻灯片,但我不确定如何为每个图像添加标题,上一个按钮无法正常工作(两次或多次单击后不显示图像)我也将如何能够转到幻灯片的第一个和最后一个图像?

var index=4;
var titles=[1,2,3,4,5];


// LIST OF CAPTİONS
caption[0] = "Caption for the first image";
caption[1] = "Caption for the second image";
caption[2] = "Caption for the third image";
caption[3] = "Caption for the first image";
caption[4] = "Caption for the second image";
caption[5] = "Caption for the third image";

function NextSlide()
{
    if (index >= 5){index=0}
    var img = document.getElementById("img1");
    var slideName="images/img" + titles[index++] + ".jpg";
    img.src=slideName;
}

function PrevSlide()
{
    if (index >= 5){index=0}
    var img = document.getElementById("img1");
    var slideName="images/img" + titles[index--] + ".jpg"; img.src=slideName;
}
function last()
{
    index = 1;
}

【问题讨论】:

  • caption 定义在哪里?
  • 当您致电 PrevSlide 时,您永远不会检查您是否小于零。

标签: javascript html arrays slideshow


【解决方案1】:

下面的代码给出了一个示例,您可以如何实现 first() 和 last() 方法以及更改标题。您尚未提供任何 HTML,因此可能需要对其进行测试/调整以适应您所拥有的内容(例如,您需要添加 spanp 标记以包含 ID 为 caption1 的标题)

var index = 3;   // index for forth image as it's zero based

// List of captions
var captions  = ["Caption for the first image",
                 "Caption for the second image",
                 "Caption for the third image",
                 "Caption for the forth image",
                 "Caption for the fifth image",
                 "Caption for the sixth image"];

var slideShowImg = document.getElementById("img1");
var slideShowCaption = document.getElementById("caption1");

// Show current image
function showCurrentImage(){
    var slideName = "images/img" + (index + 1) + ".jpg";
    slideShowImg.src = slideName;
    slideShowCaption.innerText = captions[index];
}

// Move to next slide
function nextSlide()
{
    index++;
    if (index >= captions.length){ index = 0; }

    showCurrentImage();
}

// Move to previous slide
function prevSlide()
{
    index--;
    if (index < 0){ index = captions.length - 1; }

    showCurrentImage();
}

// Move to last slide
function last()
{
    index = captions.length - 1;

    showCurrentImage();
}

// Move to first slide
function first()
{
    index = 0;

    showCurrentImage();
}

HTML 可能类似于...

<div class="slide-show">
  <img src="#" id="img1" alt="" />
  <span id="caption1">Caption</span>
</div>

注意事项:

  • 您的变量 titles 只是在索引中添加了一个,所以我删除了它并添加了内联。

  • 我将字幕移到了一行中,而不是一个一个地定义。

  • 将 getElementById("img1") 移出方法,因为它每次都会重复自己

  • 创建 showCurrentImage() 来对页面进行更改,所有其他方法只需更改索引然后调用它

  • 修复了 prevSlide() 中第一项的测试(看起来像是从 nextSlide() 复制而来的

【讨论】:

  • 感谢您的建议,我的 HTML 代码是这样的 '

    ' 但是显示图像的最佳方式是什么?
  • 不确定您的问题是什么?您已经通过img 标签显示正常的图像,不是吗?如果您的意思是从视觉上查看其他幻灯片库的工作原理,特别是诸如预加载下一张图像和从一张图像到下一张图像的淡入淡出过渡。
  • 嗯,我在不同的浏览器中查看它以及上传页面时遇到问题。这是“slideShowImg.src = slideName;”部分,我得到“Uncaught TypeError: cannot set property 'src' of null”
  • 这意味着slideShowImg 为空,这意味着它无法在 DOM 中找到图像。怀疑您只是在某处的代码中有错字。检查 IMG 标签和 getElementById() 部分上的 id 以确保它们看起来正确。否则,您可能需要发布代码链接或提出其他问题。
  • 我检查了它,我可以看到它有任何问题。 var slideShowImg = document.getElementById("img1"); var slideShowCaption = document.getElementById("caption1"); // Show current image function showCurrentImage(){ var slideName = "images/img" + (index + 1) + ".jpg"; slideShowImg.src = slideName; slideShowCaption.innerText = captions[index]; }' and heres the HTML
    字幕
    `
猜你喜欢
  • 2014-04-17
  • 1970-01-01
  • 2012-12-28
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2021-08-27
  • 2013-01-31
相关资源
最近更新 更多