使用CSS添加按钮点击按下效果
为了允许用户与网站或应用程序交互,按钮是网页设计中必不可少的组件。通过使用CSS为按钮添加视觉上美观且创新的点击效果,可以极大地改善用户体验并使交互更有趣。
可以使用CSS的`active`伪类在按钮点击时创建按下效果。此样式通过定义按钮被点击时的外观来提供视觉反馈。在了解更多信息之前,让我们快速了解一下HTML中的按钮。
HTML按钮
在HTML中,可点击的按钮由<button>标签定义。我们可以在<button>标签内使用文本和图像。不同浏览器的默认<button>类型不同,用于提交内容。我们可以使用CSS工具来设置按钮的样式。
语法
以下是HTML按钮的语法
<button>....</button>
示例
让我们来看下面的例子,我们将创建一个按钮并应用CSS。
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #34495E;
text-align: center;
}
button {
margin: 150px;
position: relative;
overflow: hidden;
padding: 10px 40px;
font-size: 30px;
font-family: verdana;
background-color: transparent;
border: none;
color: #DE3163;
transition: 0.3s;
&::before {
content: '';
height: 3px;
width: 0;
left: 0;
bottom: 0;
position: absolute;
background: #E8DAEF;
transition: 0.4s;
}
&:hover {
&::before {
width: 100%;
}
}
}
span.y {
background-color: #D5F5E3;
border-radius: 100%;
position: absolute;
transform: scale(0);
animation: y .4s linear forwards;
}
@keyframes y {
to {
transform: scale(1);
opacity: 0;
}
}
</style>
</head>
<body>
<div>
<button class="tp">CLICK ME</button>
</div>
<script>
var button = document.querySelectorAll('.tp');
Array.prototype.forEach.call(button, function(a) {
a.addEventListener('click', ripple)
});
function ripple(event) {
var x = document.createElement('span');
x.classList.add('y');
var max = Math.max(this.offsetWidth, this.offsetHeight);
x.style.width = x.style.height = max * 2 + 'px';
var rect = this.getBoundingClientRect();
x.style.left = event.clientX - rect.left - max + 'px';
x.style.top = event.clientY - rect.top - max + 'px';
this.appendChild(x);
}
</script>
</body>
</html>
运行以上代码后,它将在网页上生成一个包含点击按钮的输出。当用户尝试点击按钮时,它会显示由CSS制作的效果。
示例
再来看另一个例子,我们将鼠标悬停在按钮上时添加星号。
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #D5F5E3;
text-align: center;
}
.tp {
border-radius: 10px;
background-color: #D2B4DE;
border: none;
color: #DE3163;
font-size: 30px;
font-family: verdana;
padding: 10px;
width: 150px;
cursor: pointer;
margin: 50px;
}
.tp span {
position: relative;
transition: 0.3s;
}
.tp span:after {
content: '\2042';
position: absolute;
opacity: 0;
top: -3px;
right: -25px;
transition: 0.5s;
}
.tp:hover span {
padding-right: 30px;
}
.tp:hover span:after {
opacity: 2;
right: 1px;
}
</style>
</head>
<body>
<button class="tp">
<span>CLICK</span>
</button>
</body>
</html>
运行以上代码后,输出窗口将弹出,在网页上显示应用了CSS的按钮。当用户尝试将鼠标悬停在星号上时。
示例
在下面的例子中,我们将使用CSS创建一个简单的按钮按下效果。
<!DOCTYPE html>
<html>
<head>
<style>
.button {
background-color: orange;
color: white;
border: none;
border-radius: 7px;
box-shadow: 0 5px #999;
display: inline-block;
padding: 10px;
font-size: 16px;
cursor: pointer;
text-align: center;
outline: none;
}
.button:hover {
background-color: blue;
}
.button:active {
background-color: orange;
box-shadow: 0 5px gray;
transform: translateY(2px);
}
</style>
</head>
<body>
<button class="button">Result</button>
</body>
</html>
运行以上代码后,它将在网页上生成一个包含应用了CSS的按下效果的输出。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP