html+css 实现可点击的光影按钮

效果

点击效果

原理解析

1.光影变化:
使用了css3的keyframes定义了一个shine动画并调用。
在这里插入图片描述

2.可点击:
点击是用了 button 按钮的 active属性,把按钮做一个position: relative; top: 0;的设置,当点击的时候 top: 2px;实现的。
按钮
在这里插入图片描述

上代码,可以直接复制使用

目录

效果目录

html

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>css button</title>

<link rel="stylesheet" href="style.css">

</head>
<body>
<h1>纯 html+css 实现</h1>
<div class="buttonWrap">
    <button class="shiny">CSS button!</button>
    <button class="shinydarken">CSS光影按钮</button>
</div>

</body>
</html>

css

body {
    background-color: #313131;
   
    height: 100vh;
    font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
}


h1{
text-align: center;
color:#f5f5f5;
}

.buttonWrap{
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  align-items: center;
}
/*核心代码*/
/* 按钮的基础样式 */
button {
  display: block;
  position: relative;
  top: 0;
  border: 0;
  cursor: pointer;
  background: #9ccc65;
  color: white;
  border-radius: 3px;
  line-height: 1;
  font-size: 20px;
  padding: 15px 25px;
  outline: 0;
  margin: 20px;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}
/* 第一个按钮光影移动 */
button.shiny {
    background: #007eaf;
    background: linear-gradient(-45deg, #2284fb 50%, #0a0e92 60%, #2e59d4 70%);
    background-size: 600% 100%;
    /* 这里调用了shine */
    -webkit-animation: shine 20s infinite;
    animation: shine 20s infinite;
    -webkit-animation-delay: 0s;
    animation-delay: 0s;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
}
/* 第二个按钮光影移动 */
button.shinydarken {
  background: #9ccc65;
  background: linear-gradient(-45deg, #9ccc65 50%, #699833 60%, #9ccc65 70%);
  background-size: 600% 100%;
/* 这里调用了shine */
  -webkit-animation: shine 20s infinite;
          animation: shine 20s infinite;
  -webkit-animation-delay: 0s;
          animation-delay: 0s;
  -webkit-animation-timing-function: linear;
          animation-timing-function: linear;
}
/* 点击时的效果 */
button:active {
  top: 2px;
}
/* 定义光影效果-兼容写法 */
@-webkit-keyframes shine {
  0% {
    background-position-x: 400%;
  }
  50% {
    background-position-x: 0%;
  }
  100% {
    background-position-x: -400%;
  }
}
/* 定义光影效果 */
@keyframes shine {
  0% {
    background-position-x: 400%;
  }
  50% {
    background-position-x: 0%;
  }
  100% {
    background-position-x: -400%;
  }
}


支持一下

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值