javascript - 使用下一个/上一个按钮切换图像

标签 javascript html jquery image

有没有办法使用 jQuery 使用下一个/上一个按钮来切换图像?代码如下:

<div class="prevDiv">
    <a href="#"><img src="images/prev.png" alt="previous" /></a>
</div>
<div class="pic" id="picwebd">
    <img src="images/portfolio/webdesign/webd1.jpg" class="portPic" />
</div>
<div class="nextDiv">
    <a href="#"><img src="images/next.png" alt="previous" /></a>
</div>

我尝试根据我的需要修改此代码:http://jsfiddle.net/x5mCR/16/但我还没有成功。我认为在图像 src 中增加和减少数字就足够了,但我无法拿出合适的代码来做到这一点。 Google 也无济于事。

最佳答案

如果阅读这篇文章的人想要采用不同的方法,但仍使用 JQuery fadeIn,我将在代码下方发布该内容。

Here你可以找到它的 fiddle 。

这是 JavaScript 部分

//At the start will show the first image
$('#fullimage img.fullimage:first-child').fadeIn();
//Keep track of the image currently being visualized
var curr = $('#fullimage img.fullimage:first-child');

$('#next').on('click', function() {
    //Hide Current Image
    curr.hide();
    //Find Next Image
    var next = curr.next();
    //If theres no next image (is the last img), go back to first
    if (next.length == 0) next = $('#fullimage img:first');
    //Fade In
    next.fadeIn();
    //Save in curr the current Image
    curr = next;
    return false;
});

$('#prev').on('click', function() {
    curr.hide();
    var prev = curr.prev();
    if (prev.length == 0) prev = $('#fullimage img:last');
    prev.fadeIn();
    curr = prev;
    return false;
});

这是 HTML 部分

<div id="fullimage">
 <img class="fullimage" src="http://i.imgur.com/RHhXG.jpg" />
 <img class="fullimage" src="http://i.imgur.com/p1L2e.jpg" />
 <img class="fullimage" src="http://i.imgur.com/NsrI0.jpg" />
 <img class="fullimage" src="http://i.imgur.com/Ww6EU.jpg" /> 
</div>
<a href="#"><label id="prev">previous</label></a>
<a href="#"><label id="next">next</label></a>

关于javascript - 使用下一个/上一个按钮切换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20229109/

相关文章:

javascript - 如何在 mvc 中使用脚本?不是内联脚本

javascript - 需要折线图下方的颜色纯html和css

html - 如何摆脱两个div之间的空间?

javascript - 根据单选按钮选择显示/隐藏 div

jquery 将事件处理程序添加到数组中的对象

javascript - 悬停时动画表格背景 - Jquery

JavaScript 尝试替换 backgroundImage

javascript - 电话号码验证正则表达式由一个加号开始和前面的数字组成

Javascript 检查输入是英语还是阿拉伯语

javascript - 通过拖动调整 pre 元素的大小