热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

在img上改变图像src:悬停-CSS:Changeimagesrconimg:hover

Ineedtochange<img>sourceURLonhover.我需要在鼠标悬停时修改源URL。Ihavetriedthisbutwontwork

I need to change source URL on hover.

我需要在鼠标悬停时修改

DEMO

演示

http://jsfiddle.net/ssuryar/wcmHu/429/

http://jsfiddle.net/ssuryar/wcmHu/429/

#3


14  

What you could do is cheat a little bit by setting width and height to 0 to hide the actual image and apply some CSS to do what you want:

你所能做的就是把宽度和高度设为0来隐藏真实的图像并应用一些CSS来做你想做的事情:

#aks {
    width:0px;
    height:0px;
    background:url('http://dummyimage.com/100x100/000/fff');
    padding:50px;
}

#aks:hover {
    background: url('http://dummyimage.com/100x100/eb00eb/fff');
}

And the padding making the img tag the size you want it to have (half the size of your actual image).

以及使img标记具有您希望的大小的填充(实际图像大小的一半)。

Fiddle

小提琴

#4


12  

I had a similar problem but my solution was to have two images, one hidden (display:none) and one visible. On the hover over a surrounding span, the original image changes to display:none and the other image to display:block. (Might use 'inline' instead depending on your circumstances)

我也遇到过类似的问题,但我的解决方案是有两个图像,一个是隐藏的(显示:没有),一个是可见的。在鼠标悬停在周围的空间上,原始的图像改变显示:没有和其他图像显示:块。(可能会使用'inline',这取决于您的环境)

This example uses two span tags instead of images so you can see the result when running it here. I didn't have any online image sources to use unfortunately.

这个例子使用了两个span标签而不是图像,所以在这里运行时可以看到结果。不幸的是,我没有任何在线图片来源可以使用。

#onhover {
  display: none;
}
#surround:hover span[id="initial"] {
  display: none;
}
#surround:hover span[id="onhover"] {
  display: block;
}

    original
    replacement

#5


3  

Since you can't change the src with CSS: If jQuery is an option for you, check this fiddle.

由于不能使用CSS更改src:如果jQuery是您的选项,请检查这个小提琴。

Demo

$('#aks').hover(
    function(){
      $(this).attr('src','http://dummyimage.com/100x100/eb00eb/fff')
    },
    function(){
      $(this).attr('src','http://dummyimage.com/100x100/000/fff')
    }
)

It's basically using the .hover() method... it takes two functions to make it work. When you enter the hover and when you exit it.

它基本上是使用。hover()方法…它需要两个函数才能工作。当你进入鼠标悬停和退出鼠标时。

We are using the .attr (short for attribute) to change the src attribute.

我们使用.attr(属性的缩写)来更改src属性。

It's worth to note that you need the jQuery library included like in the fiddle to make this work.

值得注意的是,您需要jQuery库,比如fiddle。

#6


3  

I have one more solution. If anybody uses AngularJs : http://jsfiddle.net/ec9gn/30/

我还有一个解。如果有人使用AngularJs: http://jsfiddle.net/ec9gn/30/

there is a way to change image url on mouse over by using jQuery functionality as below:

同意AshisKumar的回答,有一种方法可以通过使用jQuery功能来更改鼠标上的图像url,如下所示:

$(function() {
  $("img")
    .mouseover(function() { 
        var src = $(this).attr("src").match(/[^\.]+/) + "over.gif";
        $(this).attr("src", url1); //URL @the time of mouse over on image
    })
    .mouseout(function() {
        var src = $(this).attr("src").replace("over", "");
        $(this).attr("src", url2); //default URL
    });
 });

#9


2  

I had similar problem - I want to replace picture on :hover but can't use BACKGRUND-IMAGE due to lack of Bootstrap's adaptive design.

我也遇到过类似的问题——我想要替换图片:悬停,但是由于缺少Bootstrap的自适应设计,所以不能使用backgrund图像。

If you like me only want to change the picture on :hover (but not insist of change SRC for the certain image tag) you can do something like this - it's CSS-only solution.

如果您喜欢我,只希望在:hover(但不坚持为特定的图像标记更改SRC)上更改图片,您可以这样做——这是仅针对css的解决方案。

HTML:

HTML:

  • CSS:

    CSS:

    li { position: relative; overflow: hidden; }
    li img.hoverPhoto {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      opacity: 0;
    }
    li.hover img { /* it's optional - for nicer transition effect */
      opacity: 0;
      -web-kit-transition:  opacity 1s ease;
      -moz-transition:  opacity 1s ease;li 
      -o-transition:    opacity 1s ease;
      transition:   opacity 1s ease;
    }
    li.hover img.hoverPhoto { opacity: 1; }
    

    If you want IE7-compatible code you may hide/show :HOVER image by positioning not by opacity.

    如果你想要ie7兼容的代码,你可以隐藏/显示:悬停图像通过定位而不是不透明。

    #10


    2  

    Personally, I would go with one of the Javascript / jQuery solutions. Not only does this keep your HTML semantic (i.e., an image is shown as an img element with it's usual src image defined in the markup), but if you use a JS / jQuery solution then you will also be able to use the JS / jQuery to preload your hover image.

    就我个人而言,我将使用一个Javascript / jQuery解决方案。这不仅保持了HTML的语义(例如。,将图像显示为img元素,并在标记中定义通常的src图像),但是如果使用JS / jQuery解决方案,那么还可以使用JS / jQuery预加载悬浮图像。

    Preloading the hover image will mean there is likely to be no download delay when the user hovers over the original image, resulting in your site behaving much more professionally.

    预加载悬停图像将意味着当用户悬停在原始图像上时,很可能不会出现下载延迟,从而使您的站点表现得更加专业。

    It does mean you have a dependency on JS, but the tiny minority that don't have JS enabled are probably not going to be too fussed - and everyone else will get a better experience... and your code will be good, too!

    这确实意味着您依赖于JS,但少数没有启用JS的人可能不会太过挑剔——其他人将获得更好的体验……你的代码也会很好!

    #11


    1  

    You can't change img tag's src attribute using CSS. Possible using Javascript onmouseover() event handler.

    您不能使用CSS更改img标签的src属性。可以使用Javascript onmouseover()事件处理程序。

    HTML:

    HTML:

    
    

    #16


    0  

    Heres a pure CSS solution. Put the visible image in the img tag, put the second image as a background in the css, then hide the image on hover.

    这是一个纯粹的CSS解决方案。将可见图像放入img标签中,将第二个图像作为css中的背景,然后将图像隐藏在悬停状态。

    .buttons{
    width:90%;
    margin-left:5%;
    margin-right:5%;
    margin-top:2%;
    }
    .buttons ul{}   
    .buttons ul li{
    display:inline-block;
    width:22%;
    margin:1%;
    position:relative;
    }
    .buttons ul li a p{
    position:absolute;
    top:40%;
    text-align:center;
    }   
    .but1{
    background:url('scales.jpg') center no-repeat;
    background-size:cover;
    }
    .but1 a:hover img{
    visibility:hidden;
    }   
    .but2{
    background:url('scales.jpg') center no-repeat;
    background-size:cover;
    }
    .but2 a:hover img{
    visibility:hidden;
    }   
    .but3{
    background:url('scales.jpg') center no-repeat;
    background-size:cover;
    }
    .but3 a:hover img{
    visibility:hidden;
    }   
    .but4{
    background:url('scales.jpg') center no-repeat;
    background-size:cover;
    }   
    .but4 a:hover img{
    visibility:hidden;
    }           
    
    
    

    #17


    0  

    The best way to change src for image is:

    改变src图像的最好方法是:

    
    

    See live demo: http://www.audenaerde.org/csstricks.html#imagereplacecss

    看到现场演示:http://www.audenaerde.org/csstricks.html imagereplacecss

    Enjoy!

    享受吧!

    #18


    0  

    Try This Code.

    试试这个代码。

       .card {
    
                width: 200px;
    
                height: 195px;
    
                position: relative;
    
                display: inline-block;
    
            }
    
            .card .img-top {
    
                display: none;
    
                position: absolute;
    
                top: 0;
    
                left: 0;
               
                z-index: 99;
    width:200px;
            }
    
            .card:hover .img-top {
    
                display: inline;
    
            }
     
    
        
    
        
    
        
    
     
        
    
        
    
            
    Card Back Card Front


    推荐阅读
    • 导航栏样式练习:项目实例解析
      本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
    • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
    • 自己用过的一些比较有用的css3新属性【HTML】
      web前端|html教程自己用过的一些比较用的css3新属性web前端-html教程css3刚推出不久,虽然大多数的css3属性在很多流行的浏览器中不支持,但我个人觉得还是要尽量开 ... [详细]
    • 一个登陆界面
      预览截图html部分123456789101112用户登入1314邮箱名称邮箱为空15密码密码为空16登 ... [详细]
    • 开发笔记:精通 CSS 第 10 章 变换过渡与动画 学习笔记
      开发笔记:精通 CSS 第 10 章 变换过渡与动画 学习笔记 ... [详细]
    • 使用Bootstrap创建响应式渐变固定头部导航栏的方法
      本文详细介绍了如何利用Bootstrap框架构建一个具有渐变效果的固定顶部响应式导航栏,包括HTML结构、CSS样式以及JavaScript交互的完整实现过程。适合前端开发者和学习者参考。 ... [详细]
    • 博主从零开始学习HTML(入门基础)
      从零开始学习HTML(入门基础)互联网三大基石HTTP协议URL:统一资源定位符HTML:超文本标记语言HTML的Head标签中的常用元素&amp;lt;!--告知 ... [详细]
    • 深入理解OAuth认证机制
      本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
    • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
    • 国内BI工具迎战国际巨头Tableau,稳步崛起
      尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
    • 如何高效创建和使用字体图标
      在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
    • 本文详细介绍了如何在Android 4.4及以上版本中配置WebView以实现内容的自动高度调整和屏幕适配,确保中文显示正常,并提供代码示例。 ... [详细]
    • 使用JS、HTML5和C3创建自定义弹出窗口
      本文介绍如何结合JavaScript、HTML5和C3.js来实现一个功能丰富的自定义弹出窗口。通过具体的代码示例,详细讲解了实现过程中的关键步骤和技术要点。 ... [详细]
    • 交互式左右滑动导航菜单设计
      本文介绍了一种使用HTML和JavaScript实现的左右可点击滑动导航菜单的方法,适用于需要展示多个链接或项目的网页布局。 ... [详细]
    • 优雅实现 jQuery 折叠展开下拉菜单
      本文介绍了一种使用 jQuery 实现的优雅折叠和展开效果的下拉菜单,通过简单的 HTML 结构和 CSS 样式,结合 jQuery 脚本,可以轻松创建出美观且功能强大的下拉菜单。 ... [详细]
    author-avatar
    强心脏229
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有