【问题标题】:Hover effect over div with image and text带有图像和文本的 div 上的悬停效果
【发布时间】:2016-11-21 00:57:19
【问题描述】:

我提供一个 JSFiddle:https://jsfiddle.net/om83Ljtm/

它包含divimgspan 元素。 img 充当div 的背景图像。我不使用 CSS background-property 作为背景图像,因为我想在悬停 div 时更改不透明度。 span 包含与背景图像重叠的文本。悬停div 时,我想更改图像的不透明度。但是,当鼠标悬停在文本 (span) 上时,此方法有效,img 的不透明度变回初始值 0.6。但是当我将鼠标悬停在文本上时,我希望图像不会(!)改变其不透明度。如何实现?

总结一下:在 JSFiddle 中,如果我将鼠标悬停在 div 上,不透明度会变为 1。即使我将鼠标悬停在 span 中的文本上,它也应该保持不变。这还不行。

【问题讨论】:

    标签: html css image background opacity


    【解决方案1】:

    使用.event:hover img 而不是.event img:hover

    .event {
      width: 200px;
      position: relative;
      border: 1px solid #dddddd;
      height: auto;
    }
    
    .event .titel {
      float:left;
      font-size: 20px;
      background-color:white;
      padding:3px;
      position:absolute;
      top:50px;
      left:5px;
    }
    
    .event img {
      opacity: 0.6;
      width: 100%;
    }
    
    .event img {
      -webkit-transition: opacity 1s ease-in-out;
      -moz-transition: opacity 1s ease-in-out;
      -o-transition: opacity 1s ease-in-out;
      transition: opacity 1s ease-in-out;
    }
    
    .event:hover img {
      opacity: 1;
    }
    <div class="event">
       <img src="https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300">
    		<span class="titel">text text text text text text text text text </span>
    </div>

    【讨论】:

      【解决方案2】:

      您可以通过将鼠标悬停在 div 而不是 img 上来实现这一点:https://jsfiddle.net/om83Ljtm/2/

      .event:hover img {
         opacity: 1;
      }
      

      【讨论】:

        【解决方案3】:

        就像这样;)

        .event {
          width: 200px;
          position: relative;
          border: 1px solid #dddddd;
          height: auto;
        }
        
        .event .titel {
            float:left;
            font-size: 20px;
            background-color:white;
            padding:3px;
            position:absolute;
            top:50px;
            left:5px;
        }
        
        .event img {
            opacity: 0.6;
            width: 100%;
        }
        
        .event img {
           -webkit-transition: opacity 1s ease-in-out;
          -moz-transition: opacity 1s ease-in-out;
          -o-transition: opacity 1s ease-in-out;
          transition: opacity 1s ease-in-out;
        }
        
        .event:hover img { /* Just change this */
            opacity: 1;
        }
        <div class="event">
           <img src="https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300">
        		<span class="titel">text text text text text text text text text </span>
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-06
          • 2011-02-28
          • 1970-01-01
          • 2021-07-31
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          相关资源
          最近更新 更多