javascript - jQuery 图像 slider - 当图像用完时从头开始重新启动

标签 javascript jquery html css

所以我在我自己构建的 wordpress 页面上有一个非常简单的(可能不是推荐的构建方式)图像 slider 。我的问题是,当图像用完时,它只会滑动空白,等等。我希望它从头开始滑动,或者只是在最后一张图片停止滑动。这是我的代码: HTML:

<div id="gallery-wrap">
   <div id="gallery">
      <img class="galleryimage" src="http://materiaalit.kotisivut.name/sivustokuvat/ccc.PNG" alt="" />
      <img class="galleryimage2"  src="http://materiaalit.kotisivut.name/sivustokuvat/coverline.PNG" alt="" />
      <img class="galleryimage3"  src="http://materiaalit.kotisivut.name/sivustokuvat/ccc.PNG" alt="" />
      <img class="galleryimage4"  src="http://materiaalit.kotisivut.name/sivustokuvat/coverline.PNG" alt="" />
   </div>
    <div id="gallery-controls">
      <a id="gallery-prev" href="#"><img alt="" /> </a>
      <a id="gallery-next" href="#"><img alt="" /></a></div>
    </div>
</div>

CSS:

#gallery-wrap{margin: 0 auto; overflow: hidden; width: 100%; position: relative; height:300px; border:1px solid black; border-radius:6px; z-index:3;}
#gallery{position: relative; left: 0; top: 0; width:100%;}
.galleryimage{position:absolute; width:100%; height:300px; top:0px;}
.galleryimage2{position:absolute; width:100%; height:300px; left:100%;top:0px;}
.galleryimage3{position:absolute; width:100%; height:300px; left:200%;top:0px;}
.galleryimage4{position:absolute; width:100%; height:300px; left:300%;top:0px;}

#gallery-controls{width: 100%; z-index:4;}
#gallery-prev{position:absolute; left:0px; top:0px; width:50%; height:300px; }
#gallery-next{position:absolute; right:0px; top:0px;  width:50%; height:300px;}

和 js/jquery

var position = 1; // you always start at the first image?
$(document).ready(function()
{
  $("#gallery-prev").click(function(){

    var nr_of_img = $('img', $('#gallery')).length;

    if (position == 1)
    { 
      // move all the way to the last image
      position = nr_of_img;
    }
    else
    {$("#gallery").animate({"left": "+=100%"}, "slow");
      // move to the previous image
      position--;
    }
  });

  $("#gallery-next").click(function(){

    var nr_of_img = $('img', $('#gallery')).length;

    if (position == nr_of_img)
    { 
      // move all the way to the first image
      position = 1;
    }
    else
    {$("#gallery").animate({"left": "-=100%"}, "slow");
      // move to the previous image
      position++;
    }
  });
});

正如您可能看到的那样,我正在考虑将什么放入变量中。脚本如何知道图像何时用完?正如您所看到的,图像是绝对定位的,这是将它们全部放在同一条水平线上的最简单方法。 如果有人想在行动中看到这个: http://wordpress.kotisivut.name/

最佳答案

你为什么不计算 slider 中的图像数量...

var nr_of_img = $('img', $('#gallery')).length;

然后你可以计算你从左到右移动的次数等等,看看你是否用完了图像。

[编辑]

首先,您的 if 语句位于错误的位置。您想在事件函数中执行 if 语句。

这意味着你会做这样的事情

var position = 1; // you always start at the first image?
$(document).ready(function()
{
  $("#gallery-prev").click(function(){
    var nr_of_img = $('img', $('#gallery')).length;

    if (position == 1)
    {
      // move all the way to the last image
      position = nr_of_img;
    }
    else
    {
      // move to the previous image
      position--;
    }
  });

  $("#gallery-next").click(function(){
    var nr_of_img = $('img', $('#gallery')).length;

    if (position == nr_of_img)
    {
      // move all the way to the first image
      position = 1;
    }
    else
    {
      // move to the previous image
      position++;
    }
  });
});

关于javascript - jQuery 图像 slider - 当图像用完时从头开始重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11629054/

相关文章:

javascript - 动态删除对象属性 JavaScript

javascript - 如何从动态表中获取数据

javascript - OpenLayers:OSM 未显示在 div 上

javascript - 从一个 noUISlider 输出两个值

javascript - 根据内容和浏览器宽度更改 div 宽度

python - 如何防止漂白剂逃逸 > (blockquote) 标签在 Markdown 中使用

javascript - Angular 页面刷新中发生了什么为什么我的登录用户无法识别

javascript - 仅对当前行显示保存按钮

jquery - 导航栏固定到顶部没有内容

html - CSS3::selection 不覆盖整个页面