【问题标题】:Css Arrow TransitionCSS箭头过渡
【发布时间】:2019-09-19 16:42:48
【问题描述】:

我有一个从右到左的 CSS 箭头。我首先有箭头,然后是文本。我试图在悬停时实现以下过渡:悬停,左侧的箭头消失并出现在文本之后的右侧。如何实现鼠标悬停时箭头出现在右侧的效果?

我正在尝试实现this 效果,但方向相反

body {
  background: black;
}

.the-arrow {
  width: 64px;
  transition: all 0.2s;
  transform: rotate(180deg);
}

.the-arrow.-left {
  position: absolute;
  top: 60%;
  right: 0;
}

.the-arrow.-left .shaft {
  width: 0;
  background-color: #999;
}

.the-arrow.-left .shaft:before,
.the-arrow.-left .shaft:after {
  width: 0;
  background-color: #999;
}

.the-arrow.-left .shaft:before {
  -webkit-transform: rotate(0);
  transform: rotate(0);
}

.the-arrow.-left .shaft:after {
  -webkit-transform: rotate(0);
  transform: rotate(0);
}

.the-arrow.-right {
  top: 3px;
}

.the-arrow.-right .shaft {
  width: 64px;
  transition-delay: 0.2s;
}

.the-arrow.-right .shaft:before,
.the-arrow.-right>.shaft:after {
  width: 8px;
  transition-delay: 0.3s;
  transition: all 0.5s;
}

.the-arrow.-right .shaft:before {
  -webkit-transform: rotate(40deg);
  transform: rotate(40deg);
}

.the-arrow.-right .shaft:after {
  -webkit-transform: rotate(-40deg);
  transform: rotate(-40deg);
}

.the-arrow .shaft {
  background-color: #fff;
  display: block;
  height: 1px;
  position: relative;
  transition: all 0.2s;
  transition-delay: 0;
  will-change: transform;
}

.the-arrow .shaft:before,
.the-arrow .shaft:after {
  background-color: #fff;
  content: '';
  display: block;
  height: 1px;
  position: absolute;
  top: 0;
  right: 0;
  transition: all 0.2s;
  transition-delay: 0;
}

.the-arrow .shaft:before {
  -webkit-transform-origin: top right;
  transform-origin: top right;
}

.the-arrow .shaft:after {
  -webkit-transform-origin: bottom right;
  transform-origin: bottom right;
}

.animated-arrow {
  display: inline-block;
  color: #fff;
  font-size: 12px;
  text-decoration: none;
  position: relative;
  transition: all 0.2s;
}

.animated-arrow:hover {
  color: #eaeaea;
}

.animated-arrow:hover .the-arrow.-left .shaft {
  width: 64px;
  transition-delay: 0.1s;
  background-color: #eaeaea;
}

.animated-arrow:hover .the-arrow.-left .shaft:before,
.animated-arrow:hover .the-arrow.-left .shaft:after {
  width: 8px;
  transition-delay: 0.1s;
  background-color: #eaeaea;
}

.animated-arrow:hover .the-arrow.-left .shaft:before {
  -webkit-transform: rotate(40deg);
  transform: rotate(40deg);
}

.animated-arrow:hover .the-arrow.-left .shaft:after {
  -webkit-transform: rotate(-40deg);
  transform: rotate(-40deg);
}

.animated-arrow:hover .main {
  -webkit-transform: translateX(80px);
  transform: translateX(80px);
}

.animated-arrow:hover .main .the-arrow.-right .shaft {
  width: 0;
  -webkit-transform: translateX(200%);
  transform: translateX(200%);
  transition-delay: 0;
}

.animated-arrow:hover .main .the-arrow.-right .shaft:before,
.animated-arrow:hover .main .the-arrow.-right .shaft:after {
  width: 0;
  transition-delay: 0;
  transition: all 0.1s;
}

.animated-arrow:hover .main .the-arrow.-right .shaft:before {
  -webkit-transform: rotate(0);
  transform: rotate(0);
}

.animated-arrow:hover .main .the-arrow.-right .shaft:after {
  -webkit-transform: rotate(0);
  transform: rotate(0);
}

.animated-arrow .main {
  display: flex;
  align-items: center;
  transition: all 0.2s;
}

.animated-arrow .main .text {
  margin: 0 0 0 16px;
  line-height: 1;
  font-family: 'Montserrat';
  text-transform: uppercase;
}

.animated-arrow .main .the-arrow {
  position: relative;
}
<div class="pivot-arrows-1" style="">


  <a class="animated-arrow arrow-c-f" href="#">

    <span class="main">
      <span class="the-arrow -right">
        <span class="shaft"></span>
      </span>

      <span class="text">
        Some cool Text
      </span>

    </span>
    <span class="the-arrow -left">
      <span class="shaft"></span>
    </span>
  </a>
</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    只是为了好玩,用一个伪和过渡使它更短

    body {
      background: black;
      color:purple;
      font-size:2em;
    }
    
    a {
      color: #fff;
      font-size: 12px;
      text-decoration: none;
      position: relative;/* to be the reference for the absolute pseudo */
      display:inline-block;/* to size it and set horizontal margin */
      vertical-align:middle;
      background:black;
      margin:0 68px;
      padding:0 0.25em;
    }
    
    .animated-arrow::before {
      content:'\0003c';/* the sign < to draw edge of the arrow */
      font-size:16px;/* might need to be resized */
      /*display:inline-block; if not absolute */
      width:64px;/* that was your value, equal at the most to the margin of parent link  */
      background:linear-gradient(to bottom, transparent 50%,currentcolor 50%,  currentcolor calc(50% + 1px) , transparent  calc(50% + 1px));/* to draw the body arrow */
      top:-0.15em;/* if needed */
      position:absolute;/* , so coordonate are easier to set */
      z-index:-1;/* let slide under the link */
      right:100%;
      transition: right  0.2s, transform 0.1s 0s;
    }
    .animated-arrow:hover::before {
      right:-64px;
      transform:scale(-1,1)
    } 
    a+a {color:gold;}
    <a class="animated-arrow arrow-c-f" href="#">Some cool Text</a> ⊛
    <a class="animated-arrow arrow-c-f" href="#">Some cool Text of any length</a>

    【讨论】:

    • 谢谢。我试图达到这种效果,但方向相反
    • 谢谢。我在下面发布了一个解决方案
    【解决方案2】:

    使用单个元素

    *{  
      box-sizing: border-box
    }
    body{padding: 4rem}
    .arrow{
      position: relative; 
      display:inline-block
    }
    
    .arrow:before,
    .arrow:after{
      content: "";
      position: absolute;
      top: 50%;
      transition: all 0.2s;
    }
    .arrow:before{
      right: 100%;
      width:32px;
      height:1px;
      background: black;
    }
    .arrow:after{
      width:12px;
      height: 12px;
      border-top: 1px solid black;
      border-left: 1px solid black;
      right: calc( 100% + 16px );
      transform: rotate(-45deg);
      margin-top: -6px;
    }
    
    .arrow:hover:before{
      right: -32px
    }
    .arrow:hover:after{
      transform: rotate(-45deg) scale(-1);
      right: -32px;
    }
    &lt;div class="arrow"&gt;some cool text&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      我删除了一些元素,有点用箭头作弊。

      /* So it's easier to change spacing */
      :root {
        --spacing: 1rem;
        --spacing-half: calc(var(--spacing) / 2);
        --spacing-quarter: calc(var(--spacing) / 4);
      }
      
      body {
        background: black;
        color: #fff;
        font-family: 'Montserrat';
      }
      
      a {
        font-size: 12px;
        text-decoration: none;
        color: #fff;
      }
      
      .text {
        padding: var(--spacing-quarter);
        text-transform: uppercase;
      }
      
      .text,
      .animated-arrows
      {
        display: inline-block;
      }
      
      .animated-arrows::before,
      .animated-arrows::after {
        position: relative;
        vertical-align: text-bottom;
        
        transition: left 600ms, opacity 800ms;
      }
      
      .animated-arrows::before {
        content: '⟵';
        left: 0rem;
      }
      
      .animated-arrows::after {
        content: '⟶';
        opacity: 0;
        left: calc(-1 * var(--spacing-half));
      }
      
      /* Animation changes */
      .animated-arrows:hover::before {
        left: var(--spacing-half);
        opacity: 0;
      }
      
      .animated-arrows:hover::after {
        left: 0rem;
        opacity: 1;
      }
        <a href="#">
          <div class="animated-arrows">
            <div class="text">Some cool Text</div>
          </div>
        </a>

      【讨论】:

        【解决方案4】:

        所以我已经设法解决了这个问题:

        body {
          background: black;
        }
        
        .left-arrow .the-arrow {
          width: 64px;
          transition: all 0.2s;
          transform: rotate(180deg);
        }
        
        .left-arrow .the-arrow.-left {
          top: 3px;
        }
        
        .left-arrow .the-arrow.-left .shaft {
          width: 64px;
          transition-delay: 0.2s;
        }
        
        .left-arrow .the-arrow.-left .shaft:before,
        .left-arrow .the-arrow.-left>.shaft:after {
          width: 8px;
          transition-delay: 0.3s;
          transition: all 0.5s;
        }
        
        .left-arrow .the-arrow.-left .shaft:before {
          -webkit-transform: rotate(40deg);
          transform: rotate(40deg);
        }
        
        .left-arrow .the-arrow.-left .shaft:after {
          -webkit-transform: rotate(-40deg);
          transform: rotate(-40deg);
        }
        
        .left-arrow .animated-arrow:hover .main .the-arrow.-left .shaft {
          width: 0;
          -webkit-transform: translateX(200%);
          transform: translateX(200%);
          transition-delay: 0;
        }
        
        .left-arrow .animated-arrow:hover .main .the-arrow.-left .shaft:before,
        .left-arrow .animated-arrow:hover .main .the-arrow.-left .shaft:after {
          width: 0;
          transition-delay: 0;
          transition: all 0.1s;
        }
        
        .left-arrow .animated-arrow:hover .main .the-arrow.-left .shaft:before {
          -webkit-transform: rotate(0);
          transform: rotate(0);
        }
        
        .left-arrow .animated-arrow:hover .main .the-arrow.-left .shaft:after {
          -webkit-transform: rotate(0);
          transform: rotate(0);
        }
        
        .left-arrow .the-arrow .shaft {
          background-color: #fff;
          display: block;
          height: 1px;
          position: relative;
          transition: all 0.2s;
          transition-delay: 0;
          will-change: transform;
        }
        
        .left-arrow .the-arrow .shaft:before,
        .left-arrow .the-arrow .shaft:after {
          background-color: #fff;
          content: '';
          display: block;
          height: 1px;
          position: absolute;
          top: 0;
          right: 0;
          transition: all 0.2s;
          transition-delay: 0;
        }
        
        .left-arrow .the-arrow .shaft:before {
          -webkit-transform-origin: top right;
          transform-origin: top right;
        }
        
        .left-arrow .the-arrow .shaft:after {
          -webkit-transform-origin: bottom right;
          transform-origin: bottom right;
        }
        
        .left-arrow .animated-arrow {
          display: inline-block;
          color: #fff;
          font-size: 12px;
          text-decoration: none;
          position: relative;
          transition: all 0.2s;
        }
        
        .left-arrow .animated-arrow:hover {
          color: #eaeaea;
        }
        
        .left-arrow .animated-arrow .main {
          display: flex;
          align-items: center;
          transition: all 0.2s;
        }
        
        .left-arrow .animated-arrow .main .text {
          margin: 0 16px 0 16px;
          line-height: 1;
          font-family: 'Montserrat';
          text-transform: uppercase;
        }
        
        .left-arrow .animated-arrow .main .the-arrow {
          position: relative;
        }
        
        .left-arrow .animated-arrow:hover .the-arrow.-left .shaft {
          width: 0px;
        }
        
        .left-arrow .animated-arrow:hover .the-arrow.-left {
          width: 0px;
        }
        
        .the-arrow.-right {
          display: none;
        }
        
        .left-arrow .animated-arrow:hover .the-arrow.-right {
          position: absolute;
          display: inline-block;
          right: -64px;
          top: 60%;
          padding-left: -16px
        }
        
        .left-arrow .the-arrow.-right .shaft:before,
        .left-arrow .the-arrow.-right>.shaft:after {
          width: 8px;
          transition-delay: 0.3s;
          transition: all 0.5s;
        }
        
        .left-arrow .the-arrow.-right .shaft:before {
          -webkit-transform: rotate(40deg);
          transform: rotate(40deg);
        }
        
        .left-arrow .the-arrow.-right .shaft:after {
          -webkit-transform: rotate(-40deg);
          transform: rotate(-40deg);
        }
        <div class="left-arrow">
        
          <a class="animated-arrow arrow-c-f" href="#">
        
            <span class="main">
              <span class="the-arrow -left">
                <span class="shaft"></span>
            </span>
        
            <span class="text">
                Some Text
              </span>
        
            </span>
            <span class="the-arrow -right">
                <span class="shaft"></span>
            </span>
        
        
          </a>
        </div>

        【讨论】: