用css悬停在img里面改变图片

时间:2017-06-05 06:54:37

标签: html css css3

我需要在悬停时更改源网址。

我试过这个但不行:

这是我的Css:

.nav > li > div {
    position: absolute;
    display: block;
    width: 100%;
    top: 38px;
    left: 0;
    opacity: 0;
    visibility: hidden;
    overflow: hidden;
    background: #ffffff;
    border-top-color: #5f7ec7;
    border-top-style: solid;
    border-top-width: 1px;
    border-bottom-color: #5f7ec7;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    -webkit-transition: all .3s ease .15s;
    -moz-transition: all .3s ease .15s;
    -o-transition: all .3s ease .15s;
    -ms-transition: all .3s ease .15s;
    transition: all .3s ease .15s;
}
.nav > li:hover > div {
    opacity: 1;
    visibility: visible;
    overflow: visible;
}

这是我的HTML:

<ul class="nav" style="width: 100%">
    <li>
        Something
        <a title="D" itemprop="url" href="/Default.aspx"><span itemprop="name">Something</span><img src="/images/Top-Menu-Layer/arrow_122b87.gif" style="vertical-align:middle; padding-left:5px; height:4px; width:7px" /></a>
       </li>
</ul>

我想通过css更改html里面的img标签的内容。我该怎么做 ?有什么建议?

2 个答案:

答案 0 :(得分:3)

使用content:url("imageURL");注意[此方法仅适用于Chrome而非Firefox或IE]。

.image{
  width:100px;
  height:100px;
}

.image:hover{
    content:url("http://via.placeholder.com/350x150");
}
<img class="image"/>

最好的方法是使用javascript,或者如果你想要一个完整的css解决方案,请使用background-image代替img代码,并在悬停时更改background-image

答案 1 :(得分:1)

你不能用纯CSS,但可以帮助Jquery。

$(document).ready(function(){
  $('.nav li').hover(function(){
        $('.target').attr('src','https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLd77JVS_4xE04KaLd3E-j2pTyiO_fTcEwHgQL9tj8GMsXZQW2');
   },function(){
	$('.target').attr('src','http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg');
   })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <ul class="nav" style="width: 100%">
   <li>
     Something
     <a title="D" itemprop="url" href="/Default.aspx">
        <span itemprop="name">Something</span>
        <img class="target" src="http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg" style="vertical-align:middle; padding-left:5px; height:40px; width:70px" />
        </a>
    </li>
</ul>