使用 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