简体   繁体   English

用jquery和“ this”旋转CSS卡

[英]Rotate CSS card with jquery and “this”

I use the following code to rotate my CSS card. 我使用以下代码旋转CSS卡。 The problem is when I have a few cards and they all rotate when clicking any button. 问题是当我有几张卡片并且单击任何按钮时它们都旋转时。

I would like to rotate only the card which contains clicked button. 我只想旋转包含单击按钮的卡。

I suppose I should to add a context to my cards by using 'this', but I cannot do it right. 我想我应该使用'this'在卡片中添加上下文,但是我做不正确。

  $('button').click(function () { $('.card').toggleClass('flipped'); }); 
 .animation { -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .cardContainer { -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; transition: all .3s ease; /*depth of the elements */ -webkit-perspective: 800px; -moz-perspective: 800px; -o-perspective: 800px; perspective: 800px; /*border: 1px solid #ff0000;*/ padding-left: 1%; } .card { width: 99%; height: 200px; /*transition effects */ -webkit-transition: -webkit-transform 0.6s; -moz-transition: -moz-transform 0.6s; -o-transition: -o-transform 0.6s; transition: transform 0.6s; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; } .card.flipped { -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .card.flipped: {} .card .front, .card .back { display: block; height: 100%; width: 100%; line-height: 60px; color: white; text-align: center; font-size: 4em; position: absolute; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25); -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); } .card .back { width: 100%; padding-left: 3%; padding-right: 3%; font-size: 16px; text-align: left; line-height: 25px; } .card .back { background: #03446A; -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .red { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card1</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 1</button> </div> <br> <br> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card2</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 2</button> </div> 

I would like to rotate only the card which contains clicked button. 我只想旋转包含单击按钮的卡。

The cards don't contain the button, but their parents do. 卡片不包含按钮,但父母却包含按钮。 You can use closest to find the cardContainer , then find to find the card : 您可以使用closest找到cardContainer ,然后find以找到该card

$('button').click(function () {
    $(this).closest('.cardContainer').find('.card').toggleClass('flipped');
});

 $('button').click(function() { $(this).closest('.cardContainer').find('.card').toggleClass('flipped'); }); 
 .animation { -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .cardContainer { -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; transition: all .3s ease; /*depth of the elements */ -webkit-perspective: 800px; -moz-perspective: 800px; -o-perspective: 800px; perspective: 800px; /*border: 1px solid #ff0000;*/ padding-left: 1%; } .card { width: 99%; height: 200px; /*transition effects */ -webkit-transition: -webkit-transform 0.6s; -moz-transition: -moz-transform 0.6s; -o-transition: -o-transform 0.6s; transition: transform 0.6s; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; } .card.flipped { -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); } .card.flipped: {} .card .front, .card .back { display: block; height: 100%; width: 100%; line-height: 60px; color: white; text-align: center; font-size: 4em; position: absolute; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25); -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); } .card .back { width: 100%; padding-left: 3%; padding-right: 3%; font-size: 16px; text-align: left; line-height: 25px; } .card .back { background: #03446A; -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); } .red { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card1</h3> </div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 1</button> </div> <br> <br> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card2</h3> </div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 2</button> </div> 

Or as they're siblings, use siblings : 或者因为他们是兄弟姐妹,所以使用siblings

$('button').click(function () {
    $(this).siblings('.card').toggleClass('flipped');
});

 $('button').click(function() { $(this).siblings('.card').toggleClass('flipped'); }); 
 .animation { -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .cardContainer { -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; transition: all .3s ease; /*depth of the elements */ -webkit-perspective: 800px; -moz-perspective: 800px; -o-perspective: 800px; perspective: 800px; /*border: 1px solid #ff0000;*/ padding-left: 1%; } .card { width: 99%; height: 200px; /*transition effects */ -webkit-transition: -webkit-transform 0.6s; -moz-transition: -moz-transform 0.6s; -o-transition: -o-transform 0.6s; transition: transform 0.6s; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; } .card.flipped { -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); } .card.flipped: {} .card .front, .card .back { display: block; height: 100%; width: 100%; line-height: 60px; color: white; text-align: center; font-size: 4em; position: absolute; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25); -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); } .card .back { width: 100%; padding-left: 3%; padding-right: 3%; font-size: 16px; text-align: left; line-height: 25px; } .card .back { background: #03446A; -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); } .red { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card1</h3> </div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 1</button> </div> <br> <br> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card2</h3> </div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 2</button> </div> 

Use sibling for it. 为此使用兄弟姐妹 Check following JQuery: 检查以下JQuery:

 $('button').click(function () { $(this).siblings(".card").toggleClass('flipped'); }); 
 .animation { -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .cardContainer { -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; transition: all .3s ease; /*depth of the elements */ -webkit-perspective: 800px; -moz-perspective: 800px; -o-perspective: 800px; perspective: 800px; /*border: 1px solid #ff0000;*/ padding-left: 1%; } .card { width: 99%; height: 200px; /*transition effects */ -webkit-transition: -webkit-transform 0.6s; -moz-transition: -moz-transform 0.6s; -o-transition: -o-transform 0.6s; transition: transform 0.6s; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; } .card.flipped { -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .card.flipped: {} .card .front, .card .back { display: block; height: 100%; width: 100%; line-height: 60px; color: white; text-align: center; font-size: 4em; position: absolute; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25); -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); } .card .back { width: 100%; padding-left: 3%; padding-right: 3%; font-size: 16px; text-align: left; line-height: 25px; } .card .back { background: #03446A; -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .red { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card1</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 1</button> </div> <br> <br> <div class="col-md-3 cardContainer"> <div class="card red"> <div class="front"> <h3 class="cardTitle">Card2</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button">Rotate card 2</button> </div> 

You can add context using attributes in html that will also be the class of the cards as shown below. 您可以使用html中的属性添加上下文,这些属性也将是卡片的类,如下所示。 .sibling() in jquery will also work but it needs your html to have exact same sequence of elements jQuery中的.sibling()也可以使用,但需要您的html具有完全相同的元素序列

  $('button').click(function () { var cardClass=$(this).attr('data-card'); $('.'+cardClass).toggleClass('flipped'); }); 
 .animation { -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .cardContainer { -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; transition: all .3s ease; /*depth of the elements */ -webkit-perspective: 800px; -moz-perspective: 800px; -o-perspective: 800px; perspective: 800px; /*border: 1px solid #ff0000;*/ padding-left: 1%; } .card { width: 99%; height: 200px; /*transition effects */ -webkit-transition: -webkit-transform 0.6s; -moz-transition: -moz-transform 0.6s; -o-transition: -o-transform 0.6s; transition: transform 0.6s; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; } .card.flipped { -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .card.flipped: {} .card .front, .card .back { display: block; height: 100%; width: 100%; line-height: 60px; color: white; text-align: center; font-size: 4em; position: absolute; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25); -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26); } .card .back { width: 100%; padding-left: 3%; padding-right: 3%; font-size: 16px; text-align: left; line-height: 25px; } .card .back { background: #03446A; -webkit-transform: rotateY( 180deg); -moz-transform: rotateY( 180deg); -o-transform: rotateY( 180deg); transform: rotateY( 180deg); } .red { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 cardContainer"> <div class="card red card-1"> <div class="front"> <h3 class="cardTitle">Card1</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button" data-card="card-1">Rotate card 1</button> </div> <br> <br> <div class="col-md-3 cardContainer"> <div class="card red card-2"> <div class="front"> <h3 class="cardTitle">Card2</h3></div> <div class="back"> <div class="content"> </div> </div> </div> <button type="button" data-card="card-2">Rotate card 2</button> </div> 

You can use .prev() to get the previous sibling which in your example is the card, but be aware that changes to your html structure could break it. 您可以使用.prev()来获取上一个同级同级(在您的示例中为卡),但请注意,对html结构的更改可能会破坏它。

$('button').click(function () {
  $(this).prev().toggleClass('flipped');
});

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

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