Home > Mobile >  Stop event bubbling in foreach loop
Stop event bubbling in foreach loop

Time:12-10

I am very new to web development, but I have a simple card flip animation with javascript. It works fine until I add links to the back of the cards. Once I do this it will flip the correct card but open the links from the card above. I believe it has to do with event bubbling, but I am unable to find a solution that will work.

I want all cards to work like the first one. What I mean is that the card flips when clicked on and shows the information and the links that the user can click on if they want too.

const card = document.querySelectorAll(".card__inner");

function flipCard() {
  this.classList.toggle('is-flipped');
}


card.forEach((card) => card.addEventListener("click", flipCard));
:root {
  --primary: #FFCE00;
  --secondary: #FE4880;
  --dark: #212121;
  --light: #F3F3F3;
  /* bottom back color*/
}

* {
  margin: 0;
  padding: 0;
}

body {
  font-family: montserrat, sans-serif;
  width: 100%;
  min-height: 100vh;
}

.card {
  margin: 100px auto 0;
  width: 400px;
  height: 600px;
  perspective: 1000px;
}

.card__inner {
  width: 100%;
  height: 100%;
  transition: transform 1s;
  transform-style: preserve-3d;
  cursor: pointer;
  position: relative;
}

.card__inner.is-flipped {
  transform: rotateY(180deg);
}

.card__face {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  overflow: hidden;
  border-radius: 16px;
  box-shadow: 0px 3px 18px 3px rgba(0, 0, 0, 0.2);
}

.card__face--front {
  background-image: url("iFoxify.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card__face--front h2 {
  color: rgb(0, 0, 0);
  font-size: 32px;
}

.card__face--back {
  background-color: var(--light);
  transform: rotateY(180deg);
}

.card__content {
  width: 100%;
  height: 100%;
}

.card__header {
  position: relative;
  padding: 30px 30px 40px;
}

.card__header:after {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(to bottom left, var(--primary) 10%, var(--secondary) 115%);
  z-index: -1;
  border-radius: 0px 0px 50% 0px;
}

.pp {
  display: block;
  width: 128px;
  height: 128px;
  margin: 0 auto 30px;
  border-radius: 50%;
  background-color: rgb(0, 0, 0);
  border: 5px solid rgb(0, 0, 0);
  object-fit: cover;
}

.card__header h2 {
  color: rgb(0, 0, 0);
  font-size: 32px;
  font-weight: 900;
  /* text-transform: uppercase; */
  text-align: center;
}

.card__body {
  padding: 30px;
}

.card__body h3 {
  color: var(--dark);
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 15px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Game Card</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div >
    <div >
      <div >
        <h2></h2>
      </div>
      <div >
        <div >
          <div >
            <img src="iFoxify.png" alt=""  />
            <h2>Swift and Java
              <h2>
          </div>
          <div >
            <h3>iFoxify</h3>
            <p>A simple app that shows random pictures of foxes.</p><br><br>
            <p><a href="https://play.google.com/store/apps/details?id=com.LucasDahl.ifoxify" target="_blank">Google Play</p>
                            
                            <p><a href="https://apps.apple.com/us/app/ifoxify/id1576016692" target = "_blank" >iOS</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
    
        <div >
            <div >
                <div >
                    <h2></h2>
                </div>
                <div >
                    <div >
                        <div >
                            <img src="babysleep.png" alt=""  />
                            <h2>Swift<h2>
                        </div>
                        <div >
                            <h3>Baby Sleepytime</h3>
                            <p>A simple white noise app.</p><br><br>
                            <p><a href="https://apps.apple.com/us/app/baby-sleepytime/id1480001818" target = "_blank" ><img alt="ApplePlayBadge" src="Download_on_the_App_Store_Badge_US-UK_RGB_blk_092917.svg" width="200" height="70"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
        <script src="main.js"></script>
    </body>
    </html>

CodePudding user response:

You need to close out your <a> tags. You've left them open, so everything under the first tag is a link.

Change:

<p><a href = "https://play.google.com/store/apps/details?id=com.LucasDahl.ifoxify" target="_blank">Google Play</p>

To:

<p><a href = "https://play.google.com/store/apps/details?id=com.LucasDahl.ifoxify" target="_blank">Google Play</a></p>

Do that for all the card links.

  • Related