本文介绍如何利用JavaScript结合CSS样式,实现单击按钮后文字颜色逐渐褪去的视觉效果。通过控制透明度或颜色值的变化,创建简洁的前端交互动画,适用于网页按钮点击反馈或文字淡出效果。
实现颜色褪去效果的核心代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <style type="text/css"> p{ font-size: 12px; font-family: Arial; } input{ cursor: pointer; } </style> </head> <body> <p>"黄褪"效果</p> <input type="button" value="Click!" onclick="flash(60)" /> </body> </html> 注意:若代码中引入了外部JavaScript文件,必须在页面完全加载并刷新后,该脚本才能正常执行。