4.0.0 ā¢ Published 2 years ago
super-impose v4.0.0
Web Component that super imposes one child over another to the same scroll position
Examples
import { SuperImposeElement } from 'super-impose'
customElements.define('super-impose', SuperImposeElement)
class WrapElement extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
this.shadowRoot!.innerHTML = /*html*/ `
<style>
:host {
display: flex;
position: relative;
resize: both;
white-space: pre;
overflow: scroll;
}
[part="box"] {
}
</style>
<super-impose part="box">
<textarea slot="leader" part="leader" wrap="off">
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
some content wide enough to scroll<br />
</textarea>
<div slot="follower">
other content<br />
other content<br />
other content<br />
other content<br />
other content<br />
other content<br />
</div>
</super-impose>
`
}
}
customElements.define('wrap-element', WrapElement)
document.body.innerHTML = /*html*/ `
<style>
* {
background: transparent;
color: #bbb;
}
wrap-element {
width: 100px;
height: 100px;
}
</style>
<pre>
one
two
three
</pre>
<div style="padding: 20px; border: 1px solid #000; width: 100px; height: 100px; overflow: hidden; resize: both">
<super-impose onscroll="console.log(this.scrollTop, this.scrollLeft)">
<textarea slot="leader" wrap="off">
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
some content wide enough to scroll
</textarea>
<pre slot="follower" style="white-space: pre">
other content
other content
other content
other content
other content
other content
other content
</pre>
</super-impose>
</div>
<wrap-element></wrap-element>
`
const el = document.body.querySelector('super-impose')!
setTimeout(() => {
el.remove()
setTimeout(() => {
document.body.appendChild(el)
}, 500)
}, 2500)
import { SuperImposeElement } from 'super-impose'
customElements.define('super-impose', SuperImposeElement)
document.body.innerHTML = /*html*/ `
<super-impose
id="demo"
style="width: 120px; height: 120px; overflow: hidden; resize: both">
<pre slot="leader" style="color: #fa4; font-size: 25px;">
ššššššššš
ššššššššš
ššššššššš
ššššššššš
ššššššššš
ššššššššš
ššššššššš
ššššššššš
ššššššššš
</pre>
<pre slot="follower" style="white-space: pre; color:#1ff; font-size: 15px;">
ššššššš
ššššššš
ššššššš
ššššššš
ššššššš
ššššššš
</pre>
</super-impose>
</div>
`
const leader = document.querySelector('[slot=leader]')!
const mag = 20
let i = 0
const render = () => {
requestAnimationFrame(render)
const x = (i * (1 / 1000)) * Math.PI * 2
Object.assign(leader, {
scrollLeft: mag + Math.sin(x) * mag,
scrollTop: mag + Math.cos(x) * mag,
})
i += 1000 / 60
}
requestAnimationFrame(render)
API
import { SuperImposeElement } from 'super-impose'
customElements.define('super-impose', SuperImposeElement)
--
<super-impose>
<div slot="leader"></div>
<div slot="follower"></div>
</super-impose>
Credits
Contributing
All contributions are welcome!
License
MIT Ā© 2022 stagas