PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / before-after / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/before-after/frontend.js

94 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
4 function clamp(n, min, max) {
5 n = parseFloat(n);
6 if (isNaN(n)) n = min;
7 return Math.max(min, Math.min(max, n));
8 }
9
10 function getPosFromEvent(e, wrap, orientation) {
11 var rect = wrap.getBoundingClientRect();
12 var point = e.touches && e.touches[0] ? e.touches[0] : e;
13 var x = point.clientX - rect.left;
14 var y = point.clientY - rect.top;
15 if (orientation === 'vertical') {
16 return clamp((y / rect.height) * 100, 0, 100);
17 }
18 return clamp((x / rect.width) * 100, 0, 100);
19 }
20
21 function setCssPos(root, pos) {
22 root.style.setProperty('--bkbg-ba-pos', pos + '%');
23 }
24
25 function initOne(root) {
26 var inner = root.querySelector('.bkbg-ba-inner');
27 if (!inner) return;
28
29 var range = root.querySelector('.bkbg-ba-range');
30 var orientation = root.getAttribute('data-orientation') || 'horizontal';
31 var start = clamp(root.getAttribute('data-pos') || 50, 0, 100);
32
33 setCssPos(root, start);
34 if (range) range.value = String(start);
35
36 var dragging = false;
37
38 function onDown(e) {
39 dragging = true;
40 try { e.preventDefault(); } catch (_) {}
41 var pos = getPosFromEvent(e, inner, orientation);
42 setCssPos(root, pos);
43 if (range) range.value = String(Math.round(pos));
44 }
45
46 function onMove(e) {
47 if (!dragging) return;
48 try { e.preventDefault(); } catch (_) {}
49 var pos = getPosFromEvent(e, inner, orientation);
50 setCssPos(root, pos);
51 if (range) range.value = String(Math.round(pos));
52 }
53
54 function onUp() {
55 dragging = false;
56 }
57
58 // Prefer pointer events when available
59 if (window.PointerEvent) {
60 inner.addEventListener('pointerdown', onDown, { passive: false });
61 window.addEventListener('pointermove', onMove, { passive: false });
62 window.addEventListener('pointerup', onUp);
63 window.addEventListener('pointercancel', onUp);
64 } else {
65 inner.addEventListener('mousedown', onDown);
66 window.addEventListener('mousemove', onMove);
67 window.addEventListener('mouseup', onUp);
68
69 inner.addEventListener('touchstart', onDown, { passive: false });
70 window.addEventListener('touchmove', onMove, { passive: false });
71 window.addEventListener('touchend', onUp);
72 window.addEventListener('touchcancel', onUp);
73 }
74
75 if (range) {
76 range.addEventListener('input', function () {
77 var pos = clamp(range.value, 0, 100);
78 setCssPos(root, pos);
79 });
80 }
81 }
82
83 function initAll() {
84 var nodes = document.querySelectorAll('.bkbg-ba-wrap');
85 for (var i = 0; i < nodes.length; i++) initOne(nodes[i]);
86 }
87
88 if (document.readyState === 'loading') {
89 document.addEventListener('DOMContentLoaded', initAll);
90 } else {
91 initAll();
92 }
93 })();
94