返回下一个/上一个div javascript onclick事件

时间:2015-09-06 23:01:02

标签: javascript jquery html css javascript-events

这里是两个按钮的HTML代码

<button id="Page-Transition" class="btn" id="prev" onClick="loadPreviousPage()"> <-- </button>
        <button id="Page-Transition" class="btn" onClick="loadNextPage()"> --> </button>

并且这里是div父组div的代码

<div id="pt-main" class="pt-perspective">
    <div class="pt-page pt-page-1" id="page1"><h1><span>A collection of</span><strong>Page</strong> Transitions1</h1></div>
    <div class="pt-page pt-page-2" id="page2"><h1><span>A collection of</span><strong>Page</strong> Transitions2</h1></div>
    <div class="pt-page pt-page-3" id="page3"><h1><span>A collection of</span><strong>Page</strong> Transitions3</h1></div>
    <div class="pt-page pt-page-4" id="page4"><h1><span>A collection of</span><strong>Page</strong> Transitions4</h1></div>
    <div class="pt-page pt-page-5" id="page5"><h1><span>A collection of</span><strong>Page</strong> Transitions5</h1></div>
    <div class="pt-page pt-page-6" id="page6"><h1><span>A collection of</span><strong>Page</strong> Transitions6</h1></div>
</div>

2 个答案:

答案 0 :(得分:2)

您可以使用相同的功能并选择任何过渡并循环它们以获得所需的效果。

&#13;
&#13;
function slideShow(mode){
  if(mode){
    $('#pt-main .pt-page:visible').eq(0).slideUp(500,function(){
      $(this).next('.pt-page').slideDown(200);
      $(this).appendTo('#pt-main');
    });
  }else{
    $('#pt-main .pt-page:visible').eq(0).slideUp(500,function(){
      $('#pt-main .pt-page:last').slideDown(200).prependTo('#pt-main');
    });
  }
}

$('#pt-main .pt-page').hide().eq(0).show();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pt-main" class="pt-perspective">
    <div class="pt-page pt-page-1" id="page1"><h1><span>A collection of</span><strong>Page</strong> Transitions1</h1></div>
    <div class="pt-page pt-page-2" id="page2"><h1><span>A collection of</span><strong>Page</strong> Transitions2</h1></div>
    <div class="pt-page pt-page-3" id="page3"><h1><span>A collection of</span><strong>Page</strong> Transitions3</h1></div>
    <div class="pt-page pt-page-4" id="page4"><h1><span>A collection of</span><strong>Page</strong> Transitions4</h1></div>
    <div class="pt-page pt-page-5" id="page5"><h1><span>A collection of</span><strong>Page</strong> Transitions5</h1></div>
    <div class="pt-page pt-page-6" id="page6"><h1><span>A collection of</span><strong>Page</strong> Transitions6</h1></div>
</div>

<button id="Page-Transition" class="btn" id="prev" onClick="slideShow(false)"> &lt;-- </button>
        <button id="Page-Transition" class="btn" onClick="slideShow(true)"> --&gt; </button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是标准的next / prev按钮实现,

$(function() {
    $("body").on("click", "#prev", function() {
       var _prev = $(".pt-page.current").prev();
       _prev = _prev.length == 0 ? $(".pt-page:last") : _prev;
           $(".pt-page.current").fadeOut(function() {
               $(this).removeClass("current");
                _prev.fadeIn(function() {
                    $(this).addClass("current");
                });
           });
    });

    $("body").on("click", "#next", function() {
       var _next = $(".pt-page.current").next();
      _next = _next.length == 0 ? $(".pt-page:first") : _next;
           $(".pt-page.current").fadeOut(function() {
                $(this).removeClass("current");
                _next.fadeIn(function() {
                    $(this).addClass("current");
                });
           });
    });
});

DEMO