添加到现有的旧幻灯片脚本以包含图像标题

时间:2014-03-10 05:51:05

标签: javascript html arrays slideshow caption

我想知道我是否可以对现有脚本进行少量更改或添加,为幻灯片中的图像添加标题,或者我是否应该放弃这个旧脚本并重新开始。所以有两个页面 - 介绍页面和幻灯片页面。介绍页面包含小缩略图图像,点击后将访问者带到幻灯片页面以查看缩略图的大版本,访问者可以从中点击所有大型缩略图代表图像或返回介绍页面。 / p>

这是介绍页面(相关部分):

<table>
   <tr>
    <td><a href="arbor-viewer-test-files.html?img=0">
     <img src="images/thumbnails/arbor_0.jpg" width="100" />
     </a></td>
    <td><a href="arbor-viewer-test-files.html?img=1">
     <img src="images/thumbnails/arbor_1.jpg" width="100" />
     </a></td>
    <td><a href="arbor-viewer-test-files.html?img=2">
     <img src="images/thumbnails/arbor_2.jpg" width="100" />
     </a></td>
    <td><a href="arbor-viewer-test-files.html?img=3">
     <img src="images/thumbnails/arbor_3.jpg" width="100" />
     </a></td>
    <td><a href="arbor-viewer-test-files.html?img=4">
     <img src="images/thumbnails/arbor_4.jpg" alt="DFW Patio Cover Arbors" border="0" width="100" />
     </a></td>
   </tr>
  </table>

这是幻灯片页面(相关部分):

<table border="0">
          <tr>
            <td align="center">
              <p class="center largeP bold"> <a href="javascript:chgImg(-1)">Previous</a> | <a href="javascript:auto()">Auto/Stop</a> | <a href="javascript:chgImg(1)">Next</a> <br />
                <a href="/arbors.htm">Return to the Arbor Gallery</a> </p>
              <br />
            </td>
          </tr>
          <tr>
            <td align="center"> <br />
              <img src="images/arbor_0.jpg" name="slideshow" />
              <br />
            </td>
          </tr>
        </table>
<script type="text/javascript">
showImg(queryString.img);
</script>
</body>

这是剧本:

var newimg = new Array();
newimg[0] = "images/arbor_0.jpg";
newimg[1] = "images/arbor_1.jpg";
newimg[2] = "images/arbor_2.jpg";
newimg[3] = "images/arbor_3.jpg";
newimg[4] = "images/arbor_4.jpg";

function obj_queryString(){
qs = document.URL.substring(document.URL.indexOf('?')+1,document.URL.length);
queries = qs.split(/\&/);
for(i=0;i<queries.length;i++){ 
    query = queries[i].split(/\=/);
    this[query[0]] = unescape(query[1]);}
 }

queryString = new obj_queryString();
var imgnum = 0;
var imglength = newimg.length - 1;
//delay betwn slides in millisecs
var delay = 3000;
var lock = false;
var run;

function showImg(imgnum){
document.slideshow.src = newimg[imgnum];
}

function chgImg(direction) {
if (document.images){
    imgnum = imgnum + direction;
    if (imgnum > imglength) 
        imgnum = 0;
    if (imgnum < 0) 
        imgnum = imglength;
    document.slideshow.src = newimg[imgnum];}
}

function auto(){
if (lock == true) {
    lock = false;
    window.clearInterval(run);}
else if (lock == false) {
    lock = true;
    run = setInterval("chgImg(1)", delay);}
 }

我能想到的最好的就是将其添加到脚本中:

//under the newimg Array:
var caption = new Array();  
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 fourth image"; 
caption[4] = "Caption for the fifth image";

在幻灯片页面的底部添加一个片段以及该功能(其他一切与上面相同):

<p id="captionDiv><br /></p>
<script type="text/javascript">
showImg(queryString.img);
document.getElementById("captionDiv").innerHTML=caption[imgnum];
</script>

...只显示标题[0]并且不随图像一起变化。对于那些比我更了解javascript的人来说,我确信这个解决方案是明智之举。我已经在网上研究和研究了一个多星期,但仍然不明白该怎么做。我需要一个javascript解释器!

1 个答案:

答案 0 :(得分:0)

您可以将document.getElementById("captionDiv").innerHTML=caption[imgnum];放在function showImg(imgnum)内,以便获得相关的图片标题。