【问题标题】:Iterating through images using forward and back buttons使用前进和后退按钮遍历图像
【发布时间】:2012-11-20 22:32:17
【问题描述】:

我有五张图片要作为图库进行迭代。因此,当用户单击“前进按钮”时,数组中的下一个图像将显示在 div 中。以及单击“后退按钮”时。对不起,如果这是愚蠢的编码,但我在这里很新。

$(document).ready(function () {
    // when thumbnail is clicked

    $("img#thumb").click(function () {
        var imgpath = $(this).attr("src");
        $("img#cover").attr("src", imgpath);

    });

    $(function () {
        $("img#thumb").bind('click', function () {
            var img = $(this).clone(); //this clones the img, so you do not need to re-load that using src path
            $("img#cover").hide().html(img).fadeIn(500); //remove .hide() segment if you just are hiding by css rules

        });
    });

    //when the nextimage link is clicked
    var imgs = ["images/exteriors/abandonned/img0.jpg",
                "images/exteriors/abandonned/img1.jpg",
                "images/exteriors/abandonned/img2.jpg"];

    $("img#nextimage").click(function () {

        $.each(imgs, function () {

            $("img#cover").attr("src", imgs); // I want to iterate each image and display when it is clicked
        });

    });
});

HTML:

<div id="thumbs"> <!--gallery thumbs-->
<img id="thumb1" src="images/exteriors/abandonned/img0.jpg" width="100px" height="80px" class="" /><br>
<img id="thumb2" src="images/exteriors/abandonned/img1.jpg" width="100px" height="80px" class="" /><br>
<img id="thumb3" src="images/exteriors/abandonned/img2.jpg" width="100px" height="80px" class="" /><br>
 <img id="thumb" src="images/exteriors/abandonned/img3.jpg" width="100px" height="80px" class="" /><br>
 <img id="thumb" src="images/exteriors/abandonned/img3.jpg" width="100px" height="80px" class="" /><br>
 </div>


【问题讨论】:

    标签: jquery image gallery loops


    【解决方案1】:

    您无需在每次单击下一个或上一个按钮时迭代图像;相反,使用变量将索引存储到 imgs 数组中,并让您的点击处理程序更新该索引,然后使用数组中该索引处的 URL 更新标记的 src 属性。可能是这样的:

    var imgindex = 0;
    
    $('img#nextimage').click(function() {
      imgindex++; // increment the index into array 'imgs'
      if (imgindex > (imgs.length - 1)) { imgindex = 0; }; // if we just walked off the far end of the array, reset the index to zero (loop around to the 1st image)
      $('img#cover').attr('src', imgs[imgindex]); // update the img src
    });
    
    $('img#previmage').click(function() {
      imgindex--; // decrement the array index
      if (imgindex < 0) { imgindex = (images.length - 1); }; // if we just walked off the near end of the array, loop around to the last image
      $('img#cover').attr('src', imgs[imgindex]);
    });
    

    【讨论】:

    • 我注意到你有一个条件,如果索引大于数组的长度,循环会重置。我怎么知道我的阵列有多长?这是值中字符的长度吗?
    • 我按照你说的做了,而且有点效果。图像切换但它不会“粘住”它会翻转回上一个!它只是闪烁。
    • 我通过它的长度属性获取数组的长度——在上面的例子中是'imgs.length'。该属性存在于所有 JS 数组对象上,并包含一个数字,指示数组中有多少元素——因为数组的索引是从零开始的,所以从 length 属性中减去 1 会得到最后一个元素的索引。关于没有坚持的变化——我猜这与其他事件处理程序之一有关。我建议从您的代码中的 imgs 数组开始,然后是我的答案,然后从那里构建——我无法从我在这里看到的内容中判断出什么可能是错误的。
    • 好的,非常感谢!您提供的建议非常有用,我会随时发布任何更新。编辑:我想我知道问题所在,当单击下一个图像链接时,页面会刷新,从而意味着 imgsindex 被重置为 0 并且相同的图像再次闪烁。
    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多