In my experience and by that other answer here, it is not possible to do fade of opacity between two background-image
using only CSS3.
What you would have to do would be to create two overlapping elements, with background-image
different, and animate their respective opacity
via CSS3 Animation.
If you really want to do it in Javascript, this code here serves as an example, does not use any very new functionality, and works in Chrome, Firefox and even IE9:
<!DOCTYPE html>
<html lang="pt-br" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Teste</title>
<style type="text/css">
.container, .botao {
font: normal 16px tahoma;
text-align:center;
width: 150px;
height: 40px;
color:#FFF;
cursor: pointer;
display: inline-block;
vertical-align: bottom;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.container {
position: relative;
}
.botao {
padding:10px;
position: absolute;
left: 0px;
top: 0px;
}
.frente {
background: url(http://i.stack.imgur.com/5PWNy.png) 0 -100px;
z-index: 1;
}
.tras {
background: url(http://i.stack.imgur.com/5PWNy.png) 0 -150px;
z-index: 0;
}
.container:active .tras {
box-shadow: inset 0px 0px 10px rgba(0, 0, 0, 0.76);
-webkit-box-shadow: inset 0px 0px 10px rgba(0, 0, 0, 0.76);
-moz-transition: inset 0px 0px 10px rgba(0, 0, 0, 0.76);
-ms-transition: inset 0px 0px 10px rgba(0, 0, 0, 0.76);
-o-transition: inset 0px 0px 10px rgba(0, 0, 0, 0.76);
}
</style>
</head>
<body>
<span>Botão 1:</span>
<div class="container">
<div class="botao frente" style="opacity: 1;" id="el1">TESTE</div>
<div class="botao tras">TESTE</div>
</div>
<span>Botão 2:</span>
<div class="container">
<div class="botao frente" style="opacity: 1;" id="el2">TESTE 2</div>
<div class="botao tras">TESTE 2</div>
</div>
<script type="text/javascript">
function buttonMouseChanged(fadingIn) {
//Trabalhando com o Closure a nosso favor ;)
var _this = this, animate;
animate = function () {
var o = parseFloat(_this.style.opacity);
if (_this.fadingIn) {
o += 0.05;
if (o < 1)
setTimeout(animate, 1000 / 60);
else
o = 1;
} else {
o -= 0.05;
if (o > 0)
setTimeout(animate, 1000 / 60);
else
o = 0;
}
_this.style.opacity = o;
};
this.fadingIn = fadingIn;
setTimeout(animate, 1000 / 60);
}
function buttonMouseEnter() {
buttonMouseChanged.call(this, false);
}
function buttonMouseLeave() {
buttonMouseChanged.call(this, true);
}
function prepareButton(id) {
var btn = document.getElementById(id);
btn.onmouseenter = buttonMouseEnter;
btn.onmouseleave = buttonMouseLeave;
}
prepareButton("el1");
prepareButton("el2");
</script>
</body>
</html>
Note how the value of opacity
of el1
, and of el2
, starts defined as 1, to be able to do the parseFloat()
always work.
To make the animation improve a little, it would be interesting not to use a fixed value (like 0.05) to add and subtract from the opacity
, but a value proportional to the time elapsed since the last execution of the function animate()
.
What do you mean out of place? It’s that white line that appears on top of the black line, at the bottom?
– carlosrafaelgn
Explain your problem better, it’s not clear what you want.
– Paulo
no, like, I put a transition, but it doesn’t fade, it just changes the background position. http://jsfiddle.net/a5Yn8/3/
– Alexandre Lopes
It is that you are effectively animating the background position, so there is no fade. To have fade, you would have to animate another property
– carlosrafaelgn
Could you give me a hand? I also thought of using jQuery/Javascript to create some cool effect...
– Alexandre Lopes
Look at the buttons on this site: http://www.hoteldaweb.com.br/hospedagemde-sites/ It uses jQuery effects.
– Alexandre Lopes
I suggest you reformulate the question and the title a little bit, to better reflect the desire for the fade effect. The expression "out of place" is very vague
– carlosrafaelgn