简体   繁体   English

在悬停时隐藏文本并显示图像

[英]Hide text on hover and show an image

I made a button with some text in it and I want the text to disappear on hover and an image to appear in its place. 我做了一个带有一些文本的按钮,我希望文本在悬停时消失,并在其位置显示一个图像。 Here is the HTML code: 这是HTML代码:

 #facebook { width: 200px; height: 50px; border-style: solid; border-color: white; border-width: 1px; border-radius: 7px; line-height: 50px; display: table-cell; transition-timing-function: ease-in-out; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; } #facebook:hover { width: 200px; height: 50px; border-style: solid; border-color: white; line-height: 50px; background: rgba(59, 89, 152, .6); } 
 <a href="https://www.facebook.com"> <div id="facebook"><span id="text">Facebook</span> </div> </a> 

So basically I want a Facebook logo to appear instead of text. 因此,基本上,我希望显示Facebook标志而不是文本。 I tried to do it alone, but I failed. 我试图独自做,但是失败了。 Does anyone know how this can be done? 有谁知道该怎么做?

Something like that? 这样的事吗?

https://jsfiddle.net/d04pLr6q/ https://jsfiddle.net/d04pLr6q/

#facebook { 
width: 200px; 
height: 50px; 
border-style: solid; 
border-color: white;
border-width: 1px;
border-radius: 7px;
line-height: 50px;
display: table-cell;
transition-timing-function: ease-in-out;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
-o-transition: all 0.5s;
-ms-transition: all 0.5s;
transition: all 0.5s;
background-color: rgba(59, 89, 152, 1);
overflow:hidden;
text-align: center;
color:white;
}
a{
text-decoration:none;
}
#facebook:hover{ 
width: 200px; 
height: 50px; 
border-style: solid; 
border-color: white;
line-height: 50px;
background-image: url(http://www.underconsideration.com/brandnew/archives/facebook_2015_logo_detail.png);
background-size: 100%;
background-position: center;
color: transparent;
}

You can replace this facebook img with any logo. 您可以使用任何徽标替换此facebook img。 Its not perfect but pure CSS :) 它不是完美的,但纯CSS :)

i suggest you don't use a div inside an a . 我建议您不要在a使用div it could cause errors. 它可能会导致错误。 so i changed a bit your html structure ( the solution works the same with your html structure ) . 所以我改变了你的html结构(解决方案与你的html结构相同)。

so i set the id #facebook directly on the a and if you want it to behave like a div just add display:block to a#facebook 所以我将id #facebook直接设置在a ,如果您希望它像div一样,只需将display:block添加到a#facebook

second, i've hidden the text when hover with visibility:hidden , you can also use opacity:0 . 第二,当我将鼠标悬停在visibility:hidden时,我已经隐藏了文本,您也可以使用opacity:0 in this case it won't matter. 在这种情况下,没关系。

keep in mind that you can use transition with opacity but not with visibility 记住,您可以对opacity使用transition ,但对visibility不能使用transition效果

then on :hover added a background-image to the #facebook element ( you can add whatever url you like ) 然后在:hover #facebook background-image添加到#facebook元素中(您可以添加所需的任何url)

let me know if it helps ;) 让我知道是否有帮助;)

 #facebook { width: 200px; height: 50px; border-style: solid; border-color: white; border-width: 1px; border-radius: 7px; line-height: 50px; display: table-cell; transition-timing-function: ease-in-out; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; background-position:center center; } #facebook:hover{ width: 200px; height: 50px; border-style: solid; border-color: white; line-height: 50px; background-color: rgba(59, 89, 152, .6); background-image:url("http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico"); background-repeat:no-repeat; } #facebook:hover #text { visibility:hidden; } 
 <a href="https://www.facebook.com" id="facebook"><span id="text">Facebook</span></a> 

You need 2 blocks of hover CSS. 您需要2个悬浮CSS块。 See example below and adjust as needed. 请参见下面的示例,并根据需要进行调整。

 #facebook { width: 200px!important; height: 50px!important; border-style: solid; border-color: white; border-width: 1px; border-radius: 7px; line-height: 50px; display: table-cell; transition-timing-function: ease-in-out; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; position: relative; display: inline-block; } img { width: auto; height: 50px; display: none; } #facebook:hover img { display: block; height: 50px; width: auto; } #facebook:hover #text { display: none; } #facebook img { width: 100%; height: auto; } 
 <a href="https://www.facebook.com"> <div id="facebook"> <span id="text">Facebook</span> <img src ="http://i65.tinypic.com/2evxmqs.jpg" width="150px" height="45px" /> </div></a> 

 #facebook { width: 200px; height: 50px; border-style: solid; border-color: white; border-width: 1px; border-radius: 7px; line-height: 50px; display: table-cell; transition-timing-function: ease-in-out; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; } #facebook:hover { width: 200px; height: 50px; border-style: solid; border-color: white; line-height: 50px; background: rgba(59, 89, 152, .6); } #facebook span { transition-timing-function: ease-in-out; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; } #facebook:hover span { color: transparent; } 
 <a href="https://www.facebook.com"> <div id="facebook"><span id="text">Facebook</span> </div> </a> 

you can use JavaScript for that. 您可以为此使用JavaScript。

document.getElementById('facebook').onmouseover = function(){
    document.getElementById('facebook').innerHTML = '';
    document.getElementById('facebook').style.backgroundImage = 'url(pic.jpg)';
    document.getElementById('facebook').style.backgroundSize = 'cover';
};
document.getElementById('facebook').onmouseout = function(){
    document.getElementById('facebook').innerHTML = 'Facebook';
    document.getElementById('facebook').style.backgroundImage = 'none';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM