JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<div class="results"></div>
<p class="desc">Vitual Queue Made Of Two Stacks</p>
<div class="btns">
<button class="btn btn-enqueue">Enqueue</button>
<button class="btn btn-dequeue">Dequeue</button>
</div>
<div class="stacks">
<div class="stack stack1"></div>
<div class="stack stack2"></div>
</div>
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
background: #294C63;
}
.desc {
color: #fff;
}
.results {
display: flex;
height: 20vh;
}
.btns {
margin-bottom: 8vh;
}
.btn {
margin: 0 1em;
font-size: 1.2em;
}
.stacks {
display: flex;
justify-content: center;
width: 100%;
height: 30vh;
}
.stack {
display: flex;
flex-direction: column-reverse;
width: 30vw;
height: 40vh;
margin: 0 5vw;
border: 5px solid #95D4C4;
border-top: none;
transition: all 0.4s;
}
.item {
display: flex;
justify-content: center;
align-items: center;
height: 5vh;
width: 30vw;
color: #fff;
transition: all 0.4s;
user-select: none;
}
const colors = ['#9400D3', '#4B0082', '#0000FF', '#00FF00', '#FFFF00', '#FF7F00', '#FF0000']
let iColor = 0
const $stack1 = document.querySelector('.stack1')
const $stack2 = document.querySelector('.stack2')
const $results = document.querySelector('.results')
const $enqueue = document.querySelector('.btn-enqueue')
const $dequeue = document.querySelector('.btn-dequeue')
let iItem = 0
$enqueue.addEventListener('click', enqueue)
$dequeue.addEventListener('click', dequeue)
function enqueue () {
if ($stack1.children.length >= 8) { return }
if ($stack1.children.length >= 7) {
$enqueue.disabled = true
}
const rect = $stack1.getBoundingClientRect()
const $item = document.createElement('div')
$item.className = 'item'
$item.textContent = iItem++
$item.style.backgroundColor = colors[(iColor++) % colors.length]
$item.style.opacity = '0'
$item.style.transform = `translate3d(0, ${-rect.height * 1.5}px, 0)`
$stack1.appendChild($item)
$item.offsetHeight
$item.style.opacity = '1'
$item.style.transform = 'translate3d(0, 0, 0)'
}
async function dequeue () {
if ($stack2.children.length > 0) {
await stackToResults($stack2)
} else if ($stack1.children.length > 0) {
$enqueue.disabled = true
$dequeue.disabled = true
await stack1ToStack2()
await stackToResults($stack1)
}
$enqueue.disabled = false
$dequeue.disabled = false
}
async function stackToResults ($stack) {
await new Promise(resolve => {
const $item = $stack.children[$stack.children.length - 1]
const { top, left } = $item.getBoundingClientRect()
$item.remove()
$item.style = `
position: absolute;
top: ${top}px;
left: ${left}px;
background-color: ${$item.style.backgroundColor};
`
const forwardStyle = `
position: absolute;
top: 0;
left: ${$results.children.length * 5}vw;
width: 5vw;
height: 20vh;
background-color: ${$item.style.backgroundColor};
transition: none;
`
const id = `gen` + Date.now()
const destroyKeyframe = addKeyframes(`
40% {
top: 0;
}
100% { ${forwardStyle} }`,
id,
)
function animationend () {
$item.style = forwardStyle
$item.removeEventListener('animationend', animationend)
destroyKeyframe()
resolve()
}
$item.addEventListener('animationend', animationend)
$item.style.animation = `1s ${id}`
$results.appendChild($item)
})
}
async function stack1ToStack2 () {
while($stack1.children.length > 1) {
await new Promise(resolve => {
const $item = $stack1.children[$stack1.children.length - 1]
const rStack1 = $stack1.getBoundingClientRect()
const rStack2 = $stack2.getBoundingClientRect()
const { top, left, height } = $item.getBoundingClientRect()
$item.remove()
$item.style = `
position: absolute;
top: ${top}px;
left: ${left}px;
background-color: ${$item.style.backgroundColor};
`
$stack2.appendChild($item)
const id = `gen` + Date.now()
const destroyKeyframe = addKeyframes(`
30% {
top: ${rStack1.top}px;
left: ${left}px;
}
60% {
top: ${rStack1.top - 150}px;
left: ${left + rStack1.width / 2}px;
}
90% {
top: ${rStack2.top}px;
left: ${rStack2.left + $stack2.clientLeft}px;
}
100% {
top: ${rStack2.top + (8 - $stack2.children.length) * height}px;
left: ${rStack2.left + $stack2.clientLeft}px;
}
`,
id,
)
function setStatic () {
$item.style.position = 'static'
$item.removeEventListener('animationend', setStatic)
destroyKeyframe()
resolve()
}
$item.addEventListener('animationend', setStatic)
$item.style.animation = `1s ${id}`
})
}
}
function addKeyframes (style, id) {
const cssAnimation = document.createElement('style')
cssAnimation.type = 'text/css'
const rules = document.createTextNode(`@keyframes ${id} { ${style} }`)
cssAnimation.appendChild(rules)
document.getElementsByTagName("head")[0].appendChild(cssAnimation)
return () => cssAnimation.remove()
}
Also see: Tab Triggers