如何为网站按钮添加Material Design涟漪动效
引入 Material Design 库
首先,确保在项目中引入了 Material Design 的 CSS 和 JavaScript 库。
<!-- Material Design Lite CSS -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<!-- Material Design Lite JavaScript -->
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>、创建按钮
使用 Material Design 的类来创建按钮。
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 按钮实例
</button>添加 Ripple 效果
mdl-js-ripple-effect 类会自动为按钮添加点击时的涟漪效果。
自定义动效
如果需要更复杂的动效,可以使用 CSS 或 JavaScript 自定义。
使用 CSS 自定义动效
例如,添加一个缩放效果:
.mdl-button {
transition: transform 0.3s ease;
}
.mdl-button:active {
transform: scale(0.95);
}使用 JavaScript 自定义动效
例如,点击时改变背景颜色:
document.querySelector('.mdl-button').addEventListener('click', function() {
this.style.backgroundColor = '#ff4081';
});使用 Material Components Web
如果需要更丰富的组件和动效,可以使用 Material Components Web。
<!-- Material Components Web CSS -->
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<!-- Material Components Web JavaScript -->
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>然后使用 mdc-button 类:
<button class="mdc-button mdc-button--raised">
<span class="mdc-button__label">按钮实例</span>
</button>使用动画库
如果需要更复杂的动效,可以结合动画库如 Animate.css 或 GSAP。
<!-- Animate.css -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">然后为按钮添加动画类:
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect animate__animated animate__bounce">
点击我
</button>
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Bleaz
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果

