简体   繁体   English

使用下一个和上一个按钮滑过div

[英]sliding through divs using next and previous buttons

I am trying to slide through 2 different divs on my page with 2 buttons - next and previous. 我试图在我的页面上滑动2个不同的div,有2个按钮 - 下一个和上一个。

it is basically like a setup wizard that you see while installing a software. 它基本上就像您在安装软件时看到的设置向导。

so here is the mark up: 所以这是标记:

<div id="container">

        <div id="box1" class="box">
            <button id="nxt">Next</button>
            <button id="prv">Previous</button>
        </div>
        <div id="box2" class="box">
            <button id="nxt">Next</button>
            <button id="prv">Previous</button>
        </div>

    </div>

and here is the JQuery for both buttons: 这里是两个按钮的JQuery:

$('#nxt').click(function() {

                    $(this).parent(".box").animate({left: '-150%'}, 500 );
                    $(this).parent(".box").next(".box").animate({left: '50%'},500);
        });
 $('#prv').click(function() {

            $(this).parent(".box").animate({left: '150%'}, 500 );
            $(this).parent(".box").prev(".box").animate({left: '50%'},500);
            });

I am able to do perform the "next" operation but not the "previous" one, there is a mistake with jquery, please help me with that. 我能够执行“下一步”操作而不是“前一个”操作,jquery有一个错误,请帮助我。

you can also have a glance at this fiddle 你也可以瞥一眼这个小提琴

Thank You 谢谢

You must not use same ids for multiple elements. 您不得对多个元素使用相同的ID。 Use class attribute instead, see below markup and jquery; 请改用class属性,参见下面的标记和jquery;

HTML: Change id to class for previous and next buttons HTML:将上一个和下一个按钮的ID更改为类

<div id="container">
    <div id="box1" class="box">
       <button class="nxt">Next</button>
       <button class="prv">Previous</button>
    </div>
    <div id="box2" class="box">
       <button class="nxt">Next</button>
       <button class="prv">Previous</button>
    </div>

</div>

jQuery: make changes in jquery selector for class attribute and keep rest of the code as it is. jQuery:在类属性的jquery选择器中进行更改,并按原样保留其余代码。

$('.nxt').click(function() {
    $(this).parent(".box").animate({left: '-150%'}, 500 );
    $(this).parent(".box").next(".box").animate({left: '50%'},500);
});

$('.prv').click(function() {    
    $(this).parent(".box").animate({left: '150%'}, 500 );
    $(this).parent(".box").prev(".box").animate({left: '50%'},500);
});

JSFiddle Demo JSFiddle演示

you have just missed .animate({left: '-50%'} in $('#prv').click(function(){} 你刚刚错过.animate({left: '-50%'}$('#prv').click(function(){} .animate({left: '-50%'} $('#prv').click(function(){}

JSFiddle link JSFiddle链接

You use same id for buttons in first div and in second div. 您在第一个div和第二个div中使用相同的id作为按钮。 It is not right and that's the source of problem. 这是不对的,这是问题的根源。 Check out this fiddle to see right version. 看看这个小提琴看到正确的版本。

 $('#nxt').click(function() { $(this).parent(".box").animate({left: '-150%'}, 500 ); $(this).parent(".box").next(".box").animate({left: '50%'},500); }); $('#prv1').click(function() { $(this).parent(".box").animate({left: '150%'}, 500 ); $(this).parent(".box").prev(".box").animate({left: '50%'},500); }); 
 #container { position: absolute; margin: 0px; padding: 0px; width: 100%; height: 100%; overflow: hidden; } .box { position: absolute; width: 50%; background-color: white; height: 500px; line-height: 300px; font-size: 50px; text-align: center; border: 2px solid black; left: 50%; top: 100px; margin-left: -25%; } #box2 { left: 150%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="box1" class="box"> <button id="nxt">Next</button> <button id="prv">Previous</button> </div> <div id="box2" class="box"> <button id="nxt1">Next</button> <button id="prv1">Previous</button> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM