You do not need Javascript .
Nowadays, a lot of the hover and toggle issues can only be handled with HTML and CSS. I would say Javascript should be used more to handle some behavior that should be dynamic. In your case, you have a text and want that when the :hover
event occurs, this text will be changed.
You can use data attributes ( those attributes that begin with data-*
) to define which text should be displayed with and without hover. For example:
<!-- TODO: usar nomes melhores para o atributo? -->
<a href='#' data-text-with-hover='Gostou? Adquira já!'
data-text-without-hover='Ligue 0800 123 4567'>
</a>
With this, in CSS you can use the function attr()
of the CSS to get the attributes of the element, in the case data-text-with-hover
and data-text-without-hover
. Thus, you can change the content
of the element according to its current state. For example:
elemento::after {
content: attr(data-text-without-hover)
}
elemento:hover::after {
content: attr(data-text-with-hover)
}
Follow a snippet:
a {
background: url(http://lorempixel.com/400/200/technics);
display: inline-block;
position: relative;
height: 200px;
width: 400px;
}
a::after {
background: rgba(0, 0, 0, .8);
bottom: 0;
color: #fff;
content: attr(data-text-without-hover);
display: inline-block;
height: 40px;
line-height: 40px;
position: absolute;
left: 0;
text-align: center;
width: 100%
}
a:hover::after {
content: attr(data-text-with-hover);
color: gold
}
<a href='#' data-text-with-hover='Gostou? Adquira já!'
data-text-without-hover='Ligue 0800 123 4567'></a>