How to animate the layout of Divs and caption, when doing Hover in one of these elements?

Asked

Viewed 937 times

5

I’m creating a website, I’ve been asked to play a prank on the team. According to this image, when :Hover in one of the parts, all the text and image are ordered:

inserir a descrição da imagem aqui

I don’t know how to take this boot off, because javascript is not my strong suit. I wish you could help me.

Cropped photo

My HTML is:

<div class="equipa_membro">
  <div class="equipa_foto equipa_foto1">
    <div class="equipa_ft_tr equipa_ft2_tr1"></div>
    <div class="equipa_ft_tr equipa_ft2_tr2"></div>
    <div class="equipa_ft_tr equipa_ft2_tr3"></div>
    <div class="equipa_ft_tr equipa_ft2_tr4"></div>
    <div class="equipa_ft_tr equipa_ft2_tr5"></div>
    <div class="equipa_ft_tr equipa_ft2_tr6"></div>
  </div>
  <h5 class="nome_equipa1">oNem od bmoerM</h6>
  <p class="funcao_equipa2">ãnuonçF edsepDamehane</p>
</div>

My CSS:

.equipa_membro {height: 320px; margin-top: 50px;}
.equipa_foto {width: 282px; height: 244px; margin: 0 auto;}
.equipa_ft_tr {width: 141px; height: 122px;}

.equipa_ft1_tr1 {background: url(images/foto1.png); position: absolute; margin: 0px 0px; }
.equipa_ft1_tr2 {background: url(images/foto2.png); position: absolute; margin: 0px 70px; z-index: 1000;}
.equipa_ft1_tr3 {background: url(images/foto3.png); position: absolute; margin: 0px 141px;}
.equipa_ft1_tr4 {background: url(images/foto4.png); position: absolute; margin: 122px 0px;}
.equipa_ft1_tr5 {background: url(images/foto5.png); position: absolute; margin: 122px 70px; z-index: 1000;}
.equipa_ft1_tr6 {background: url(images/foto6.png); position: absolute; margin: 122px 141px;}
  • @Avelino, has a part of its edition that does not agree. The subject is being discussed in this Meta post (has an answer from me that explains what I disagree).

  • I, I see no problem in the edition of Avelino, @brasofilo.

2 answers

4

Put the following in HTML. O max-width it is necessary because without it it is as if the text occupies the whole screen horizontally:

<h5 id="texto" style="max-width: 150px" class="nome_equipa1 texto">oNem od bmoerM</h6>
<p id="texto2" style="max-width: 150px" class="funcao_equipa2 texto">ãnuoçF edsepDamehane</p>
<img src="imgembaralhada.jpg" id="image2" />
<img src="imgnormal.jpg" id="image1" />

And the following in Javascript. Don’t forget to put the jQuery script link http://code.jquery.com/jquery-1.11.0.min.js:

$(document).ready(function(){
    //Colocando o mouse sobre o texto(mousemove)
    $('#texto').on("mousemove", function(){
        $(this).text("Nome Do Membro");
    })
    //Retirando o mouse sobre o texto(mouseout)
    $('#texto').on("mouseout", function(){
        $(this).text("oNem oD bmoerM");
    })
    //Colocando o mouse sobre o texto(mousemove)
    $('#texto2').on("mousemove", function(){
        $(this).text("Função Desempenhada");
    })
    //Retirando o mouse sobre o texto(mouseout)
    $('#texto2').on("mouseout", function(){
        $(this).text("ãnuoçF edsepDamehane");
    })
    //Colocando o mouse sobre o texto(mousemove)
    $('#image2').on("mousemove", function(){
        $(this).attr("src","imgnormal.jpg");
    })
    //Retirando o mouse sobre o texto(mouseout)
    $('#image1').on("mouseout", function(){
        $(this).attr("src","imgmbaralhada.jpg");
    })
})
  • Thank you! The part of the text is resolved. In the image part I would like a solution, in which this would have several div’s each as a part of the image as background. And that by doing Hover in the text or in the respective image, they change to the correct position.

  • so I could even solve if you made the images available

  • I’ll get right on it. I’ll pass a Prite. Thank you!

  • is here the crop photo http://we.tl/B0ey4NSoU0. thank you! :)

  • Dude you wanted the images moving until you get the normal picture,?

  • Yes. The Divs with the images as background, moved to normal form.

Show 1 more comment

3


Here are two alternatives:

#1 - using CSS Transitions for the background image url

var linkImagens;
var divImagens;
$('.equipa_membro').hover(function () {
    divImagens = $(this).find('.equipa_ft_tr');
    linkImagens = divImagens.map(function () {
        return {
            top: $(this).css('margin-top'),
            left: $(this).css('margin-left')
        };
    })
    var qtdImagens = divImagens.length;
    divImagens.each(function (i) { // re-organizar os links
        var imagemAusar = (i + 2) < qtdImagens ? i + 2 : i - qtdImagens + 2;
        $(this).css('margin', linkImagens[imagemAusar]);

    });
    
        $(this).find('.nome_equipa1').toggle(); // mostrar o que está invisivel e esconder o que está visivel
        $(this).find('.funcao_equipa1').toggle(); // mostrar o que está invisivel e esconder o que está visivel
}, function () {
    divImagens.each(function (i) {
        $(this).css('margin', linkImagens[i]);
    });
        $(this).find('.nome_equipa1').toggle();
        $(this).find('.funcao_equipa1').toggle();
});

CSS

.equipa_ft_tr{
    transition: background-image 0.5s ease-in-out;
}

Example

#2 - Using CSS Transitions for position(margin) of background images

var linkImagens;
var divImagens;
$('.equipa_membro').hover(function () {
    divImagens = $(this).find('.equipa_ft_tr');
    linkImagens = divImagens.map(function () {
        return {
            top: $(this).css('margin-top'),
            left: $(this).css('margin-left')
        };
    })
    var qtdImagens = divImagens.length;
    divImagens.each(function (i) {
        var imagemAusar = i >= 2 ? i - 2 : i + 4;
        $(this).css('margin-top', linkImagens[imagemAusar].top);
        $(this).css('margin-left', linkImagens[imagemAusar].left);
    });
    $(this).find('.nome_equipa1').toggle();
    $(this).find('.funcao_equipa1').toggle();
}, function () {
    divImagens.each(function (i) {
        $(this).css('margin-top', linkImagens[i].top);
        $(this).css('margin-left', linkImagens[i].left);
    });
    $(this).find('.nome_equipa1').toggle();
    $(this).find('.funcao_equipa1').toggle();
});

CSS

.equipa_ft_tr {
    transition: margin 0.5s ease-in-out;
}

Example


In both suggestions that I present above I ordered the images to make javascript simpler. So the images are :

1 2 3
4 5 6

In addition to this change, I also use this HTML code to show/hide the right text:

<h5 class="nome_equipa1">oNem od bmoerM</h5>
<h5 class="nome_equipa1" style="display:none">Nome do Membro</h5>
<p class="funcao_equipa1">ãnuonçF edsepDamehane</p>
<p class="funcao_equipa1" style="display:none">Função Desempenhada</p>
  • Thank you! This solves the problem. There’s only one minor drawback, is that the effect is reversed, the image should appear disassembled, and be mounted after the Hover. I’m trying to do that, I just can’t get the pieces taken apart to be in the order that’s here.

  • @user3216077, prefer solution 1 or 2?

  • Solution 2 is exactly what I want. Thank you :)

  • @user3216077: http://jsfiddle.net/sJa8B/

  • 1

    Very Good! Thank you! D

  • I found that only the text part works on the Firefox and IE browsers. In Chrome everything works. I already put prefixes in CSS, and it doesn’t work anyway. I think the problem is in javascript.

  • @user3216077, here’s a version for FF and IE as well. I thought jQuery normalized this, but no. Test this: http://jsfiddle.net/k3PWg/

  • Thank you so much! This is it! Cheers :D

  • @user3216077, I’m glad I helped. Cheers.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.