jquery淡出当前div,找到下一个div并淡入或找到最后一个并淡入

发布于 2024-10-09 03:55:03 字数 870 浏览 0 评论 0原文

我有一个名为 .stage 的 div 类,它是调查问卷的一个阶段

<div class="stage">
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
</div>

,我正在尝试编写一些紧凑的 jQuery,如果您选择“下一步”,它会关闭当前阶段并打开下一个阶段,或者如果您单击“返回”,它会关闭当前阶段,打开最后一个阶段,然后等等..

$(".next").click(function () {
        $(this).closest(".stage").fadeOut();
        (".stage").next().fadeIn();
    });
$(".back").click(function () {
        $(this).closest(".stage").fadeOut();
        (".stage").last().fadeIn();
    });
});

运气不太好

I have div class called .stage which is a stage of a questionnaire

<div class="stage">
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
    <div class="next">Next</div>
</div>
<div class="stage">
    <div class="back">Back</div>
</div>

I am trying to write some compact jQuery that if you select next it closes the current stage and opens the next stage or if you click back it closes current stage opens last one and so on..

$(".next").click(function () {
        $(this).closest(".stage").fadeOut();
        (".stage").next().fadeIn();
    });
$(".back").click(function () {
        $(this).closest(".stage").fadeOut();
        (".stage").last().fadeIn();
    });
});

Not having much luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

じ违心 2024-10-16 03:55:04

您只需要一些链接更改,如下所示:

$(".next").click(function () {
  $(this).closest(".stage").fadeOut().next().fadeIn();
});
$(".back").click(function () {
  $(this).closest(".stage").fadeOut().prev().fadeIn();
});

注意 .prev()< 的使用/a> 代表前一个兄弟,而不是 .last()获取集合中的最后一个元素。如果您不想要交叉淡入淡出,请在回调中执行淡入,如下所示:

$(".next").click(function () {
  $(this).closest(".stage").stop().fadeOut(function() {
    $(this).next().fadeIn();
  });
});
$(".back").click(function () {
  $(this).closest(".stage").stop().fadeOut(function() {
    $(this).prev().fadeIn();
  });
});

You just need some chaining changes, like this:

$(".next").click(function () {
  $(this).closest(".stage").fadeOut().next().fadeIn();
});
$(".back").click(function () {
  $(this).closest(".stage").fadeOut().prev().fadeIn();
});

Note the use of .prev() for the previous sibling, rather than .last() which gets the last element in the set. If you don't want the cross-fade, do the fade-in inside the callback, like this:

$(".next").click(function () {
  $(this).closest(".stage").stop().fadeOut(function() {
    $(this).next().fadeIn();
  });
});
$(".back").click(function () {
  $(this).closest(".stage").stop().fadeOut(function() {
    $(this).prev().fadeIn();
  });
});
埖埖迣鎅 2024-10-16 03:55:04

您可以尝试这样的操作:

var i = 0;
var $stages = $('.stage');
$('.next').click(function() {
  $($stages[i]).fadeOut();
  i++;
  $($stages[i]).fadeIn();
});
$('.back').click(function() {
  $($stages[i]).fadeOut();
  i--;
  $($stages[i]).fadeIn();
});

通过这样做并跟踪用户所在的项目,您还可以始终通过执行 $stages[i] 检索应该可见的当前阶段。

You could try something like this:

var i = 0;
var $stages = $('.stage');
$('.next').click(function() {
  $($stages[i]).fadeOut();
  i++;
  $($stages[i]).fadeIn();
});
$('.back').click(function() {
  $($stages[i]).fadeOut();
  i--;
  $($stages[i]).fadeIn();
});

By doing it this way and tracking which item the user's on, you can also always retrieve the current stage that should be visible by doing $stages[i].

潜移默化 2024-10-16 03:55:04
<div class="stage">  
<h1>stage 1</h1>
</div>
<div class="stage">   
<h1>stage 2</h1>
</div>
<div class="stage">   
<h1>stage 3</h1>
</div>
<div class="stage">
<h1>stage 4</h1>
</div>


$(document).ready(function() {

             var navBtns = "BackNext";
            $(".stage").hide().append(navBtns);
            $(".stage:first .fade_back").hide();
            $(".stage:last .fade_next").hide();
            $(".stage:first").show();

            $(".fade_next").click(function() {
                $(this).closest(".stage").hide();
                $(this).closest(".stage").next(".stage").fadeIn("slow");
            });

            $(".fade_back").click(function() {
                $(this).closest(".stage").hide();
                $(this).closest(".stage").prev(".stage").fadeIn("slow");
            });
        });





.fade_nav{
    background-color: #f2f2f2;   
    padding: 10px;
    margin: 5px 0;
    text-align: center;
}
.fade_back, .fade_next{
    background-color: #666;
    color: #fff;
    border: 1px solid #888;
    margin: 3px;
    padding: 3px;
}
.fade_back:hover, .fade_next:hover{
    background-color: #000;
   cursor: pointer;
}


<div class="stage">  
<h1>stage 1</h1>
</div>
<div class="stage">   
<h1>stage 2</h1>
</div>
<div class="stage">   
<h1>stage 3</h1>
</div>
<div class="stage">
<h1>stage 4</h1>
</div>


$(document).ready(function() {

             var navBtns = "BackNext";
            $(".stage").hide().append(navBtns);
            $(".stage:first .fade_back").hide();
            $(".stage:last .fade_next").hide();
            $(".stage:first").show();

            $(".fade_next").click(function() {
                $(this).closest(".stage").hide();
                $(this).closest(".stage").next(".stage").fadeIn("slow");
            });

            $(".fade_back").click(function() {
                $(this).closest(".stage").hide();
                $(this).closest(".stage").prev(".stage").fadeIn("slow");
            });
        });





.fade_nav{
    background-color: #f2f2f2;   
    padding: 10px;
    margin: 5px 0;
    text-align: center;
}
.fade_back, .fade_next{
    background-color: #666;
    color: #fff;
    border: 1px solid #888;
    margin: 3px;
    padding: 3px;
}
.fade_back:hover, .fade_next:hover{
    background-color: #000;
   cursor: pointer;
}


影子是时光的心 2024-10-16 03:55:04

抽奖有点慢。基本上与上面的想法相同。然而,“下一步”和“后退”按钮标记有点重复,可以动态添加。这是一个 jsfiddle 显示如何添加:

http://jsfiddle.net/tBwYA/

希望您发现这很有用。

鲍勃

A little slow on the draw. Mostly the same idea aas the above. However, the 'next' and 'back' button markup is a bit repetitive and could be added dynamically. Here is a jsfiddle showing how to add:

http://jsfiddle.net/tBwYA/

Hope you find this useful.

Bob

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