CSS3 图片悬停效果

下面是鼠标悬停在图片上的几种效果:文本淡入,淡入框,上下左右滑入效果。

淡入文本:

Avatar
Hello World

淡入框:

Avatar
John

顶部划入:

Avatar
Hello World

淡入文本 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. display: block;
  11. width: 100%;
  12. height: auto;
  13. }
  14. .overlay {
  15. position: absolute;
  16. top: 0;
  17. bottom: 0;
  18. left: 0;
  19. right: 0;
  20. height: 100%;
  21. width: 100%;
  22. opacity: 0;
  23. transition: .5s ease;
  24. background-color: #008CBA;
  25. }
  26. .container:hover .overlay {
  27. opacity: 1;
  28. }
  29. .text {
  30. color: white;
  31. font-size: 20px;
  32. position: absolute;
  33. top: 50%;
  34. left: 50%;
  35. transform: translate(-50%, -50%);
  36. -ms-transform: translate(-50%, -50%);
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <h2>淡入文本</h2>
  42. <div class="container">
  43. <img src="/images/img_avatar.png" alt="Avatar" class="image">
  44. <div class="overlay">
  45. <div class="text">Hello World</div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

淡入框 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. opacity: 1;
  11. display: block;
  12. width: 100%;
  13. height: auto;
  14. transition: .5s ease;
  15. backface-visibility: hidden;
  16. }
  17. .middle {
  18. transition: .5s ease;
  19. opacity: 0;
  20. position: absolute;
  21. top: 50%;
  22. left: 50%;
  23. transform: translate(-50%, -50%);
  24. -ms-transform: translate(-50%, -50%)
  25. }
  26. .container:hover .image {
  27. opacity: 0.3;
  28. }
  29. .container:hover .middle {
  30. opacity: 1;
  31. }
  32. .text {
  33. background-color: #4CAF50;
  34. color: white;
  35. font-size: 16px;
  36. padding: 16px 32px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <h2>淡入框</h2>
  42. <div class="container">
  43. <img src="/images/img_avatar.png" alt="Avatar" class="image" style="width:100%">
  44. <div class="middle">
  45. <div class="text">John Doe</div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

顶部滑入 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. display: block;
  11. width: 100%;
  12. height: auto;
  13. }
  14. .overlay {
  15. position: absolute;
  16. bottom: 100%;
  17. left: 0;
  18. right: 0;
  19. background-color: #008CBA;
  20. overflow: hidden;
  21. width: 100%;
  22. height: 0;
  23. transition: .5s ease;
  24. }
  25. .container:hover .overlay {
  26. bottom: 0;
  27. height: 100%;
  28. }
  29. .text {
  30. white-space: nowrap;
  31. color: white;
  32. font-size: 20px;
  33. position: absolute;
  34. overflow: hidden;
  35. top: 50%;
  36. left: 50%;
  37. transform: translate(-50%, -50%);
  38. -ms-transform: translate(-50%, -50%);
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>顶部滑入</h2>
  44. <div class="container">
  45. <img src="/images/img_avatar.png" alt="Avatar" class="image">
  46. <div class="overlay">
  47. <div class="text">Hello World</div>
  48. </div>
  49. </div>
  50. </body>
  51. </html>

底部滑入:

Avatar
Hello World

左边滑入:

Avatar
Hello World

右边滑入:

Avatar
Hello World

底部滑入 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. display: block;
  11. width: 100%;
  12. height: auto;
  13. }
  14. .overlay {
  15. position: absolute;
  16. bottom: 0;
  17. left: 0;
  18. right: 0;
  19. background-color: #008CBA;
  20. overflow: hidden;
  21. width: 100%;
  22. height: 0;
  23. transition: .5s ease;
  24. }
  25. .container:hover .overlay {
  26. height: 100%;
  27. }
  28. .text {
  29. white-space: nowrap;
  30. color: white;
  31. font-size: 20px;
  32. position: absolute;
  33. overflow: hidden;
  34. top: 50%;
  35. left: 50%;
  36. transform: translate(-50%, -50%);
  37. -ms-transform: translate(-50%, -50%);
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <h2>悬停时从底部滑入</h2>
  43. <div class="container">
  44. <img src="/images/img_avatar.png" alt="Avatar" class="image">
  45. <div class="overlay">
  46. <div class="text">Hello World</div>
  47. </div>
  48. </div>
  49. </body>
  50. </html>

左边滑入 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. display: block;
  11. width: 100%;
  12. height: auto;
  13. }
  14. .overlay {
  15. position: absolute;
  16. bottom: 0;
  17. left: 0;
  18. right: 0;
  19. background-color: #008CBA;
  20. overflow: hidden;
  21. width: 0;
  22. height: 100%;
  23. transition: .5s ease;
  24. }
  25. .container:hover .overlay {
  26. width: 100%;
  27. }
  28. .text {
  29. white-space: nowrap;
  30. color: white;
  31. font-size: 20px;
  32. position: absolute;
  33. overflow: hidden;
  34. top: 50%;
  35. left: 50%;
  36. transform: translate(-50%, -50%);
  37. -ms-transform: translate(-50%, -50%);
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <h2>悬停时从左边滑入</h2>
  43. <div class="container">
  44. <img src="/images/img_avatar.png" alt="Avatar" class="image">
  45. <div class="overlay">
  46. <div class="text">Hello World</div>
  47. </div>
  48. </div>
  49. </body>
  50. </html>

右边滑入 代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 50%;
  8. }
  9. .image {
  10. display: block;
  11. width: 100%;
  12. height: auto;
  13. }
  14. .overlay {
  15. position: absolute;
  16. bottom: 0;
  17. left: 100%;
  18. right: 0;
  19. background-color: #008CBA;
  20. overflow: hidden;
  21. width: 0;
  22. height: 100%;
  23. transition: .5s ease;
  24. }
  25. .container:hover .overlay {
  26. width: 100%;
  27. left: 0;
  28. }
  29. .text {
  30. white-space: nowrap;
  31. color: white;
  32. font-size: 20px;
  33. position: absolute;
  34. overflow: hidden;
  35. top: 50%;
  36. left: 50%;
  37. transform: translate(-50%, -50%);
  38. -ms-transform: translate(-50%, -50%);
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>悬停时从右边滑入</h2>
  44. <div class="container">
  45. <img src="/images/img_avatar.png" alt="Avatar" class="image">
  46. <div class="overlay">
  47. <div class="text">Hello World</div>
  48. </div>
  49. </div>
  50. </body>
  51. </html>

分类导航