按钮动画仅适用于特定边距

时间:2017-07-07 18:13:43

标签: html css

我有一些奇怪的问题。在我的代码中,按钮css动画仅在margin-top设置为125px(对于sim-button类)时才有效。我想将margin-top减少到25px,但动画不起作用。

Codepen Here

HTML:

<div class="media" style = "background-color:#000;">
                          <img class="media__image" src="https://www.offensive-security.com/wp-content/uploads/2012/01/security-risk-assessment-report.png">
                          <div class="media__body">
                            <h2>Penetration Report</h2>
                            <p>Get your hands on our Sample Presentation Report</p>
                            <div class="sim-button button1" data-toggle="modal" data-target="#getQuoteModal">Download Report</div>
                          </div>
                        </div>

CSS:

    .media {
  display: inline-block;
  position: relative;
  vertical-align: top;
}

.media__image { display: block; }

.media__body {
  background: rgba(41, 128, 185, 0.7);
  bottom: 0;
  color: white;
  font-size: 1em;
  left: 0;
  opacity: 0;
  overflow: hidden;
  padding: 3.75em 3em;
  position: absolute;
  text-align: center;
  top: 0;
  right: 0;
  -webkit-transition: 0.6s;
  transition: 0.6s;
}

.media__body:hover { opacity: 1; }

.media__body:after,
.media__body:before {
  border: 1px solid rgba(255, 255, 255, 0.7);
  bottom: 1em;
  content: '';
  left: 1em;
  opacity: 0;
  position: absolute;
  right: 1em;
  top: 1em;
  -webkit-transform: scale(1.5);
  -ms-transform: scale(1.5);
  transform: scale(1.5);
  -webkit-transition: 0.6s 0.2s;
  transition: 0.6s 0.2s;
}

.media__body:before {
  border-bottom: none;
  border-top: none;
  left: 2em;
  right: 2em;
}

.media__body:after {
  border-left: none;
  border-right: none;
  bottom: 2em;
  top: 2em;
}

.media__body:hover:after,
.media__body:hover:before {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  opacity: 1;
}

.media__body h2 { margin-top: 0; }

.media__body p { margin-bottom: 0em; }





.sim-button{
    line-height: 50px;
    height: 50px;
    text-align: center;
    margin-right: auto;
    margin-left: auto;
    margin-top: 125px;
    width: 60%;
    cursor: pointer;
}
.button1 {
    color: rgba(255,255,255,1);
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
    -o-transition: all 0.5s;
    transition: all 0.5s;
    position: relative;
    border: 1px solid rgba(255,255,255,0.5);
}
.button1 a{
    color: rgba(51,51,51,1);
    text-decoration: none;
    display: block;
}
.button1:hover {
    background-color: rgba(255,255,255,0.2);
    -webkit-border-radius: 25px;
    -moz-border-radius: 25px;
    border-radius: 25px;    
}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您的按钮隐藏在叠加层下方:

z-index: 1;

将上述内容放入你的sim-button类可以获得你想要的效果。它将您的按钮带到前台,并且可以悬停以激活动画。

这里有一个小提琴我添加了更改: jsfiddle

答案 1 :(得分:0)

另一种方法,如果您希望文本可以选择并且按钮效果可以使用并且可以点击,则可以在z-index: 1上设置.media__body,然后在z-index: -1上设置::before/::after {1}}伪元素。这会将伪元素放在.media__body中的所有内容后面,但不要让它们落后于.media__body

&#13;
&#13;
.media {
  display: inline-block;
  position: relative;
  vertical-align: top;
}

.media__image {
  display: block;
}

.media__body {
  background: rgba(41, 128, 185, 0.7);
  bottom: 0;
  color: white;
  font-size: 1em;
  left: 0;
  opacity: 0;
  overflow: hidden;
  padding: 3.75em 3em;
  position: absolute;
  text-align: center;
  top: 0;
  right: 0;
  -webkit-transition: 0.6s;
  transition: 0.6s;
  z-index: 1;
}

.media__body:hover {
  opacity: 1;
}

.media__body:after, .media__body:before {
  border: 1px solid rgba(255, 255, 255, 0.7);
  bottom: 1em;
  content: '';
  left: 1em;
  opacity: 0;
  position: absolute;
  right: 1em;
  top: 1em;
  -webkit-transform: scale(1.5);
  -ms-transform: scale(1.5);
  transform: scale(1.5);
  -webkit-transition: 0.6s 0.2s;
  transition: 0.6s 0.2s;
  z-index: -1;
}

.media__body:before {
  border-bottom: none;
  border-top: none;
  left: 2em;
  right: 2em;
}

.media__body:after {
  border-left: none;
  border-right: none;
  bottom: 2em;
  top: 2em;
}

.media__body:hover:after, .media__body:hover:before {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  opacity: 1;
}

.media__body h2 {
  margin-top: 0;
}

.media__body p {
  margin-bottom: 0em;
}

.sim-button {
  line-height: 50px;
  height: 50px;
  text-align: center;
  margin-right: auto;
  margin-left: auto;
  margin-top: 25px;
  width: 60%;
  cursor: pointer;
}
.button1 {
  color: rgba(255, 255, 255, 1);
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.5);
}
.button1 a {
  color: rgba(51, 51, 51, 1);
  text-decoration: none;
  display: block;
}
.button1:hover {
  background-color: rgba(255, 255, 255, 0.2);
  -webkit-border-radius: 25px;
  -moz-border-radius: 25px;
  border-radius: 25px;
}
&#13;
<div class="media" style="background-color:#000;">
  <img class="media__image" src="https://www.offensive-security.com/wp-content/uploads/2012/01/security-risk-assessment-report.png">
  <div class="media__body">
    <h2>Penetration Report</h2>
    <p>Get your hands on our Sample Presentation Report</p>
    <div class="sim-button button1" data-toggle="modal" data-target="#getQuoteModal">Download Report</div>
  </div>
</div>
&#13;
&#13;
&#13;