简体   繁体   English

如何反转jQuery CSS样式?

[英]How to reverse jQuery css styling?

What is the best way to reverse css styling on the second on click? 单击第二个反转CSS样式的最佳方法是什么?

What I'd like to happen is when the user clicks on the button again, it will just reverse everything to the original position. 我想发生的是,当用户再次单击按钮时,它将把所有内容恢复到原始位置。 I'm not sure what's the most efficient way to do this without re-declaring everything in reverse. 我不确定在不重新声明所有内容的情况下最有效的方法是什么。 Especially that there's not just one class that changes status. 特别是,不仅只有一个类会改变状态。

HTML: HTML:

<div class="wrapper-available">            
   <a class="available">Available</a>
   <div class="available-img"><img src="http://www.petmd.com/sites/all/modules/breedopedia/images/thumbnails/cat/tn-california-spangled-cat.jpg" width="40" height="40"></div>
</div>

CSS: CSS:

.wrapper-available {    
    display: inline-block;
    margin-top: 40px;
    position: relative;
}

.available {
    border-radius: 15px;
    padding: 5px 20px 5px 50px;
    background: #39b54a;    
    color: #FFF;
  display: inline-block;
    font-weight: bold;
}

.available-img {
    left: 0;
    position: absolute;
    top: -12px; 
    transition: all .20s ease-in;
}   

    .available-img img {
        border-radius: 30px;
        border: 2px solid #39b54a;
    }

jQuery: jQuery的:

 $(".available").click(function() {
    $(this).css({ "background" : "#CCC", "padding" : "5px 50px 5px 20px" }).text("Away");

    $(".available-img").css({
      "left": 100
    });

    $(".available-img img").css({
      "border" : "2px solid #CCC"
    });
  });

http://codepen.io/aguerrero/pen/ORKjya http://codepen.io/aguerrero/pen/ORKjya

The simplest way would be to toggle a class on the wrapper-available instead of adding inline styling and to also toggle the text() within the .available element. 最简单的方法是在wrapper-available上切换一个类,而不是添加内联样式,并在.available元素内切换text() Try this: 尝试这个:

 $(".available").click(function() { $(this).text(function(i, t) { return t == 'Available' ? 'Away' : 'Available'; }).closest('.wrapper-available').toggleClass('away'); }); 
 .wrapper-available { display: inline-block; margin-top: 40px; position: relative; } .available { border-radius: 15px; padding: 5px 20px 5px 50px; background: #39b54a; color: #FFF; display: inline-block; font-weight: bold; } .wrapper-available.away .available { background-color: #CCC; padding: 5px 50px 5px 20px; } .available-img { left: 0; position: absolute; top: -12px; transition: all .20s ease-in; } .wrapper-available.away .available-img { left: 70px; /* note 70px seems to work better than 100px here */ } .available-img img { border-radius: 30px; border: 2px solid #39b54a; } .wrapper-available.away .available-img img { border: 2px solid #CCC; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper-available"> <a class="available">Available</a> <div class="available-img"> <img src="http://www.petmd.com/sites/all/modules/breedopedia/images/thumbnails/cat/tn-california-spangled-cat.jpg" width="40" height="40"> </div> </div> 

I added three classes in your CSS document with the styling that you added in your jQuery snippet. 我在CSS文档中添加了三个类,并在jQuery代码段中添加了样式。 Now, instead of updating the specific styling for each element via jQuery you can just specify the new styling in your three selectors with the .active class. 现在,不必通过jQuery更新每个元素的特定样式,只需在.active类的三个选择器中指定新样式即可

CSS: CSS:

.available.active {
  background: #CCC;
  padding: 5px 50px 5px 20px;
}

.available-img.active {
  left: 100px;
}

.available-img.active img {
  border: 2px solid #CCC;
}

The jQuery snippet now toggles the .active class on your desired elements. jQuery片段现在在所需元素上切换.active类。 These two: 这两个:

<a class="available">Available</a>
<div class="available-img">

$(this).text() now toggles between Available and Away . $(this).text()现在在AvailableAway之间切换。

jQuery: jQuery的:

$('.available').click(function() {
  $(this).toggleClass('active').siblings().toggleClass('active');
  $(this).text(function(i, text) {
    return (text === 'Available') ? 'Away' : 'Available';
  });
});

Example: http://codepen.io/praktikdan/pen/wzVrGM 示例: http//codepen.io/praktikdan/pen/wzVrGM

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

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