Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 1x 1x 1x 1x 1x 1x | import CustomElement from "../../custom-element/CustomElement";
import defineCustomElement from "../../custom-element/helpers/defineCustomElement";
import html from "../../virtual-dom/html";
import sliderCss from "./Slider-css"
export default class Slider extends CustomElement {
static get properties() {
return {
/**
* The value of the slider
*/
value: {
type: Number,
value: 0,
reflect: true,
afterUpdate: function () { // Do not use an arrow function so we can use Function.prototype.call()
this.refreshSlider(this.value);
}
}
}
}
static get styles() {
return sliderCss;
}
render() {
return html`
<div class="bg-overlay"></div>
<div class="thumb"></div>
`;
}
async refreshSlider(value: number) {
if (this.querySelector('.thumb')) {
this.querySelector('.thumb').style.left = (value / 100 *
this.offsetWidth -
this.querySelector('.thumb').offsetWidth / 2)
+ 'px';
}
}
connectedCallback() {
super.connectedCallback?.();
document.addEventListener('mousemove', e => this.eventHandler(e));
document.addEventListener('mouseup', e => this.eventHandler(e));
this.addEventListener('mousedown', e => this.eventHandler(e));
// this.refreshSlider(this.value);
//
// this.setColor(this.backgroundcolor);
}
updateX(x) {
// Offset the horizontal position to use the center of the thumbnail
let hPos = x - this.querySelector('.thumb').offsetWidth / 2;
// Restrict horizontal position to confines of component bounds
if (hPos > this.offsetWidth) {
hPos = this.offsetWidth;
}
if (hPos < 0) {
hPos = 0;
}
// Calculate the percentage horizontal position and set the value attribute through the setter API
this.value = (hPos / this.offsetWidth) * 100; 3
}
eventHandler(e) {
const bounds = this.getBoundingClientRect();
// Calculates horizontal position relative to left edge of the component
const x = e.clientX - bounds.left;
switch (e.type) {
// Set a boolean to indicate the user is dragging, update the "value" attribute, and update the slider position
case 'mousedown':
{
this.isDragging = true;
this.updateX(x);
this.refreshSlider(this.value);
}
break;
//Set the boolean to false to indicate the user is no longer dragging
case 'mouseup':
{
this.isDragging = false;
}
break;
// If the boolean indicates the user is dragging, updates the “value” attribute and updates the slider position
case 'mousemove':
{
if (this.isDragging) {
this.updateX(x);
this.refreshSlider(this.value);
}
}
break;
}
}
}
defineCustomElement('gcl-slider', Slider); |