Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ally.when.focusable Example</title>
</head>
<body>
<article id="example-introduction">
  <h1><code>ally.when.focusable</code> Example</h1>
  <p>Press the &quot;start&quot; button below. An element will be animated into view and focused once it is fully visible (yet before the transition ended). Press <kbd>ESC</kbd> to programmatically abort focusing the target element. Click on the &quot;abort&quot; button to focus another element during transition to prevent the target element from getting focused.</p>
</article>
<div id="example-html">
  <main>
    <button type="button" id="activate">loading&#x2026;</button>
    <button type="button">abort</button>
    <hr>
    <div id="example-container">
      <div id="container">
        <div class="thing" tabindex="1"></div>
      </div>
    </div>
  </main>
</div>
<script src="https://cdn.jsdelivr.net/ally.js/1.4.1/ally.min.js"></script>
</body>
</html>
 
#example-container {
  margin: 0;
}
.thing {
  box-sizing: border-box;
  width: 300px;
  height: 30px;
  border: 1px solid red;
  background: #CCFFFF;
  opacity: 0.5;
  -webkit-transform: translate3d(-300px, -30px, 0px);
      -ms-transform: translate(-300px, -30px);
          transform: translate3d(-300px, -30px, 0px);
  transition: -webkit-transform 0.1s 0s ease-in-out;
  transition:     -ms-transform 0.1s 0s ease-in-out;
  transition:         transform 0.1s 0s ease-in-out;
}
#container {
  width: 400px;
  height: 100px;
  overflow: hidden;
  border: 2px solid silver;
}
div:focus {
  background: #AAA;
}
.move {
  -webkit-transform: translate3d(60px, 50px, 0px);
      -ms-transform: translate(60px, 50px);
          transform: translate3d(60px, 50px, 0px);
  transition: -webkit-transform 2s 0s ease-in-out;
  transition:     -ms-transform 2s 0s ease-in-out;
  transition:         transform 2s 0s ease-in-out;
}
  
 
var button = document.getElementById('activate');
var element = document.querySelector('.thing');
var handle;
ally.when.key({
  escape: function(event) {
    console.log('aborted');
    handle && handle.disengage();
    handle = null;
  },
});
document.addEventListener('focus', function(event) {
  if (event.target === element) {
    handle = null;
    console.log('the element received focus');
    return;
  }
  if (event.target === button || !element.classList.contains('move')) {
    return;
  }
  console.log('a different element received focus');
}, true);
document.addEventListener('transitionend', function() {
  console.log('the transition ended');
}, true);
button.addEventListener('click', function() {
  if (element.classList.contains('move')) {
    element.classList.remove('move')
    button.textContent = 'start';
    return;
  }
  handle = ally.when.focusable({
    context: element,
    callback: function(element) {
      console.log('focusing element');
      element.focus();
    },
  });
  button.textContent = 'reset';
  element.classList.add('move');
}, true);
button.textContent = 'start';
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
rodneyrehmpro
0viewers