【问题标题】:Code for CSS only button like this? A different way around this style?像这样的纯 CSS 按钮的代码?这种风格的不同方式?
【发布时间】:2017-12-24 02:17:22
【问题描述】:

此按钮是否有更好的版本或延迟较小的版本?

我试图找到这种确切类型的样式按钮,但我找不到。这是一个非常糟糕的尝试,需要大量重构,因为我从一开始就没有考虑正确的样式。

任何链接或更高效的代码将不胜感激

我见过类似的代码,但是它只是这种精确的线条样式,基本上是在我想要的按钮周围绘制和删除的(这只是一个漫谈,因为“要求”有更多的描述而不是代码)

.UnderlineButton { 
	border: none;
	background-color: white;
	position: relative;
	border: 2px solid;
	border-top-color: rgb(255,255,255);
	border-right-color: rgb(255,255,255);
	border-left-color: rgb(255,255,255);
	border-bottom-color: rgb(255,255,255);
    padding: 10px;
	padding-top: 20px;
    padding-bottom: 0;
	cursor: pointer;
	
	-webkit-animation: UnderlineButton .5s forwards;
}

	@-webkit-keyframes UnderlineButton {
		0%   { 
			padding-top: 10px;
			padding-bottom: 10px;
			border-right-color: red;
			border-bottom-color: rgb(255,255,255);
			border-top-color: red;
			border-left-color: red;
		}
		19% {
			border-right-color: red;
		}
		20% {
			border-right-color:  rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
			border-top-color: red;
			border-left-color: red;
		}
		39% {
			border-top-color: red;
		}
		40% {
			border-right-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
			border-top-color:  rgb(255,255,255);
			border-left-color: red;
		}
		59% {
			border-left-color: red;
		}
		60 {
			border-right-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
			border-top-color: rgb(255,255,255);
			border-left-color:  rgb(255,255,255);
		}
		100% { 
			padding-top: 20px;
			padding-bottom: 0;
			border-right-color: rgb(255,255,255);
			border-top-color: rgb(255,255,255);
			border-left-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
		}
	}
	
	.UnderlineButton:hover {
		-webkit-animation: UnderlineButtonHover .5s forwards;
	}
	
	@-webkit-keyframes UnderlineButtonHover {
		0%   {
			padding-top: 20px;
			padding-bottom: 0;
			border-right-color: rgb(255,255,255);
			border-top-color: rgb(255,255,255);
			border-left-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
		}
		20% {
			padding-top: 20px;
			padding-bottom: 0;
			border-right-color: rgb(255,255,255);
			border-top-color: rgb(255,255,255);
			border-left-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
		}
		39% {
			border-right-color: rgb(255,255,255);
		}
		40% {
			border-right-color: red;
			border-left-color: rgb(255,255,255);
			border-top-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
		}
		59% {
			border-top-color: rgb(255,255,255);
		}
		60% {
			border-right-color: red;
			border-top-color: red;
			border-left-color: rgb(255,255,255);
			border-bottom-color: rgb(255,255,255);
		}
		79% {
			border-left-color: rgb(255,255,255);
		}
		80% {
			border-right-color: red;
			border-top-color: red;
			border-left-color: red;
			border-bottom-color: rgb(255,255,255);
		}
		99% {
			border-bottom-color: red;
		}
		100% {
			padding-top: 10px;
			padding-bottom: 10px;
			border-right-color: red;
			border-bottom-color: red;
			border-top-color: red;
			border-left-color: red;
		}
	}


	.UnderlineButton:before {
		position: absolute;
		bottom: -2px;
		background-color: red;
		width: 80%;
		height: 2px;
		content: "";
		right: 10%;
		-webkit-animation: UnderlineButtonBefore .5s forwards;
	}
	
		@-webkit-keyframes UnderlineButtonBefore {
			0%   { 
				top: 100%;
				width: 100%;
				left: 0;
				right: 0;
				height: 2px;
			}
			20% {
				left: 100%;
				right: 0;
				height: 2px;
				width: 2px;
				top: 100%;
			}
			21% {
				top: 0;
				width: 2px;
				height: 100%;
				right: 0;
				left: 100%;
			}
			40% {
				top: 0;
				width: 2px;
				height: 2px;
				right: 0;
				left: 100%;
			}
			41% {
				top: -2px;
				width: 100%;
				height: 2px;
				right: 0;
				left: 0;
			}
			60% {
				top: -2px;
				width: 2px;
				height: 2px;
				left: 0;
			}
			61% {
				top: 0;
				width: 2px;
				height: 100%;
				left: -2px;
				bottom: 0;
			}
			80% {
				top: 100%;
				bottom: 0;
				width: 2px;
				height: 0;
				left: -2px;
				bottom: 0;
			}
			90% {
				bottom: -2px;
				left: 0;
				width: 0;
				height: 2px;
			}
			100% { 
				right: 10%;
				left: 10%;
				width: 80%;
				bottom: -2px;
				height: 2px;
			}
		}
	
	.UnderlineButton:hover:before {

		-webkit-animation: UnderlineButtonBeforeHover .5s forwards;
	}
	
		@-webkit-keyframes UnderlineButtonBeforeHover {
			0%   { 
				right: 10%;
				width: 80%;
				left: 10%;
			}
			20% {
				right: 0;
				width: 2px;
				height: 2px;
				bottom: -2px;
				left: 100%;
			}
			21% {
				right: 0;
				width: 2px;
				height: 2px;
				top: 100%;
				
			}
			40% {
				width: 2px;
				height: 100%;
				top: 0;
				right: -2px;
			}
			41% {
				top: -2px;
				height: 2px;
				width: 2px;
				left: 100%;
			}
			60% {
				width: 100%;
				height: 2px;
				left: 0;
				top: -2px;
			}
			61% {
				width: 2px;
				height: 2px;
				top: -2px;
				left: -2px;
			}
			80% {
				height: 100%;
				width: 2px;
				top: -2px;
				left: -2px;
			}
			81% {
				height: 2px;
				width: 2px;
				bottom: -2px;
				left: -2px;
				top: 100%;
			}
			100% { 
				top: 100%;
				width: 100%;
				left: 0;
				height: 2px;
			}
		}
<div style="padding: 40px; background-color:rgb(252,252,252);">
  <button class="UnderlineButton">Underline</button>
</div>
  

【问题讨论】:

  • 您的问题到底是什么?什么不符合您的预期?
  • 它看起来很慢,我想要一个更好的这个按钮版本。很抱歉造成混乱
  • FWIW,没有看到滞后。我觉得很好。

标签: css user-interface animation transition


【解决方案1】:

我认为您正在寻找这种东西。我过去也有类似的要求。请给我一些时间来修改您的要求。同时,请随意看看你是否可以自己做。我相信您正在寻找过渡?

button {
  background: none;
  border: 0;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  margin: 1em;
  padding: 1em 2em;
  -webkit-box-shadow: inset 0 0 0 2px #f45e61;
          box-shadow: inset 0 0 0 2px #f45e61;
  color: #f45e61;
  font-size: inherit;
  font-weight: 700;
  position: relative;
  vertical-align: middle;
}
button::before, button::after {
  -webkit-box-sizing: inherit;
          box-sizing: inherit;
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
}

.draw {
  -webkit-transition: color 0.25s;
  transition: color 0.25s;
}
.draw::before, .draw::after {
  border: 2px solid transparent;
  width: 0;
  height: 0;
}
.draw::before {
  top: 0;
  left: 0;
}
.draw::after {
  bottom: 0;
  right: 0;
}
.draw:hover {
  color: #60daaa;
}
.draw:hover::before, .draw:hover::after {
  width: 100%;
  height: 100%;
}
.draw:hover::before {
  border-top-color: #60daaa;
  border-right-color: #60daaa;
  -webkit-transition: width 0.25s ease-out, height 0.25s ease-out 0.25s;
  transition: width 0.25s ease-out, height 0.25s ease-out 0.25s;
}
.draw:hover::after {
  border-bottom-color: #60daaa;
  border-left-color: #60daaa;
  -webkit-transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s;
  transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s;
}
<html>
<head>


</head>
<body>
<div id="wrapper">

<section class="buttons">
 <button class="draw">Draw</button>
 <section>
</div>


</body>
</html>

【讨论】:

  • 这是我找到的例子之一!但是,不幸的是,我希望它在返回时也能制作动画。我还发现过渡的一个问题是,如果您在动画期间将鼠标移开,您会得到一个故障结果,而在动画前进时您不会遇到这个问题。
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
相关资源
最近更新 更多