jQuery 动画到 div 中的下一个图像
我在 div
中有一些图像,当我单击下一个按钮时,我尝试将其动画到 div 中的下一个图像在
这个 div 中,我还有上一个和下一个按钮,显然我不想动画化。
<div class="work">
<a href="#" class="rightButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/right.png" /></a>
<a href="#" class="leftButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/left.png" /></a>
<a href="#" class="1"><img class="topLeft" src="https://i.sstatic.net/JQFbb.jpg" /></a>
<a href="#" class="2"><img class="topRight" src="https://i.sstatic.net/l5OPs.jpg" /></a>
<a href="#" class="3"><img class="bottomLeft" src="https://i.sstatic.net/okxQz.jpg" /></a>
<a href="#" class="4"><img class="bottomRight" src="https://i.sstatic.net/4uPHw.jpg" /></a>
</div>
我的 jQuery 实际上不起作用
// prevent click jump
$('a').click(function() {
return false;
});
// do work
$('.work > .leftButton').click(function() {
//animate to previous image in the list
$(this).siblings().prev().show();
});
$('.work > .rightButton').click(function() {
//animate to next image in the list
$(this).siblings().next().show();
});
有没有一种方法可以动画到下一个没有 leftButton
或 rightButton
类的图像。
还有一种方法,如果我在第一张图片上,它会转到 div 中的最后一张图片?
有什么想法吗?
I've got some images within a div
which i'm trying to when I click the next button animate to the next one in the div
In this div I also have my previous and next button which I obviously don't want to animate to.
<div class="work">
<a href="#" class="rightButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/right.png" /></a>
<a href="#" class="leftButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/left.png" /></a>
<a href="#" class="1"><img class="topLeft" src="https://i.sstatic.net/JQFbb.jpg" /></a>
<a href="#" class="2"><img class="topRight" src="https://i.sstatic.net/l5OPs.jpg" /></a>
<a href="#" class="3"><img class="bottomLeft" src="https://i.sstatic.net/okxQz.jpg" /></a>
<a href="#" class="4"><img class="bottomRight" src="https://i.sstatic.net/4uPHw.jpg" /></a>
</div>
My jQuery which doesn't actually work
// prevent click jump
$('a').click(function() {
return false;
});
// do work
$('.work > .leftButton').click(function() {
//animate to previous image in the list
$(this).siblings().prev().show();
});
$('.work > .rightButton').click(function() {
//animate to next image in the list
$(this).siblings().next().show();
});
Is there a way to animate to the next image that doesn't have the class of leftButton
or rightButton
.
Also is there a way so that if i'm on the first image it will go to the last image in the div?
Here's a link to the jsFiddle i've got
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑以将图像滚动到视图中:
这需要对标记进行一些更改,但允许图像向左/右滚动:
HTML:
相关 CSS: >
JavaScript:
基本上,发生的事情是有一个固定视口(
scroller
),带有隐藏的溢出,其中包含一个非常长的div
(items
)包含所有图像。每次单击下一个/上一个按钮时,items
div 就会动画化。棘手的部分是将最后一个元素移动到前面,反之亦然,当到达图像行的末尾时。这是由硬编码值确定的,但可以使用精确计算来改进。
工作示例: http://jsfiddle.net/andrewwhitaker/6TLK2/3/
Edited to scroll images into view:
This requires some change to your markup, but will allow images to scroll left/right:
HTML:
Relevant CSS:
JavaScript:
Basically what's going on is that there's a fixed viewport (
scroller
) with hidden overflow that contains a really longdiv
(items
) containing all the images. Theitems
div is just animated on every click of the next/previous buttons.The tricky part is moving the last element to the front and vice/versa when the end of the line of images is reached. This is determined by hardcoded values, but could probably be improved using an exact calculation.
Working example: http://jsfiddle.net/andrewwhitaker/6TLK2/3/
如果您从 CSS(在 img 标签上)中删除“display:none”,则以下代码可与您的标记配合使用并在左右按钮上旋转图像:
If you remove 'display:none' from the CSS (on img tags), here is the code that works with your markup and rotates images on right and left buttons: