HTML+CSS+JS实现按钮悬停效果

HTML+CSS+JS实现按钮悬停效果

效果图

按钮悬停效果

原地址

https://codepen.io/aaroniker/pen/bGGVMbY

打包下载(无需积分,立即下载)

https://download.csdn.net/download/qq_45136860/85475907

HTML

<a href="" class="button">Button</a>
<a href="" class="button reverse dark">Reverse</a>
<a href="" class="button fast white">Fast</a>

CSS

.button.dark {
  --background: #2F3545;
  --shadow: 0 2px 8px -1px rgba(21, 25, 36, 0.32);
  --shadow-hover: 0 4px 20px -2px rgba(21, 25, 36, 0.5);
}

.button.white {
  --background: #fff;
  --text: #275efe;
  --shadow: 0 2px 8px -1px rgba(18, 22, 33, 0.04);
  --shadow-hover: 0 4px 20px -2px rgba(18, 22, 33, 0.12);
}

.button.fast {
  --duration: .32s;
}

.button {
  --background: #275efe;
  --text: #fff;
  --font-size: 16px;
  --duration: .44s;
  --move-hover: -4px;
  --shadow: 0 2px 8px -1px rgba(39, 94, 254, 0.32);
  --shadow-hover: 0 4px 20px -2px rgba(39, 94, 254, 0.5);
  --font-shadow: var(--font-size);
  padding: 16px 32px;
  font-family: 'Roboto';
  font-weight: 500;
  line-height: var(--font-size);
  border-radius: 24px;
  display: block;
  outline: none;
  text-decoration: none;
  font-size: var(--font-size);
  letter-spacing: .5px;
  background: var(--background);
  color: var(--text);
  box-shadow: var(--shadow);
  transform: translateY(var(--y)) translateZ(0);
  transition: transform var(--duration) ease, box-shadow var(--duration) ease;
}

.button div {
  display: flex;
  overflow: hidden;
  text-shadow: 0 var(--font-shadow) 0 var(--text);
}

.button div span {
  display: block;
  backface-visibility: hidden;
  font-style: normal;
  transition: transform var(--duration) ease;
  transform: translateY(var(--m)) translateZ(0);
}

.button div span:nth-child(1) {
  transition-delay: 0.05s;
}

.button div span:nth-child(2) {
  transition-delay: 0.1s;
}

.button div span:nth-child(3) {
  transition-delay: 0.15s;
}

.button div span:nth-child(4) {
  transition-delay: 0.2s;
}

.button div span:nth-child(5) {
  transition-delay: 0.25s;
}

.button div span:nth-child(6) {
  transition-delay: 0.3s;
}

.button div span:nth-child(7) {
  transition-delay: 0.35s;
}

.button div span:nth-child(8) {
  transition-delay: 0.4s;
}

.button div span:nth-child(9) {
  transition-delay: 0.45s;
}

.button div span:nth-child(10) {
  transition-delay: 0.5s;
}

.button div span:nth-child(11) {
  transition-delay: 0.55s;
}

.button:hover {
  --y: var(--move-hover);
  --shadow: var(--shadow-hover);
}

.button:hover span {
  --m: calc(var(--font-size) * -1);
}

.button.reverse {
  --font-shadow: calc(var(--font-size) * -1);
}

.button.reverse:hover span {
  --m: calc(var(--font-size));
}

html {
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
}

* {
  box-sizing: inherit;
}

*:before,
*:after {
  box-sizing: inherit;
}

body {
  min-height: 100vh;
  display: flex;
  font-family: 'Roboto', Arial;
  justify-content: center;
  align-items: center;
  background: #E4ECFA;
}

body .button {
  margin: 0 12px;
}

body .dribbble {
  position: fixed;
  display: block;
  right: 20px;
  bottom: 20px;
}

body .dribbble img {
  display: block;
  height: 28px;
}

JS

document.querySelectorAll('.button').forEach(button => button.innerHTML = '<div><span>' + button.textContent.trim().split('').join('</span><span>') + '</span></div>');
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值