首页 > 解决方案 > 响应式按钮动画 CSS

问题描述

我正在尝试将一个按钮的外观与另一个按钮的响应性结合起来:

按钮 A:https ://codepen.io/vitor-siqueira/pen/xNBExN

按钮 B:https ://codepen.io/AnthonyBmm/pen/poooJmO

我想制作按钮 C,它的外观和感觉与按钮 A 完全相同,但它会自动调整大小以适应按钮文本(没有换行,如按钮 B)。目前,我创建了 3-4 个几乎相同的按钮 A,并调整了 SVG 和 CSS 的宽度值,这……是一种糟糕的做法。

我找到了具有类似动画但没有 SVG 的 Button B,并认为尝试复制 Button A 效果可能是一个好的开始,但我未能成功。

有人可以帮忙吗?


2支笔的附加代码可以在下面找到:

按钮 A HTML:

  <div class="container">
    <div class="center">
      <button class="btn">
        <svg width="180px" height="60px" viewBox="0 0 180 60" class="border">
          <polyline points="179,1 179,59 1,59 1,1 179,1" class="bg-line" />
          <polyline points="179,1 179,59 1,59 1,1 179,1" class="hl-line" />
        </svg>
        <span>HOVER ME</span>
      </button>
    </div>
  </div>

按钮 A CSS:

@import url('https://fonts.googleapis.com/css?family=Lato:100&display=swap');

body, html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: #5CA4EA;
  overflow: hidden;
  font-family: 'Lato', sans-serif;
}

.container {
  width: 400px;
  height: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.center {
  width: 180px;
  height: 60px;
  position: absolute;
}

.btn {
  width: 180px;
  height: 60px;
  cursor: pointer;
  background: transparent;
  border: 1px solid #91C9FF;
  outline: none;
  transition: 1s ease-in-out;
}

svg {
  position: absolute;
  left: 0;
  top: 0;
  fill: none;
  stroke: #fff;
  stroke-dasharray: 150 480;
  stroke-dashoffset: 150;
  transition: 1s ease-in-out;
}

.btn:hover {
  transition: 1s ease-in-out;
  background: #4F95DA;
}

.btn:hover svg {
  stroke-dashoffset: -480;
}

.btn span {
  color: white;
  font-size: 18px;
  font-weight: 100;
}

按钮 B HTML:

<body>
    <a href="#">push this and that
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </a>
</body>

按钮 B CSS:

* {
    box-sizing: border-box;
}
body {
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #999;
}

a {
    /*border-radius: 12px;*/
    border: 3px outset #888;
    position: relative;
    display: inline-block;
    padding: 15px 30px;
    color: #eee;
    text-transform: uppercase;
    letter-spacing: 4px;
    overflow: hidden;
    
    box-shadow: 0 0 10px rgb(0, 0, 0, 1);
    font-family: verdana;
    font-size: 28px;
    font-weight: bolder;
    text-decoration: none;
    background:linear-gradient(160deg, #666, #444);
    text-shadow: 0px 0px 2px rgba(0, 0, 0, .5);

    transition: 0.2s;

}
a:active {
    border: 3px outset #ddd;
    color: #fff;
    background: linear-gradient(160deg, #666, #444);
    text-shadow: 0px 0px 4px #ccc;
    box-shadow: 0 0 10px #fff, 0 0 40px #fff, 0 0 80px #fff;
    transition-delay: 1s;
}

a span {
    position: absolute;
    display: block;
}

a span:nth-child(1) {
    top: 0;
    left: -100%;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, #eee);
}
a:active span:nth-child(1) {
    left: 100%;
    transition: 1s;
}
a span:nth-child(2) {
    top: -100%;
    right: 0;
    width: 2px;
    height: 100%;
    background: linear-gradient(180deg, transparent, #eee);
}
a:active span:nth-child(2) {
    top: 100%;
    transition: 1s;
    transition-delay: 0.25s;
}
a span:nth-child(3) {
    bottom: 0;
    right: -100%;
    width: 100%;
    height: 2px;
    background: linear-gradient(270deg, transparent, #eee);
}
a:active span:nth-child(3) {
    right: 100%;
    transition: 1s;
    transition-delay: 0.5s;
}

a span:nth-child(4) {
    bottom: -100%;
    left: 0;
    width: 2px;
    height: 100%;
    background: linear-gradient(360deg, transparent, #eee);
}
a:active span:nth-child(4) {
    bottom: 100%;
    transition: 1s;
    transition-delay: 0.75s;
}

标签: htmlcsssvgbuttonhover

解决方案


我的这个“更正”是基于这个帖子这个帖子让 svg 负责。

对不起谷歌翻译的英语。

@import url('https://fonts.googleapis.com/css?family=Lato:100&display=swap');

body, html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: #5CA4EA;
  overflow: hidden;
  font-family: 'Lato', sans-serif;
}

.container {
  width: 400px;
  height: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn {
  cursor: pointer;
  background: transparent;
  border: 1px solid #91C9FF;
  outline: none;
  transition: 1s ease-in-out;
    padding:3%;
  padding-top:2%;
  padding-bottom:2%;
    position: relative;
    display: flex;
}

svg {
    width: calc(100% + 2px);
  height: calc(100% + 2px);
  position: absolute;
  left: -1px;
  top: -1px;
  fill: none;
  stroke: #fff;
  stroke-dasharray: 150 480;
  stroke-dashoffset: 150;
  transition: 1s ease-in-out;
}

.btn:hover {
  transition: 1s ease-in-out;
  background: #4F95DA;
}

.btn:hover svg {
  stroke-dashoffset: -480;
}

.bg-line{
  width:100px;
  height:100px;
}

.btn span {
  color: white;
  font-size: 18px;
  font-weight: 100;
}
<div class="container">
        
    
      <button class="btn">
            
<!-- edit -->
        <svg viewBox="0 0 180 60" class="border" preserveAspectRatio="none" class="border">
      
       <polyline points="179,1 179,59 1,59 1,1 179,1" class="bg-line" />
      <polyline points="179,1 179,59 1,59 1,1 179,1" class="hl-line" />
    
    </svg>
                    
        <span>Junior is AWESOME</span>
      </button>
    
        
  </div>

  • 或者

我真的很想在不使用 SVG 的情况下找到一种更正确的方法,我研究了一整天,这就是我得到的:

我使用了 Animation、Before 和 Linear-gradient 属性。动画不是很流畅,因为我不是很擅长它,最重要的是我建议只为关键帧使用一个文件。

(“后退”动画可以稍后包含:D)来自谷歌翻译的英语

@import url('https://fonts.googleapis.com/css?family=Lato:100&display=swap');

body, html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: #5CA4EA;
  overflow: hidden;
  font-family: 'Lato', sans-serif;
}

.container {
  width: 400px;
  height: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn {
  cursor: pointer;
  background: rgb(99, 169, 235);
  border: 1px solid #91C9FF;
  outline: none;
  transition: 1s ease-in-out;
    padding:3%;
  padding-top:2%;
  padding-bottom:2%;
    position: relative;
    display: flex;
}

.btn::before {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: -1;
    margin: -3px; /* !importanté */
    border-radius: inherit; /* !importanté */
    background: linear-gradient(0deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0) 99%, rgba(255,255,255,0) 100%);
  }

  .btn:hover::before{

-webkit-animation: all 1s; /* Chrome, Safari, Opera */ 
    animation: all 1s;
    transition:animation 1s ease-in-out;

  }

  @keyframes all {
    2%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.3) 99%, rgba(255,255,255,0) 100%);
   }
   5%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.5) 99%, rgba(255,255,255,0) 100%);
   }

    7%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   14%   {
      background: linear-gradient(-23deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   22%   {
      background: linear-gradient(0deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   28%   {
      background: linear-gradient(23deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   35%   {
      background: linear-gradient(45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   43%   {
      background: linear-gradient(90deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   50%   {
      background: linear-gradient(120deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   57%   {
      background: linear-gradient(145deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   64%   {
      background: linear-gradient(180deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   71%   {
      background: linear-gradient(200deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   78%   {
      background: linear-gradient(220deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   85%   {
      background: linear-gradient(300deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   92%   {
      background: linear-gradient(320deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   94%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.9) 99%, rgba(255,255,255,0) 100%);
   }

   97%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.5) 99%, rgba(255,255,255,0) 100%);
   }

   100%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.3) 99%, rgba(255,255,255,0) 100%);
   }

}

/* Chrome, Safari, Opera */
@-webkit-keyframes all {
      2%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.3) 99%, rgba(255,255,255,0) 100%);
   }
   5%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.5) 99%, rgba(255,255,255,0) 100%);
   }

    7%   {
      background: linear-gradient(-45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   14%   {
      background: linear-gradient(-23deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   22%   {
      background: linear-gradient(0deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   28%   {
      background: linear-gradient(23deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   35%   {
      background: linear-gradient(45deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   43%   {
      background: linear-gradient(90deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   50%   {
      background: linear-gradient(120deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   57%   {
      background: linear-gradient(145deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   64%   {
      background: linear-gradient(180deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   71%   {
      background: linear-gradient(200deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   78%   {
      background: linear-gradient(220deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   85%   {
      background: linear-gradient(300deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   92%   {
      background: linear-gradient(320deg, rgba(255,255,255,0) 72%, rgba(255,255,255,1) 99%, rgba(255,255,255,0) 100%);
   }

   94%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.9) 99%, rgba(255,255,255,0) 100%);
   }

   97%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.5) 99%, rgba(255,255,255,0) 100%);
   }

   100%   {
      background: linear-gradient(345deg, rgba(255,255,255,0) 72%, rgba(255,255,255,0.3) 99%, rgba(255,255,255,0) 100%);
   }
}

.btn:hover {
  transition: 1s ease-in-out;
  background: #4F95DA;
}

.btn:hover svg {
  stroke-dashoffset: -480;
}

.bg-line{
  width:100px;
  height:100px;
}

.btn span {
  color: white;
  font-size: 18px;
  font-weight: 100;
}
<div class="container">
        
    
      <button class="btn">
            
<!-- edit -->
                    
        <span>Junior is AWESOME</span>
      </button>
    
        
  </div>


推荐阅读