简体   繁体   English

按钮转换使用CSS或JS来回切换

[英]button transition toggle back and forth using CSS or JS

I would like to create a button that toggles back and forth between two states on click. 我想创建一个按钮,在点击时在两个状态之间来回切换。 Currently the first click transitions the button successfully to the second state, but I cannot get the button to transition back to the original state on the second click. 目前,第一次单击将按钮成功转换为第二个状态,但是我无法在第二次单击时将按钮转换回原始状态。

Here is my code: 这是我的代码:

 $('.popup_contact').on('click', function() { $(this).toggleClass('active'); }); 
 .popup_contact { display: inline-block; min-height: 50px; height: auto !important; width: 100px; padding-top: 5px; text-align: center; font-size: 20px; font-weight: 800; background: #e8eaed; border: none; border-radius: 10px; color: #424e65 !important; -webkit-transform: perspective(0px) translateZ(0); transform: perspective(0px) translateZ(0); -webkit-transition-property: color; transition-property: color; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; } .popup_contact:before { font-family: FontAwesome; content: '\\f00d'; color: #fff; padding-top: 15px; border-radius: 10px; position: absolute; text-align: center; z-index: 1; top: 0; left: 0; right: 0; bottom: 0; background: #424e65; -webkit-transform: scaleX(0); transform: scaleX(0); -webkit-transform-origin: 0 50%; transform-origin: 0 50%; -webkit-transition-property: transform; transition-property: transform; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; -webkit-transition-timing-function: ease-out; transition-timing-function: ease-out; } .popup_contact:hover, .popup_contact:focus, .popup_contact:active { color: #424e65 !important; cursor: pointer; border: none; background: #e8eaed; outline: 0 !important; } .popup_contact:focus:before, .popup_contact:active:before { -webkit-transform: scaleX(1); transform: scaleX(1); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" /> <button class="popup_contact" style="vertical-align:middle"><span><i class="fa fa-fighter-jet" aria-hidden="true"></i> </span> </button> 

This is because focus and active event is applied: 这是因为应用了focusactive事件:

.popup_contact:focus:before,
.popup_contact:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}

Instead of this you should use: 而不是这个你应该使用:

.popup_contact.active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}

and you can now toggle it. 你现在可以切换它。 Cheers! 干杯!

 $('.popup_contact').on('click', function() { $(this).toggleClass('active'); }); 
 .popup_contact { display: inline-block; min-height: 50px; height: auto !important; width: 100px; padding-top: 5px; text-align: center; font-size: 20px; font-weight: 800; background: #e8eaed; border: none; border-radius: 10px; color: #424e65 !important; -webkit-transform: perspective(0px) translateZ(0); transform: perspective(0px) translateZ(0); -webkit-transition-property: color; transition-property: color; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; } .popup_contact:before { font-family: FontAwesome; content: '\\f00d'; color: #fff; padding-top: 15px; border-radius: 10px; position: absolute; text-align: center; z-index: 1; top: 0; left: 0; right: 0; bottom: 0; background: #424e65; -webkit-transform: scaleX(0); transform: scaleX(0); -webkit-transform-origin: 0 50%; transform-origin: 0 50%; -webkit-transition-property: transform; transition-property: transform; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; -webkit-transition-timing-function: ease-out; transition-timing-function: ease-out; } .popup_contact:hover, .popup_contact:focus, .popup_contact:active { color: #424e65 !important; cursor: pointer; border: none; background: #e8eaed; outline: 0 !important; } .popup_contact.active:before { -webkit-transform: scaleX(1); transform: scaleX(1); } 
 <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> <div> <button class="popup_contact" style="vertical-align:middle"><span><i class="fa fa-fighter-jet" aria-hidden="true"></i> </span> </button> </div> 

Assuming you want your button to be static until clicked (ie no change on hover also), you need to change your CSS clases from: 假设您希望按钮在被点击之前是静态的(即悬停时也没有变化),您需要更改CSS clases:

.popup_contact:hover,
.popup_contact:focus,
.popup_contact:active {
  ...
}
.popup_contact:focus:before,
.popup_contact:active:before {
  ...
}

To: 至:

.popup_contact.active {
  ...
}

.popup_contact.active:before {
  ...
}

Currently, you're targeting the various psuedo states, whereas your JavaScript is adding a class. 目前,您的目标是各种psuedo状态,而您的JavaScript正在添加一个类。

You need the class .active to be in your CSS, see Snippet 您需要将类.active放在CSS中,请参阅Snippet

SNIPPET SNIPPET

 $('.popup_contact').on('click', function() { $(this).toggleClass('active'); }); 
 .popup_contact { display: inline-block; min-height: 50px; height: auto !important; width: 100px; padding-top: 5px; text-align: center; font-size: 20px; font-weight: 800; background: #e8eaed; border: none; border-radius: 10px; color: #424e65 !important; -webkit-transform: perspective(0px) translateZ(0); transform: perspective(0px) translateZ(0); -webkit-transition-property: color; transition-property: color; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; outline: none; } .popup_contact:before { font-family: FontAwesome; content: '\\f00d'; color: #fff; padding-top: 15px; border-radius: 10px; position: absolute; text-align: center; z-index: 1; top: 0; left: 0; right: 0; bottom: 0; background: #424e65; -webkit-transform: scaleX(0); transform: scaleX(0); -webkit-transform-origin: 0 50%; transform-origin: 0 50%; -webkit-transition-property: transform; transition-property: transform; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; -webkit-transition-timing-function: ease-out; transition-timing-function: ease-out; } .popup_contact.active:hover, .popup_contact.active:focus, .popup_contact.active:active { color: #42465 !important; cursor: pointer; border: none; background: #e8eaed; } .popup_contact.active:focus:before, .popup_contact.active:active:before { -webkit-transform: scaleX(1); transform: scaleX(1); } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> </head> <body> <button class="popup_contact" style="vertical-align:middle"><i class="fa fa-fighter-jet" aria-hidden="true"></i><span> </span> </button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </body> </html> 

Your CSS doesnt have the .active class. 你的CSS没有.active类。

Try add this to your CSS: 尝试将此添加到您的CSS:

.active {
border: 5px solid green;
}

This should add a green border when active. 这应该在激活时添加绿色边框。

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

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