简体   繁体   English

如何旋转JUST点击的元素?

[英]How do I rotate JUST the element clicked?

So I have six #card elements, and each one has a .content element inside of it that is supposed to rotate when that #card is clicked on. 所以我有六个#card元素,每个元素都有一个.content元素,当单击#card时它应该旋转。 However the problem is that when I click on a #card , all six instances of .content rotate. 但问题是,当我点击#card.content所有六个实例都会旋转。

I want JUST the instance of .content inside the clicked on #card to rotate. 我想要点击#card.content实例来旋转。

Here is the jQuery that was causing that problem: 以下是导致该问题的jQuery:

var rotated = false;
$('#card').click(function(){
    if(!rotated){
        $('.content').css({
            "transform": "rotateX(180deg)"
        });
        rotated = true; 
    }else{
        $('.content').css({
            "transform": "rotateX(0deg)"
        });
        rotated = false;
    }
});

I tried this to fix it but to no avail: 我试过这个来解决它,但无济于事:

var rotated = false;
$('#card').click(function(){
    if(!rotated){
        $(this + '.content').css({
            "transform": "rotateX(180deg)"
        });
        rotated = true; 
    }else{
        $(this + '.content').css({
            "transform": "rotateX(0deg)"
        });
        rotated = false;
    }
});

Is there a simple solution to this? 有一个简单的解决方案吗? I'm newish to jQuery and don't know the full extent of the syntax. 我对jQuery很新,并且不知道语法的全部范围。

Change 更改

$(this + '.content').css({

to be 成为

$(this).find(".content").css({

Toggle classes is so much easier: 切换类非常简单:

 $(".card").click(function() { $(this).find(".content").toggleClass("rotated"); }); 
 .card { display: inline-block; } .content { background: rgb(15, 33, 155); background: linear-gradient(to right, rgba(15, 33, 155, 1) 0%, rgba(255, 26, 85, 1) 99%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0f219b', endColorstr='#ff1a55', GradientType=1); color: white; height: 40px; width: 40px; transform: rotateX(0deg); transition: all .3s ease; } .rotated { transform: rotateX(180deg) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class=card> <div class=content>1</div> </div> <div class=card> <div class=content>2</div> </div> <div class=card> <div class=content>3</div> </div> <div class=card> <div class=content>4</div> </div> <div class=card> <div class=content>5</div> </div> <div class=card> <div class=content>6</div> </div> 

PS Don't use more than one ID on the page, use classes. PS不要在页面上使用多个ID,请使用类。

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

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