上一个函数不起作用
Previous function not working
我正在制作一个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;
}
下面的代码给出了如何实现first()和last()方法以及更改标题的示例。您还没有提供任何HTML,因此可能需要对其进行测试/调整以适应您所拥有的内容(例如,您需要添加span
或p
标签来包含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(复制的)
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- Ember Data DS.Model's set函数不起作用
- Javascript:If-then语句在函数中不起作用
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- AngularJS指令部分应用的函数don'不起作用
- 为什么我的JavaScript堆栈排序函数不起作用
- 嵌套到另一个函数中的Fancybox函数;不起作用
- Javascript onchange()函数不起作用
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- javascript函数调用不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- 从PHP调用JS函数不起作用
- 清除函数中if语句内部不起作用的间隔
- js函数堆栈传入变量,.hide()不起作用
- jQuery validate函数不起作用
- jQuery克隆函数在chrome中不起作用
- JS-窗口宽度函数不起作用
- 函数调用不起作用
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- javascript函数,该函数不起作用,但不会显示任何错误