Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Boundary Events</title>
</head>
<body>
  <div id="elem1" class="elem"></div>
  <div id="elem2" class="elem"></div>
</body>
</html>
 
body {
  touch-action: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
.elem {
  width: 100px;
  height: 100px;
  margin: 10px;
  background: blue;
}
.active {
  background: green;
}
 
var elements = document.querySelectorAll('.elem');
Array.from(elements).forEach(function(element) {
  var isActive = false;
  var isOver = false;
  
  element.addEventListener('pointerdown', function(event) {
    element.setPointerCapture(event.pointerId);
    element.classList.add('active');
    isActive = true;
    isOver = true;
  });
  
  element.addEventListener('pointerup', function() {
    element.classList.remove('active');
    isActive = false;
  });
  
  element.addEventListener('pointermove', function(event) {
    if (!isActive) {
      return;
    }
    
    var hoveredElement = event.target.ownerDocument.elementFromPoint(event.clientX, event.clientY);
    if (hoveredElement === element) {
      // Check the isOver flag to avoid unnecessary class operations
      if (!isOver) {
        isOver = true;
        element.classList.add('active');
      }
    } else {
      if (isOver) {
        isOver = false;
        element.classList.remove('active');
      }
    }
  });
});
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers