Change mouseover element / mouseleave

5

I am trying to change the src of an element by using the attr property in mouseover and then mouseleave . The idea is to change a banner while the mouse is under an element and refresh again after you exit.

I tried two ways:

1st

    $('.quartaImagem').mouseover(function () {
        $(".banner").attr("src", "Content/img/fotosHome/treinamentos.jpg");
    });
    $('.quartaImagem').mouseleave(function () {
        bannerDefault;
    });

2nd

    $(document).ready(function (e) {
        $('.quartaImagem').hover(function (e) {
            $(".banner").attr("src", "Content/img/fotosHome/treinamentos.jpg");
        }, function (e) {
            $(this).attr('src', bannerDefault);
        })
    });
    
asked by anonymous 18.08.2014 / 20:14

1 answer

2

You should do something like this:

var bannerDefault; // declara a variável no escopo global
$('.quartaImagem').hover(function (e) {
    bannerDefault = $(".banner").attr("src"); // memoriza o src atual
    $(".banner").attr("src", "Content/img/fotosHome/treinamentos.jpg"); // define novo src
}, function (e) {
    $(".banner").attr('src', bannerDefault); // define o src que estava antes
});

jsfiddle

    
18.08.2014 / 21:00