【问题标题】:Replace or show another image o top of a img tag on hover悬停时在 img 标签顶部替换或显示另一个图像
【发布时间】:2019-02-06 02:18:18
【问题描述】:

我检查了其他问题,但它们不适用于我的案例,我不确定是否可行。

我需要在悬停时替换图像。

我无法使用 javascript 或更改 html。我只能使用 CSS。

我已经尝试了很多方法,我什至设法做到了,通过使用 visibility: hidden;在 img 上并在父级上应用背景图像。

然而,它变得毫无用处,因为 img 标签是动作所在。

HTML

 <ul class="or-body-icons ZERO">
    <li id="liIconDest-or1">
       <img id="iconDest-or1" tabindex="-1" alt="Negócios" src="https://img2.gratispng.com/20180320/bke/kisspng-computer-icons-clip-art-sales-ico-5ab10c441b8ca8.6504430515215524521129.jpg" title="Negócios" style="cursor: pointer" width="16" height="16" border="0">
    </li>
 </ul>

CSS

.ZERO {
padding: 0;
margin: 0;
border: 0;
}

#iconDest-or1{
 width:150px;
 height:100px;
 position: relative;
 z-index: 1;
}


#liIconDest-or1:hover {
 box-sizing: border-box;
 background-image: url(https://img2.gratispng.com/20180304/rde/kisspng-ico-pencil-icon-pencil-5a9c43a0652ef3.9860290115201903684145.jpg);
 background-size: 320px;
 background-repeat: no-repeat;
 cursor: pointer;
 z-index: 10;
 position: relative;
 width:320px;
 height:320px;
 }

ul.or-body-icons li {
list-style-type: none;
float: left;
width: auto;
height: 16px;
line-height: 16px;
margin: 3px 2px;
padding: 0;
}

【问题讨论】:

    标签: css css-selectors


    【解决方案1】:

    您可以在 li 元素中添加一些 CSS 以在 li 悬停时隐藏其中的任何图像。这将解决您在悬停时将“可见性:隐藏”应用于 img 的问题:

    #liIconDest-or1:hover img {
      visibility: hidden;
    }
    

    【讨论】:

    • @BrunoDeFaria 您可以尝试“显示:无”或“不透明度:0”,而不是“可见性:隐藏”
    【解决方案2】:

    您可以使用悬停伪选择器更改背景图像:

    https://jsfiddle.net/moy2010/sgu2jft4/3/

    #test {
      background-image: url('https://dummyimage.com/600x400/000/fff');
      background-size: 100%;
      background-repeat: no-repeat;
      width: 300px;
      height: 300px;
    }
    
    #test:hover {
      background-image: url('https://dummyimage.com/600x300/000/fff');
    }
    

    编辑:

    没有注意到您想要覆盖来自 img 标签的图像。这是一个更新的 jsfiddle:

    https://jsfiddle.net/moy2010/sgu2jft4/8/

    【讨论】:

    • 我之前已经尝试过使用可见性:隐藏,但我无法使用它。那里有javascripts。但是感谢您的努力。
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多