【问题标题】:Why does CSS transition-duration not work on child elment?为什么 CSS 过渡持续时间不适用于子元素?
【发布时间】:2020-01-29 00:24:08
【问题描述】:

我的过渡没有应用到绝对位置子 div 上的问题。

这是我的代码笔:

https://codepen.io/Data-Mastery/pen/oNvRdGv

在 SASS 文件的第 213 行 (&:hover .image) 上,我想缩放图像并设置一个过滤器,效果很好,但没有应用过渡持续时间。

如果我只是在父元素而不是图像上应用悬停语句,则过渡效果很好。这里有什么问题,谁能帮帮我?

【问题讨论】:

    标签: css css-transitions transition


    【解决方案1】:

    只需将过渡添加到 .image 类(或悬停功能,无论您想实现什么)

          &:hover .image {
            transform: scale(1.1);
            filter: grayscale(50%);
            transition: your parameter;
          }
    

    【讨论】:

      【解决方案2】:

      添加过渡属性到

      & .image {
              position: absolute;
              top: 0;
              left: 0;
              width: 100%;
              margin-bottom: 1.5rem;
              transition:0.5s;
            }
      

      这对你有用

      【讨论】:

        【解决方案3】:

        为什么 man ,它有效,请参阅:-

        @import url("https://fonts.googleapis.com/css?family=Open+Sans&display=swap");
        @keyframes moveInLeft {
          0% {
            transform: translateX(-200px);
            opacity: 0;
          }
          80% {
            transform: translateX(10px);
            opacity: 0.8;
          }
          100% {
            transform: translateX(0px);
            opacity: 1;
          }
        }
        @keyframes moveInRight {
          0% {
            transform: translateX(200px);
            opacity: 0;
          }
          80% {
            transform: translateX(-10px);
            opacity: 0.8;
          }
          100% {
            transform: translateX(0px);
            opacity: 1;
          }
        }
        @keyframes moveInBottom {
          0% {
            transform: translateY(100px);
            opacity: 0;
          }
          100% {
            transform: translateY(0px);
            opacity: 1;
          }
        }
        .leftanimation {
          animation: moveInLeft 1.2s forwards;
        }
        
        * {
          padding: 0;
          margin: 0;
          box-sizing: border-box;
        }
        
        body {
          font-family: "Open Sans", sans-serif;
          line-height: 1.4rem;
        }
        
        li {
          list-style: none;
        }
        
        a {
          text-decoration: none;
        }
        
        p {
          margin: 0.75rem;
        }
        
        .l-heading-light {
          font-size: 3rem;
          color: white;
          padding-top: 3rem;
          padding-bottom: 1rem;
        }
        
        .l-heading-dark {
          font-size: 3rem;
          color: #c2453b;
          padding-top: 3rem;
          padding-bottom: 1rem;
        }
        
        .primary-btn {
          display: inline-block;
          background: #c2453b;
          color: #fff;
          padding: 0.5rem 2rem;
          font-size: 1.25rem;
          border-radius: 20px;
          transition: all 0.3s ease-in;
        }
        .primary-btn:hover {
          background: #fff;
          color: #c2453b;
          transform: translateY(-3px);
          box-shadow: 0 10px 15px rgba(0, 0, 0, 0.5);
        }
        .primary-btn:active {
          transform: translateY(-1px);
          box-shadow: 0 5px 10px rgba(0, 0, 0, 0.5);
        }
        
        .navbar {
          display: flex;
          z-index: 1;
          justify-content: space-between;
          align-items: center;
          width: 100%;
          position: fixed;
          top: 0;
          padding: 1rem;
          background: #333;
          color: #fff;
        }
        .navbar__nav {
          display: flex;
          justify-content: space-between;
        }
        .navbar__nav li a {
          color: #fff;
          padding: 0.75rem;
          margin: 0 0.25rem;
        }
        .navbar__nav li a:hover {
          background: #fff;
          border-radius: 5px;
          color: #333;
        }
        
        .showcase {
          background-image: linear-gradient(to bottom right, rgba(194, 69, 59, 0.4), rgba(178, 86, 65, 0.4)), url("https://thumbs.dreamstime.com/z/hiking-forest-man-morning-mist-travel-concept-45457025.jpg");
          background-size: cover;
          background-position: top;
          height: 100vh;
          position: relative;
        }
        .showcase__content {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          text-align: center;
          text-transform: uppercase;
        }
        .showcase__content p {
          color: white;
          font-size: 1.2rem;
          margin-bottom: 1rem;
          animation: moveInRight 1.2s forwards;
        }
        .showcase__content a {
          animation: moveInBottom 1.2s 0.8s linear backwards;
        }
        
        #tours {
          text-align: center;
          background: #f7f7f7;
        }
        #tours .container {
          width: 100%;
          height: 100%;
          margin: 0 auto;
          display: block;
          max-width: 1200px;
          justify-content: space-between;
          display: flex;
          color: #fff;
        }
        #tours .container .card {
          position: relative;
          height: 35rem;
          width: 20rem;
          margin: 2rem 1rem;
          padding: 0;
          background: #b25641;
          box-shadow: 0 10px 15px rgba(0, 0, 0, 0.4);
          transition: all 0.4s ease-in-out;
          overflow: hidden;
        }
        #tours .container .card:hover .image {
          transform: scale(1.1);
          filter: grayscale(100%);
          transition-duration: 0.5s;
        }
        #tours .container .card .image {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          margin-bottom: 1.5rem;
        }
        #tours .container .card__content {
          position: absolute;
          left: 0;
          right: 0;
          margin-left: auto;
          margin-right: auto;
          margin-top: 15rem;
        }
        <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <link rel="stylesheet" href="css/style.css" />
            <title>Malamar Hotels</title>
          </head>
          <body>
            <nav class="navbar">
              <h1 class="navbar__logo">
                Malamar
              </h1>
              <ul class="navbar__nav">
                <li><a href="#home">Home</a></li>
                <li><a href="#about">Tours</a></li>
                <li><a href="#contact">Contact</a></li>
              </ul>
            </nav>
        
            <header class="showcase">
              <div class="showcase__content">
                <h1 class="l-heading-light leftanimation">
                  Hiking this fall
                </h1>
                <p>
                  Book the best hikings tours for this fall in beautiful areas across
                  europe
                </p>
                <a href="#" class="primary-btn">See our tours</a>
              </div>
            </header>
        
            <section id="tours">
              <h1 class="l-heading-dark">Our best offers</h1>
              <div class="container">
                <div class="card">
                  <img src="https://thumbs.dreamstime.com/z/hiking-forest-man-morning-mist-travel-concept-45457025.jpg" alt="Hi" class="image" />
                  <div class="card__content">
                    <h1>Hiking</h1>
                    <p>2-day tour</p>
                    <a href="#" class="primary-btn">Book now!</a>
                  </div>
                </div>
                <div class="card">
                  <img src="https://thumbs.dreamstime.com/z/hiking-forest-man-morning-mist-travel-concept-45457025.jpg" alt="Hi" class="image" />
                  <div class="card__content">
                    <h1>Rafting tour</h1>
                    <p>3-day tour</p>
                    <a href="#" class="primary-btn">Book now!</a>
                  </div>
                </div>
                <div class="card">
                  <img src="https://thumbs.dreamstime.com/z/hiking-forest-man-morning-mist-travel-concept-45457025.jpg" alt="Hi" class="image" />
                  <div class="card__content">
                    <h1>Mountainbiking</h1>
                    <p>5-day tour</p>
                    <a href="#" class="primary-btn">Book now!</a>
                  </div>
                </div>
              </div>
            </section>
        
          </body>
        </html>

        【讨论】:

          猜你喜欢
          • 2016-02-13
          • 2014-02-17
          • 1970-01-01
          • 1970-01-01
          • 2018-10-20
          • 2020-11-08
          • 1970-01-01
          • 2018-08-12
          • 1970-01-01
          相关资源
          最近更新 更多