【问题标题】:Css set delay on selected element in :hoverCss在:悬停中的选定元素上设置延迟
【发布时间】:2018-01-29 21:38:39
【问题描述】:

我有一个框和一个“某物”按钮,每当有人将鼠标悬停在“onebysign”div 上时,元素的高度就会增加并出现一些文本,问题是文本在您将鼠标悬停在元素上后立即出现,但是当发生这种情况时,它看起来很丑,因为文本基本上是从无处出现的,当用户停止将鼠标悬停在元素上时也是如此。我希望文本出现延迟,就像 div 发生的情况一样。

HTML 和 CSS:

body {
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  background-color: #f2f2f2;
}

.events {
  padding: 20px 100px;
}

.textInfo {
  text-transform: uppercase;
  font-weight: 600;
  color: #085DAD;
  padding: 10px;
  background-color: white;
}

.onebyone {
  background-color: grey;
  width: 100%;
  padding-top: 100%;
  position: relative;
  background-size: cover;
}

.onebytext {
  position: absolute;
  top: 10px;
  left: 0;
  bottom: 0;
  right: 0;
  text-align: center;
  font-size: 32px;
  color: white;
  width: 90%;
  left: 5%;
}

.onebysign {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  height: 60px;
  text-align: center;
  font-size: 20px;
  background-color: white;
  font-size: 24px;
  transition: height 1s;
}

.onebytext,
.onebysign {
  position: absolute;
  text-align: center;
  color: white;
}

.onebysign:hover {
  height: 300px;
}

.onebyform {
  left: 0;
  right: 0;
  bottom: 0;
  position: absolute;
}

.submitBtn {
  background-color: #0099CC;
  text-transform: uppercase;
  padding: 10px;
  border-radius: 50px;
  border: 0px;
  width: 70%;
  margin: 10px 0;
}

.onebysign:before {
  content: "";
  height: 30px;
  width: 100%;
  position: absolute;
  background: linear-gradient(to top right, white 47%, transparent 50%);
  left: 0;
  top: -30px;
}

.signText {
  display: none;
  color: black;
  font-size: 18px;
  padding: 10px 20px;
  font-weight: 600;
  text-align: center;
  max-width: 400px;
}

.onebysign:hover .signText {
  transition-delay: 1s;
  display: inline-block;
}
<div class="row events">
  <div class="onebyone">
    <div class="onebytext">
      <div class="textInfo">Test</div>
    </div>
    <div class="onebysign">
      <span class="signText">This is a sample text. It looks pretty good now, but the longer you make the text, the more ugly it looks...</span>
      <form class="onebyform" action="<?php echo get_permalink(); ?>">
        <button class="submitBtn">Something</button>
      </form>
    </div>
  </div>
</div>

【问题讨论】:

    标签: html css text hover


    【解决方案1】:

    你可以用它做很多事情。这是一个例子。

    .onebysign {
      ...
      overflow: hidden;
    }
    
    .signText {
      opacity: 0;
      ...
    }
    
    .onebysign:hover .signText {
      ...
      opacity: 1;
    }
    

    Fiddle demo

    【讨论】:

      【解决方案2】:

      您似乎在signText 元素上尝试了transition-delay,这是一个正确方向的开始。

      我们将转换 opacity 属性,而不是转换 display 属性。像这样:

      body {
        font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
        background-color: #f2f2f2;
      }
      
      .events {
        padding: 20px 100px;
      }
      
      .textInfo {
        text-transform: uppercase;
        font-weight: 600;
        color: #085DAD;
        padding: 10px;
        background-color: white;
      }
      
      .onebyone {
        background-color: grey;
        width: 100%;
        padding-top: 100%;
        position: relative;
        background-size: cover;
      }
      
      .onebytext {
        position: absolute;
        top: 10px;
        left: 0;
        bottom: 0;
        right: 0;
        text-align: center;
        font-size: 32px;
        color: white;
        width: 90%;
        left: 5%;
      }
      
      .onebysign {
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
        height: 60px;
        color: #ffffff;
        text-align: center;
        font-size: 20px;
        background-color: white;
        font-size: 24px;
        transition: all 1s;
      }
      
      .onebytext,
      .onebysign {
        position: absolute;
        text-align: center;
        color: white;
      }
      
      .onebysign:hover {
        height: 300px;
        color: #000000;
      }
      
      .onebyform {
        left: 0;
        right: 0;
        bottom: 0;
        position: absolute;
      }
      
      .submitBtn {
        background-color: #0099CC;
        text-transform: uppercase;
        padding: 10px;
        border-radius: 50px;
        border: 0px;
        width: 70%;
        margin: 10px 0;
      }
      
      .onebysign:before {
        content: "";
        height: 30px;
        width: 100%;
        position: absolute;
        background: linear-gradient(to top right, white 47%, transparent 50%);
        left: 0;
        top: -30px;
      }
      
      .signText {
        color: black;
        font-size: 18px;
        padding: 10px 20px;
        font-weight: 600;
        text-align: center;
        max-width: 400px;
        opacity: 0;
        transition: all linear 0.2s;
      }
      
      .onebysign:hover .signText {
        transition-delay: 1s;
        opacity: 1;
      }
      <div class="row events">
        <div class="onebyone">
          <div class="onebytext">
            <div class="textInfo">Test</div>
          </div>
          <div class="onebysign">
            <span class="signText">This is a sample text. It looks pretty good now, but the longer you make the text, the more ugly it looks...</span>
            <form class="onebyform" action="<?php echo get_permalink(); ?>">
              <button class="submitBtn">Something</button>
            </form>
          </div>
        </div>
      </div>

      【讨论】:

      • 主要内容是确保正在转换的属性是animatable propertyopacity 是,display 不是。
      猜你喜欢
      • 2015-12-12
      • 2018-12-19
      • 2014-09-10
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 2018-05-03
      • 2021-02-24
      相关资源
      最近更新 更多