-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-comparison.js
54 lines (45 loc) · 1.3 KB
/
image-comparison.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class ImageComparison extends HTMLElement
{
static tagName = 'image-comparison';
constructor()
{
super();
// element created
}
connectedCallback()
{
// browser calls this method when the element is added to the document
// (can be called many times if an element is repeatedly added/removed)
this.slidable();
if ( !this.style.getPropertyValue( '--value' ) )
{
this.setProp( 'value', `${this.slider.value}%` );
}
if ( this.hasAttribute( 'aspect-ratio' ) )
{
this.setProp( 'ar', this.getAttribute( 'aspect-ratio' ) );
}
}
slidable()
{
this.slider = document.createElement( 'input' );
this.slider.type = 'range';
this.slider.min = 0;
this.slider.max = 100;
this.slider.value = 50;
this.slider.step = 0.5;
this.slider.classList.add( 'range' );
this.append( this.slider );
this.slider.addEventListener( 'input', (e) =>
{
this.setProp( 'value', `${e.target.value}%` );
});
}
setProp( name, value )
{
this.style.setProperty( `--${name}`, value );
}
}
if ( 'customElements' in window ) {
customElements.define( ImageComparison.tagName, ImageComparison );
}