CSS 按钮动画效果

用 CSS 给按钮添加动画效果。

动画按钮

实例 1

在鼠标悬停时添加箭头:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. display: inline-block;
  7. border-radius: 4px;
  8. background-color: #f4511e;
  9. border: none;
  10. color: #FFFFFF;
  11. text-align: center;
  12. font-size: 28px;
  13. padding: 20px;
  14. width: 200px;
  15. transition: all 0.5s;
  16. cursor: pointer;
  17. margin: 5px;
  18. }
  19. .button span {
  20. cursor: pointer;
  21. display: inline-block;
  22. position: relative;
  23. transition: 0.5s;
  24. }
  25. .button span:after {
  26. content: '\00bb';
  27. position: absolute;
  28. opacity: 0;
  29. top: 0;
  30. right: -20px;
  31. transition: 0.5s;
  32. }
  33. .button:hover span {
  34. padding-right: 25px;
  35. }
  36. .button:hover span:after {
  37. opacity: 1;
  38. right: 0;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>按钮的动画效果</h2>
  44. <button class="button" style="vertical-align:middle"><span>按钮 </span></button>
  45. </body>
  46. </html>

实例 2

添加点击时的“按键按下”效果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. display: inline-block;
  7. padding: 15px 25px;
  8. font-size: 24px;
  9. cursor: pointer;
  10. text-align: center;
  11. text-decoration: none;
  12. outline: none;
  13. color: #fff;
  14. background-color: #4CAF50;
  15. border: none;
  16. border-radius: 15px;
  17. box-shadow: 0 9px #999;
  18. }
  19. .button:hover {background-color: #3e8e41}
  20. .button:active {
  21. background-color: #3e8e41;
  22. box-shadow: 0 5px #666;
  23. transform: translateY(4px);
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <h2>按钮动画效果 - "按下效果"</h2>
  29. <button class="button">点击</button>
  30. </body>
  31. </html>

实例 3

鼠标悬停时淡入:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <style>
  6. .button {
  7. background-color: #f4511e;
  8. border: none;
  9. color: white;
  10. padding: 16px 32px;
  11. text-align: center;
  12. font-size: 16px;
  13. margin: 4px 2px;
  14. opacity: 0.6;
  15. transition: 0.3s;
  16. display: inline-block;
  17. text-decoration: none;
  18. cursor: pointer;
  19. }
  20. .button:hover {opacity: 1}
  21. </style>
  22. </head>
  23. <body>
  24. <h2>按钮动画效果 - "淡入效果"</h2>
  25. <button class="button">悬停</button>
  26. </body>
  27. </html>

实例 4

添加点击时的“涟漪”效果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. position: relative;
  7. background-color: #4CAF50;
  8. border: none;
  9. font-size: 28px;
  10. color: #FFFFFF;
  11. padding: 20px;
  12. width: 200px;
  13. text-align: center;
  14. transition-duration: 0.4s;
  15. text-decoration: none;
  16. overflow: hidden;
  17. cursor: pointer;
  18. }
  19. .button:after {
  20. content: "";
  21. background: #f1f1f1;
  22. display: block;
  23. position: absolute;
  24. padding-top: 300%;
  25. padding-left: 350%;
  26. margin-left: -20px !important;
  27. margin-top: -120%;
  28. opacity: 0;
  29. transition: all 0.8s
  30. }
  31. .button:active:after {
  32. padding: 0;
  33. margin: 0;
  34. opacity: 1;
  35. transition: 0s
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <h2>按钮动画效果 - 涟漪效果</h2>
  41. <button class="button">点击</button>
  42. </body>
  43. </html>

分类导航