English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery hover() method

Eventos do jQuery

When the mouse pointer hovers over the selected element, the hover() method specifies two functions to be executed.

This method triggers simultaneouslymouseenterandmouseleaveevent.

The call to the hover() method is a shorthand: ;$(selector).mouseenter(function_in).mouseleave(function_out)

Note:When a single function is passed, the hover() method will execute the function for both mouseenter and mouseleave events.

Syntax:

$(selector).hover(function_in, function_out)

Exemplo

Change the background color of all <p> elements when the mouse pointer hovers over them:

$("p").hover(function(){
  $(this).css("background-color", "yellow");
  }, function(){
  $(this).css("background-color", "lightblue");
});
Teste e Veja‹/›

Add special styles to list the items to hover over:

$(document).ready(function(){
  $("li").hover(function(){funcIn(this);}, function(){funcOut(this);});
});
function funcIn(x) {
  $(x).html("Mouse<b>ENTER</b> Pressed event triggered");
  $(x).css("background", "yellow");
}
function funcOut(x) {}}
  $(x).html("Evento de Sair do Mouse");
  $(x).css("background", "white");
}
Teste e Veja‹/›

Se apenas uma função for especificada, esta função será executada para os eventos mouseenter e mouseleave ao mesmo tempo:

$("div").hover(function(){
  $(this).css("background", randColor());
});
// Função de Obtenção de Cor Aleatória
function randColor() {
  return 'rgb(' + Math.floor(Math.random()*256) + ',' + Math.floor(Math.random()*256) + 
  ',' + Math.floor(Math.random()*256) + ')';
}
Teste e Veja‹/›

Valor do Parâmetro

ParâmetroDescrição
function_inFunção executada quando o ponteiro do mouse entra no elemento
function_outFunção executada quando o ponteiro do mouse sai do elemento (opcional)

Eventos do jQuery