首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CSS或JS来回切换按钮过渡

问使用CSS或JS来回切换按钮过渡
EN

Stack Overflow用户
提问于 2016-11-14 22:32:37
回答 4查看 888关注 0票数 1

我想创建一个按钮,在两个状态之间来回切换点击。目前,第一次单击会成功地将按钮转换到第二个状态,但我无法让按钮在第二次单击时转换回原始状态。

下面是我的代码:

​

代码语言:javascript
复制
$('.popup_contact').on('click', function() {
  $(this).toggleClass('active');
});
代码语言:javascript
复制
.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);
}
代码语言:javascript
复制
<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>

​

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-11-14 22:42:32

这是因为应用了focus和active事件:

代码语言:javascript
复制
.popup_contact:focus:before,
.popup_contact:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}

而不是这样,你应该使用:

代码语言:javascript
复制
.popup_contact.active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}

现在你可以切换它了。干杯!

​

代码语言:javascript
复制
$('.popup_contact').on('click', function() {
  $(this).toggleClass('active');
});
代码语言:javascript
复制
.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);
}
代码语言:javascript
复制
<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>

​

票数 3
EN

Stack Overflow用户

发布于 2016-11-14 22:43:43

假设你想让你的按钮在被点击之前是静态的(也就是悬停时没有变化),你需要改变你的CSS类,如下所示:

代码语言:javascript
复制
.popup_contact:hover,
.popup_contact:focus,
.popup_contact:active {
  ...
}
.popup_contact:focus:before,
.popup_contact:active:before {
  ...
}

至:

代码语言:javascript
复制
.popup_contact.active {
  ...
}

.popup_contact.active:before {
  ...
}

目前,您的目标是各种psuedo状态,而您的JavaScript正在添加一个类。

票数 0
EN

Stack Overflow用户

发布于 2016-11-14 22:46:51

您需要在CSS中包含类.active,请参阅代码片段

代码段

​

代码语言:javascript
复制
$('.popup_contact').on('click', function() {
  $(this).toggleClass('active');
});
代码语言:javascript
复制
.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);
}
代码语言:javascript
复制
<!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>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40591157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档