【问题标题】:Change img src on hover悬停时更改 img src
【发布时间】:2016-08-07 03:43:33
【问题描述】:

如何创建一个以小缩略图为前缀的 url 链接,这样当我将鼠标悬停在它们上时,链接颜色和缩略图都会发生变化

示例:

我现在使用带有锚标签的图像标签,我可以在悬停时更改锚标签文本颜色,但是我不知道如何相应地更改 img src

CSS:

.hoverable-link {
   color: gray;
}
.hoverable-link:hover {
   color: blue;
}

HTML:

<div>
  <img src="thumbnail-1">  //Change to thumbnail-2
  <a href="#" class="hoverable-link">Cool Link</a>
</div>

JsFiddle:https://jsfiddle.net/rbb5ow1v/9/

总结:
[1] 如何在 img src 悬停时更改它
[2] 如何在同一时间

【问题讨论】:

  • 使用浏览器中的开发者工具检查他们使用的样式和技术,并尝试将它们应用到您的项目中。
  • Alessio Cantarella 感谢您的建议。自从我开始使用 stackoverflow 和学习 Web 开发以来,已经发生了很多事情。我很抱歉我显示的问题质量很差。我已经用一个更易读、更容易理解的版本更新了它,也附上了我理解的答案。再次我真的很抱歉

标签: html css


【解决方案1】:

我会试试 fontello.com

一旦您选择了所需的图标,请按如下方式设置您的标签

 <a href="#"><span class="icon-twitter"></span>example</a>

当您执行 CSS 时,您只需将悬停状态应用到锚点,并且由于 fontello,它也会通过使用 CSS color 属性来更改该颜色。

编辑:

如果您使用的是您制作的特定 Twitter 图标。尝试将其更改为 SVG,并更改其填充。同样可以应用于 fontello,您可以在其中不显示任何内容并在悬停时显示。

【讨论】:

    【解决方案2】:

    [1] 如何在 img src 悬停时更改它
    您不能仅使用 CSS 来做到这一点,因为 srcDOM 属性 而不是 CSS 属性, HTML DOM 事件系统需要一些 javascript 来完成此操作

    <body>
    <div>
      <img onmouseenter="highlight(this)" onmouseleave="unhighlight(this)" 
           src="thumbnail1">
      <a href="#potato" class="hoverable-link">Some Link</a>
    </div>
    <script>
      function highlight(image) {
        image.src = "thumbnail2"; //Blue Image
      }
      function unhighlight(image) {
        image.src = "thumbnail1"; //Gray Image
      }
    </script>
    </body>
    

    JsFiddle:https://jsfiddle.net/f0c7p3tL/2/
    DOM 事件列表:http://www.w3schools.com/jsref/dom_obj_event.asp

    另一种方法是根本不使用 src DOM 属性。相反,您可以使用 background CSS 属性,这样您就可以利用 CSS:hover 选择器
    CSS:

    #my-thumbnail {
      background: url("/thumbnail1") no-repeat;
      width: 32px;
      height: 32px;
    }
    #my-thumbnail:hover {
      background: url("/thumbnail2") no-repeat;
    }
    

    HTML:

    <div>
      <img id="my-thumbnail">
      <a href="#potato" class="hoverable-link">Some Link</a>
    </div>
    

    JsFiddle:https://jsfiddle.net/7xoprwky/

    [2] 如何同时触发两个元素的悬停事件
    同样,这里有两种方法可用。

    首先是使用 javascript 和 HTML DOM 事件。在这种方法中,我们希望它们在周围的&lt;div&gt; 父元素上触发,而不是在任何一个子元素上触发事件。然后,在事件处理程序中,我们选择子元素并相应地更改它们的 DOM 属性

    <body>
    <div onmouseenter="highlight(this)" onmouseleave="unhighlight(this)">
      <img id="my-thumbnail" src="thumbnail1">
      <a   id="my-anchor" href="#potato">Some Link</a>
    </div>
    <script>
      var myThumbnail = document.getElementById('my-thumbnail'),
          myAnchor = document.getElementById('my-anchor');
    
      function highlight() {
        myThumbnail.src = "/thumbnail2";
        myAnchor.style.color = "blue";
        myAnchor.style.fontWeight = "bold";
      }
    
      function unhighlight() {
        myThumbnail.src = "/thumbnail1";
        myAnchor.style.color = "gray";
        myAnchor.style.fontWeight = "normal";
      }
    </script>
    </body>
    

    JsFiddle:https://jsfiddle.net/2uthconL/

    在第二种方法中,我们利用 CSS 选择器语法从周围的 div 中突出显示我们的内部元素

    CSS:

    #my-thumbnail-link {
    }
    #my-thumbnail-link img { /* Select all img tag inside div */
        background: url("/thumbnail1") no-repeat;
        width: 32px;
        height: 32px;
    }
    #my-thumbnail-link:hover img { /* Select all img tag inside div when it is hovered */
        background: url("/thumbnail2") no-repeat;
    }
    #my-thumbnail-link a { /* Select all a tag inside div */
        color: gray;
    }
    #my-thumbnail-link:hover a { /* Select all a tag inside div when it is hovered */
        color: blue;
        font-weight: bold;
    }
    

    HTML:

    <div id="my-thumbnail-link" class="vcenter-parent">
      <img class="vcenter-child">
      <a href="#potato" class="vcenter-child">Some Link</a>
    </div>
    

    JsFiddle:https://jsfiddle.net/x61dy0mk/2/
    更多关于 CSS 选择器:http://www.w3schools.com/cssref/css_selectors.asp

    如果您的缩略图只是一个静态资产,我推荐 CSS 方法而不是 Javascript HTML DOM 方法,因为它的可读性和可维护性(想象一下保留数千个事件处理程序)

    【讨论】:

      【解决方案3】:

      也许你可以试试这个:

      html

      <a href="http://twitter.com/me" class="twitterbird" title="Twitter link"></a>
      

      样式的css

      .twitterbird {
       margin-bottom: 10px;
       width: 160px;
       height:160px;
       display:block;
       background:transparent url('twitterbird.png') center top no-repeat;
      }
      
      .twitterbird:hover {
         background-image: url('twitterbird_hover.png');
      }
      

      这个答案是基于这个问题CSS: image link, change on hover

      更新 - 试试这个:

      html

      <ul>
        <li><a id="hoverable" href="#"><i class="home-icon"></i><span class="text">Link 1</span></a></li>
        <li><a id="hoverable" href="#"><i class="tshirt-icon"></i><span class="text">Link 2</span></a></li>
      </ul>
      

      css

      a {
        color: black;
        text-decoration: none;
      }
      
      ul {
        list-style-type: none;
        padding: 0;
        margin: 0;
      }
      
      .home-icon {
        background: url("http://s1.postimg.org/gk5fbl6vv/home_black.png") no-repeat;
        width: 32px;
        height: 32px;
        display: inline-block;
        margin-right: 20px;
      }
      
      a:hover .home-icon {
          background: url("http://s2.postimg.org/43870q29h/home_green.png") no-repeat;
      }
      
      .tshirt-icon {
        background: url("http://s30.postimg.org/61bqc12fh/tshirt_black.png") no-repeat;
        width: 32px;
        height: 32px;
        display: inline-block;
        margin-right: 20px;
      }
      
      a:hover .tshirt-icon {
        background: url("http://s17.postimg.org/3x9qzn8sb/tshirt_green.png") no-repeat;
      }
      
      a#hoverable:hover {
        color: green;
        font-weight: bold;
      }
      

      演示在此链接https://jsfiddle.net/nv4dw8vr/

      【讨论】:

      • 您也可以尝试检查元素,然后复制粘贴他们使用的方法并将其实现到您的代码中。
      • 我希望链接和图像都显示(如列表项目符号),并在链接悬停时更改它们的属性,而不是图像链接
      • 你的意思是悬停时图像和锚文本发生了变化?或类似的东西?
      • 是的,但是图像不是锚的背景,而是像列表项目符号一样作为前缀
      • 我已经更新了我的答案,看看有没有什么可以帮助你的。
      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 2015-12-22
      • 2012-06-10
      • 2019-03-08
      相关资源
      最近更新 更多