How to add a different image after card is fllipped(translated 180deg)

Viewed 101

This is a card created by using tailwind css. when hovered on image, image is flipped and contents of cards are reavealed. i want to show a new different image when card is opened. pls upload code adding a different image when card is opened. it would be be better if the answer would be in tailwind.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body class="flex justify-center h-screen items-center bg-red-100">
  <div class="card w-80 h-96 bg-white shadow-xl flex flex-col justify-center items-center relative">

    <img src="https://i.ibb.co/YTdNfPz/Optimized-IMG-20210228-100309-1-min-p5nqi9zcep08zfz2sm2vkfob3ec7bz7tl5tf5h7z1c.jpg" width="100px" class="image w-full h-full z-10 absolute shadow-xl bg-black" />
    <h2 class="font-bold text-3xl">Khagesh Bansal</h2>
    <h2 class="text-xl text-red-500 font-bold">Web Developer</h2>

    <div class="social m-5">
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500">
        <i class="fab fa-facebook-f fa-fw"></i
        ></a>
        <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
          ><i class="fab fa-twitter fa-fw"></i
        ></a>
        <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
          ><i class="fab fa-linkedin-in fa-fw"></i
        ></a>
        <a
          href="#"
          class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
        >
          <i class="fab fa-instagram fa-fw"></i
        ></a>
      </div>
      <!-- social closed -->
    </div>
    <!--  card closed -->
  </body>

  <style>
    .card {
      transition: 1s;
    }

    .image {
      transform-style: preserve-3d;
      transform-origin: left;
      transition: 1s;
      backface-visibility: ;
    }
     .card:hover {
     transform: translateX(50%);
    } 

    .card:hover .image {
     transform: rotateY(-180deg);
    } 
  </style>
</html>

2 Answers

You could do this with a global handler like ommouseover and onmouseout using two elements front and back, each holding your different images.

Add the global handler in your HTML:

front: onmouseover="this.classList.toggle('hover');"

back: onmouseout="this.classList.toggle('hover');"

I added a parent wrapper around the two front and back elements to help align the two properly.

* {
  margin: 0;
  padding: 0;
}

.card {
  perspective: 1000px;
  transition: 1s;
}

.card:hover .flipper,
.card.hover .flipper {
  transform: rotateY(180deg);
}

.card:hover {
  transform: translateX(50%);
}

.card,
.front,
.back {
  width: 320px;
  height: 480px;
  position: relative;
}

.flipper {
  transition: 1s;
  transform-style: preserve-3d;
  position: absolute;
  left: 0;
  top: 0;
}

.front,
.back {
  backface-visibility: hidden;
  position: absolute;
}

.front {
  z-index: 2;
  transform: rotateY(0deg);
}

.back {
  transform: rotateY(180deg) translateY(15%);
  
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body class="flex justify-center h-screen items-center bg-red-100">
  <div class="card w-80 h-96 bg-white shadow-xl flex flex-col justify-center items-center relative">

    <div class="flipper">
      <div class="front" onmouseover="this.classList.toggle('hover');">
        <img src="https://i.ibb.co/YTdNfPz/Optimized-IMG-20210228-100309-1-min-p5nqi9zcep08zfz2sm2vkfob3ec7bz7tl5tf5h7z1c.jpg" width="100px" class="image w-full h-full z-10 absolute shadow-xl bg-black" />
      </div>
      <div class="back" onmouseout="this.classList.toggle('hover');">
        <img src="https://previews.123rf.com/images/bannosuke/bannosuke1709/bannosuke170900006/85572363-network-connection-with-matrix-binary-vector.jpg">
      </div>
    </div>


    <h2 class="font-bold text-3xl">Khagesh Bansal</h2>
    <h2 class="text-xl text-red-500 font-bold">Web Developer</h2>

    <div class="social m-5">
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500">
        <i class="fab fa-facebook-f fa-fw"></i></a>
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500">
        <i class="fab fa-twitter fa-fw"></i></a>
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"><i class="fab fa-linkedin-in fa-fw"></i></a>
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500">
        <i class="fab fa-instagram fa-fw"></i></a>
    </div>
    <!-- social closed -->
  </div>
  <!--  card closed -->
</body>

</html>

Use mouse event onmouseenter/onmouseleave to image and change image src as below:

function mouseEnter(ev){
ev.src="some_http.jpg";
}

function mouseLv(ev){
ev.src="old_src.jpg"
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body class="flex justify-center h-screen items-center bg-red-100">
  <div class="card w-80 h-96 bg-white shadow-xl flex flex-col justify-center items-center relative">

    <img onmouseenter="mouseEnter(this)" onmouseleave="mouseLv(this)" src="https://i.ibb.co/YTdNfPz/Optimized-IMG-20210228-100309-1-min-p5nqi9zcep08zfz2sm2vkfob3ec7bz7tl5tf5h7z1c.jpg" width="100px" class="image w-full h-full z-10 absolute shadow-xl bg-black" />
    <h2 class="font-bold text-3xl">Khagesh Bansal</h2>
    <h2 class="text-xl text-red-500 font-bold">Web Developer</h2>

    <div class="social m-5">
      <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500">
        <i class="fab fa-facebook-f fa-fw"></i
        ></a>
        <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
          ><i class="fab fa-twitter fa-fw"></i
        ></a>
        <a href="#" class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
          ><i class="fab fa-linkedin-in fa-fw"></i
        ></a>
        <a
          href="#"
          class="bg-black text-white p-1 m-1 text-lg hover:bg-red-500"
        >
          <i class="fab fa-instagram fa-fw"></i
        ></a>
      </div>
      <!-- social closed -->
    </div>
    <!--  card closed -->
  </body>

  <style>
    .card {
      transition: 1s;
    }

    .image {
      transform-style: preserve-3d;
      transform-origin: left;
      transition: 1s;
      backface-visibility: ;
    }
     .card:hover {
     transform: translateX(50%);
    } 

    .card:hover .image {
     transform: rotateY(-180deg);
    } 
  </style>
</html>

Related