在我的按钮内添加CSS转换

时间:2017-09-15 22:20:49

标签: html css css-transitions

我想在我的按钮内部应用一个转换来实现这样的结果:我想将字母向左移动,当我们选择按钮时,图标也会出现并向左移动。

转型前:
enter image description here

过渡后:
enter image description here

我尝试了以下操作,但它不起作用! 出了什么问题?

.button.button-reveal span {
  -webkit-transition: left 0.3s ease, right 0.3s ease;
  -o-transition: left 0.3s ease, right 0.3s ease;
  transition: left 0.3s ease, right 0.3s ease;
}

.button.button-reveal:hover span {
  opacity: 1;
}

.button {
  display: inline-block;
  //position: relative;
  cursor: pointer;
  outline: none;
  white-space: nowrap;
  margin: 5px;
  padding: 0 22px;
  font-size: 14px;
  font-family: 'Roboto', sans-serif, 'Lato';
  height: 40px;
  line-height: 40px;
  background-color: #00a2dc;
  color: #FFF;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: none;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
}

.button.button-rounded {
  border-radius: 3px;
}

.button.button-reveal {
  padding: 0 28px;
  overflow: hidden;
}

.button.button-large {
  padding: 0 26px;
  font-size: 16px;
  height: 46px;
  line-height: 46px;
}

.button-teal {
  background-color: #00a2dc;
}
<div class="panel-heading">
  <h2 class="panel-title">Access This Service</h2>
</div>

<div class="panel-body">
  <ol class="leftmargin-sm nobottommargin">
    <li>test <a href="http://google.com">test</a></li>
    <li>test.</li>
    <li>test. </li>
    <li>test.</li>
  </ol>
  <a href="http://google.com" class="button button-rounded button-reveal button-large button-teal"><i class="fa fa-forward" aria-hidden="true"></i><span>Go there now!</span></a><br> Note: test <a href="http://google.com">google.com</a>.
</div>

1 个答案:

答案 0 :(得分:1)

我认为主要问题是您已为leftright属性设置了转换,但您不会在悬停时更改这些属性。

下面,我为all元素上的<i>设置了转换。暂停时,我从width:0;更改为width:1em;margin-right:1em;

有关详情,请参阅Using CSS Transitions

.button-reveal i {
  display: inline-block;
  width: 0;
  height: 1em;
  overflow: hidden;
  transition: all 0.3s ease;
}

.button.button-reveal:hover i {
  width: 1em;
  margin-right: 1em;
}

.button {
  display: inline-block;
  white-space: nowrap;
  margin: 5px;
  padding: 0 22px;
  font-size: 14px;
  font-family: 'Roboto', sans-serif, 'Lato';
  height: 40px;
  line-height: 40px;
  background-color: #00a2dc;
  color: #FFF;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
  text-decoration: none;
}

.button.button-rounded {
  border-radius: 3px;
}

.button.button-reveal {
  padding: 0 28px;
  overflow: hidden;
}

.button.button-large {
  padding: 0 26px;
  font-size: 16px;
  height: 46px;
  line-height: 46px;
}

.button-teal {
  background-color: #00a2dc;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<a href="http://google.com" class="button button-rounded button-reveal button-large button-teal"><i class="fa fa-forward" aria-hidden="true">&#xbb;</i><span>Go there now!</span></a>

修改

在此示例中,我为图标的left属性设置了动画,以便将其显示在视图中,并将文本的margin设置为向右移动。

.button-reveal i {
  position: absolute;
  width: 2em;
  height: 100%;
  line-height: 45px;
  background-color: rgba(0, 0, 0, .2);
  text-align: center;
  left: -100%;
  transition: left 0.25s ease;
}
.button-reveal:hover i {
  left: 0;
}

.button-reveal span {
  display: inline-block;
  margin: 0 2em;
  transition: margin 0.35s ease;
}
.button-reveal:hover span {
  margin: 0 1em 0 3em;
}

.button {
  position: relative;
  display: inline-block;
  white-space: nowrap;
  margin: 5px;
  font-size: 14px;
  font-family: 'Roboto', sans-serif, 'Lato';
  height: 40px;
  line-height: 40px;
  background-color: #00a2dc;
  color: #FFF;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
  text-decoration: none;
}

.button.button-rounded {
  border-radius: 3px;
}
.button.button-large {
  font-size: 16px;
  height: 46px;
  line-height: 46px;
}
.button-teal {
  background-color: #00a2dc;
}
.button-reveal {
  overflow: hidden;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<a href="http://google.com" class="button button-rounded button-reveal button-large button-teal"><i class="fa fa-forward" aria-hidden="true"></i><span>Go there now!</span></a>