yavg
yavg

Reputation: 3051

avoid mouse events on one div that is below another

I am trying to achieve the effect of "flip card". in the front, I have an element that has a click event, every time you click on it (.link). In the back there is nothing, but when I pass the cursor where the element with the .link class that is in the front, the click event is executed.

enter image description here

I would like to completely isolate the elements from the front part of the ones in the back part. how can I do it?

<div class="scene scene--card">
  <div class="card">
    <div class="card__face card__face--front">
      <a class="front_button link">
          text
      </a>     

    </div>
    <div class="card__face card__face--back">
      back
    </div>
  </div>
 </div>

.scene {
  width: 200px;
  height: 300px;
  border: 1px solid #CCC;
  margin: 40px 0;
  perspective: 600px;
}

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

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

.card__face {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 260px;
  color: white;
  text-align: center;
  font-weight: bold;
  font-size: 40px;
  backface-visibility: hidden;
}

.card__face--front {
  background: red;
}

.card__face--back {
  background: blue;
  transform: rotateY(180deg);
}

.front_button{
  background:yellow;
  height:200px;
  width:100%;
  position:absolute;
  bottom:0px;
  left:0px;
  cursor: pointer;
}

.link{
 border:1px solid red; 
}

var card = document.querySelector('.card');
card.addEventListener( 'click', function() {
  card.classList.toggle('is-flipped');
});


var link = document.querySelector('.link');
 link.addEventListener( 'click', function() {
 alert("link");
});

this is my code:

https://codepen.io/anon/pen/pOWONW

testing code from @Temani user enter image description here

Upvotes: 3

Views: 2210

Answers (2)

Steven B.
Steven B.

Reputation: 9362

Remove the pointer events from the link when the card has the class is-flipped.

.card.is-flipped .front_button.link {
  pointer-events: none;
}

var card = document.querySelector('.card');
card.addEventListener( 'click', function() {
  card.classList.toggle('is-flipped');
});


var link = document.querySelector('.link');
link.addEventListener( 'click', function(e) {
e.stopPropagation();
 alert("link");
});
body { font-family: sans-serif; }

.scene {
  width: 200px;
  height: 300px;
  border: 1px solid #CCC;
  margin: 40px 0;
  perspective: 600px;
}

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

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

.card.is-flipped .front_button.link {
  pointer-events: none;
}

.card__face {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 260px;
  color: white;
  text-align: center;
  font-weight: bold;
  font-size: 40px;
  backface-visibility: hidden;
}

.card__face--front {
  background: red;
}

.card__face--back {
  background: blue;
  transform: rotateY(180deg);
}

.front_button{
  background:yellow;
  height:200px;
  width:100%;
  position:absolute;
  bottom:0px;
  left:0px;
  cursor: pointer;
}

.link{
 border:1px solid red; 
}
<div class="scene scene--card">
  <div class="card">
    <div class="card__face card__face--front">
      <a class="front_button link">
          text
      </a>     
    </div>
    <div class="card__face card__face--back">
      back
    </div>
  </div>
</div>
<p>Click card to flip.</p>

Upvotes: 1

Burhan B
Burhan B

Reputation: 130

You can use the "pointer-events" (https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events) in your css for the is-flipped class.

.card.is-flipped {
  transform: rotateY(180deg);
  pointer-events: none;
}

This makes it ignore all mouse events.

Upvotes: 0

Related Questions