css – 有没有办法将鼠标悬停在一个元素上并影响不同的元素?

前端之家收集整理的这篇文章主要介绍了css – 有没有办法将鼠标悬停在一个元素上并影响不同的元素?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > How to affect other elements when a div is hovered5个答案我想要这样简单,但我知道它不是:
  1. img {
  2. opacity: 0.4;
  3. filter: alpha(opacity=40);
  4. }
  5.  
  6. img:hover {
  7. #thisElement {
  8. opacity: 0.3;
  9. filter: alpha(opacity=30);
  10. }
  11. opacity:1;
  12. filter:alpha(opacity=100);
  13. }

因此,当您将鼠标悬停在img上时,它会将#thisElement的不透明度更改为30%,并将图像的不透明度更改为100%。有没有办法实际这样做只使用css?

因此这是HTML

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript" src="C:\Users\Shikamaru\Documents\Contwined Coding\LearningToCode\Learning jQuery\js\jquery-1.6.2.min.js"></script>
  5. <script type="text/javascript" src="briefcase.js"></script>
  6. <link rel="stylesheet" type="text/css" href="taskbar.css"/>
  7. <link rel="stylesheet" type="text/css" href="briefcase.css" />
  8. <title>Briefcase</title>
  9. <Meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. </head>
  11.  
  12. <body>
  13.  
  14. <div class="mask"></div>
  15. <div class="float">
  16. <div id="album1">Album Title</div>
  17. <img class="left" src="bradBeachHeart.JPG" alt="Brad at the Lake" />
  18.  
  19. <img class="left" src="mariaNavi.jpg" alt="Making Maria Na'vi" />
  20.  
  21. <img class="left" src="mattWaterRun.jpg" alt="Photoshopped Matt" />
  22. </div>
  23.  
  24. <div class="gradientTop"></div>
  25. <div class="gradientBottom"></div>
  26.  
  27.  
  28. </body>
  29. </html>

这是CSS:

  1. body {
  2. font: normal small/3em helvetica,sans-serif;
  3. text-align: left;
  4. letter-spacing: 2px;
  5. font-size: 16px;
  6. margin: 0;
  7. padding: 0;
  8. }
  9.  
  10. div.gradientTop {
  11. position: absolute;
  12. margin-top: 5px;
  13. z-index: 2;
  14. width: 206px;
  15. height: 30px;
  16. float: left;
  17. background: -webkit-linear-gradient(rgba(255,255,2),rgba(255,0))
  18. }
  19.  
  20. div.gradientBottom {
  21. position: absolute;
  22. margin-bottom: 5px;
  23. z-index: 2;
  24. width: 206px;
  25. height: 120px;
  26. float: left;
  27. bottom: -210px;
  28. background: -webkit-linear-gradient(rgba(255,0),1))
  29. }
  30.  
  31. div.float {
  32. border-right: 1px solid orange;
  33. position: absolute;
  34. z-index: 2;
  35. margin-left: 5px;
  36. margin-top: 5px;
  37. float: left;
  38. width: 200px;
  39. }
  40.  
  41. div.mask {
  42. position: relative;
  43. z-index: 1;
  44. margin-top: 5px;
  45. float: left;
  46. width: 206px;
  47. height: 805px;
  48. background-color: white;
  49. }
  50.  
  51. img.left {
  52. z-index: inherit;
  53. margin-bottom: 3px;
  54. float: left;
  55. width: 200px;
  56. min-height: 200px;
  57. /* for modern browsers */
  58. height: auto !important;
  59. /* for modern browsers */
  60. height: 200px;
  61. /* for IE5.x and IE6 */
  62. opacity: 0.4;
  63. filter: alpha(opacity=40)
  64. }
  65.  
  66. img.left:hover + #album1 {
  67. opacity: .4;
  68. }
  69.  
  70. img.left:hover {
  71. opacity: 1.0;
  72. }
  73.  
  74. #album1 {
  75. z-index: 2;
  76. width: 200px;
  77. color: white;
  78. text-align: center;
  79. position: absolute;
  80. background: orange;
  81. top: 70px;
  82. }

解决方法

使用CSS的唯一方法是如果要影响的元素是后代或相邻的兄弟。

在后代的情况下:

  1. #parent_element:hover #child_element,/* or */
  2. #parent_element:hover > #child_element {
  3. opacity: 0.3;
  4. }

这将适用于以下元素:

  1. <div id="parent_element">
  2. <div id="child_element">Content</div>
  3. </div>

对于相邻兄弟姐妹:

  1. #first_sibling:hover + #second_sibling {
  2. opacity: 0.3;
  3. }

它适用于标记,如:

  1. <div id="first_sibling">Some content in the first sibling</div> <div id="second_sibling">and now in the second</div>

在这两种情况下,选择器中的后一个元素是所选择的元素。

给定你的伪代码示例,你可能想要像:

  1. img:hover + img {
  2. opacity: 0.3;
  3. color: red;
  4. }

JS Fiddle demo

猜你在找的CSS相关文章