dthgs
dthgs

Reputation: 81

pointer-events: "auto" in child element not reversing parents "none" on mobile

A child div with "pointer-events: auto" does not receive any events when parent has "pointer-events: none" on mobile. Same thing works perfectly on desktop browsers. Why is that?

Here's my setup:

HTML

<div class="top">
  <div class="top-content">
    <p>top content</p>
  </div>
</div>
<div class="bottom">
  <div class="bottom-content-positioning-helper"></div>
  <div class="bottom-content">
    <p>bottom content</p>
  </div>
</div>

CSS

.top {
  width: 100%;
  height: 300px;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  z-index: 500;
  overflow: auto;
}

.top-content {
  background-color: red;  
  width: 100%;
  height: 600px;
  text-align: center;
}

.bottom {
  position: absolute;
  top: 150px;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  overflow: auto;
  pointer-events: none;
}

.bottom-content-positioning-helper {
  background-color: yellow;
  width: 100%;
  height: 150px;
  display: inline-block;
  vertical-align: top;
  opacity: 0.5;
  pointer-events: none;
}

.bottom-content {
  background-color: green;
  width: 100%;  
  text-align: center;
  display: inline-block;
  vertical-align: top;
  pointer-events: auto;
}

The two parent divs "top" and "bottom" with are stacked vertically. Both have a content div nested inside that is larger in height than their parent. As both parents also have "overflow: auto", they scroll their content. Nothing special until here.

When scrolling "bottom-content", it covers "top" until its middle and then crops any further scrolling. In order to still be able to scroll "top-content" with a cursor in the lower half of "top", (while "bottom-content" is at scroll position 0), "pointer-events: none" and "auto" are used on "bottom" and "bottom-content".

Please check out my JSFiddle to better understand what's happening here.

Everything is working just fine on desktop browsers. But not at all on mobile. I tested it:

On mobile "bottom-content" is not scrolling, as it is not receiving any events. Even though, it is explicitly told to do so with "pointer-events: auto"...

Any hint much appreciated!

Upvotes: 0

Views: 1075

Answers (1)

dthgs
dthgs

Reputation: 81

Ok, I still don't know why this is happening, but I found a way around it. I now read the touch events on the "bottom" element and reapply them to the scrollTop() property of THE SAME element via javascript. This sounds a bit weird, as this should be happening on its own, but it totally works! And the performance impact is minimal.

var lastScrollTop = 0;
var startY;
$(".bottom-content").on("touchstart", function(e){
  //necessary for mobile browsers
  startY = e.originalEvent.touches[0].clientY;
  lastScrollTop = $(".bottom").scrollTop();
});

$(".bottom-content").on("touchmove", function(e){
  //necessary for mobile browsers
  var currentY = e.originalEvent.touches[0].clientY;
  var scrollDistance = startY - currentY;
  $(".bottom").scrollTop(scrollDistance + lastScrollTop);
  e.preventDefault();
});

Here's the updated JSFiddle. Make sure to check it on desktop and on mobile again!

Upvotes: 0

Related Questions