<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 "start" 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 "abort" 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…</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;
transform: translate3d(-300px, -30px, 0px);
transform: translate(-300px, -30px);
transform: translate3d(-300px, -30px, 0px);
transition: transform 0.1s 0s ease-in-out;
transition: 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 {
transform: translate3d(60px, 50px, 0px);
transform: translate(60px, 50px);
transform: translate3d(60px, 50px, 0px);
transition: transform 2s 0s ease-in-out;
transition: 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
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |