jQuery 动画到 div 中的下一个图像

发布于 2024-10-18 03:13:51 字数 1504 浏览 2 评论 0原文

我在 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();

});

有没有一种方法可以动画到下一个没有 leftButtonrightButton 类的图像。

还有一种方法,如果我在第一张图片上,它会转到 div 中的最后一张图片?

这是我得到的 jsFiddle 的链接

有什么想法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-10-25 03:13:51

编辑以将图像滚动到视图中:

这需要对标记进行一些更改,但允许图像向左/右滚动:

HTML:

<div class="work">
    <div class="nav">
        <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>       
    </div>
    <div class="scroller">
        <div class="items">
            <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>
    </div>
</div>

相关 CSS: >

div.work { background-color: #ddd; height:240px; position: relative; width:300px;  margin-left:10px}
div.items { position:relative; width: 1000em; height: 240px; }
div.scroller { overflow: hidden; width: 300px; height: 100%; }

JavaScript:

$('.work .leftButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === 0) {
        $items.css("left", "-300px");
        $items.children("a:last").prependTo($items);
    }
    $(".items").animate({
        left: "+=300px"
    });
});

$('.work .rightButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === -900) {
        $items.css("left", "-600px");
        $items.children("a:first").appendTo($items);
    }

    $(".items").animate({
        left: "-=300px"
    });

});

基本上,发生的事情是有一个固定视口(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:

<div class="work">
    <div class="nav">
        <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>       
    </div>
    <div class="scroller">
        <div class="items">
            <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>
    </div>
</div>

Relevant CSS:

div.work { background-color: #ddd; height:240px; position: relative; width:300px;  margin-left:10px}
div.items { position:relative; width: 1000em; height: 240px; }
div.scroller { overflow: hidden; width: 300px; height: 100%; }

JavaScript:

$('.work .leftButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === 0) {
        $items.css("left", "-300px");
        $items.children("a:last").prependTo($items);
    }
    $(".items").animate({
        left: "+=300px"
    });
});

$('.work .rightButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === -900) {
        $items.css("left", "-600px");
        $items.children("a:first").appendTo($items);
    }

    $(".items").animate({
        left: "-=300px"
    });

});

Basically what's going on is that there's a fixed viewport (scroller) with hidden overflow that contains a really long div (items) containing all the images. The items 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/

梦纸 2024-10-25 03:13:51

如果您从 CSS(在 img 标签上)中删除“display:none”,则以下代码可与您的标记配合使用并在左右按钮上旋转图像:

<script type="text/javascript">
<!--
    $(function() {
        $("div.work >a:gt(2)").hide();

        $(".rightButton").click(function () {

            if ($("div.work >a:last").is(":visible"))
            {
                $("div.work >a:gt(1)").hide();
                $("div.work >a:eq(2)").show();
            } else {
                $("div.work >a:visible:gt(1)").hide().next().show();
            }
        });

        $(".leftButton").click(function () {

            if ($("div.work >a:eq(2)").is(":visible"))
            {
                $("div.work >a:gt(1)").hide();
                $("div.work >a:last").show();
            } else {
                $("div.work >a:visible:gt(1)").hide().prev().show();
            }
        });

    });



//-->
</script>

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:

<script type="text/javascript">
<!--
    $(function() {
        $("div.work >a:gt(2)").hide();

        $(".rightButton").click(function () {

            if ($("div.work >a:last").is(":visible"))
            {
                $("div.work >a:gt(1)").hide();
                $("div.work >a:eq(2)").show();
            } else {
                $("div.work >a:visible:gt(1)").hide().next().show();
            }
        });

        $(".leftButton").click(function () {

            if ($("div.work >a:eq(2)").is(":visible"))
            {
                $("div.work >a:gt(1)").hide();
                $("div.work >a:last").show();
            } else {
                $("div.work >a:visible:gt(1)").hide().prev().show();
            }
        });

    });



//-->
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文