| 1 |
(function () { |
| 2 |
function typoCssVarsForEl(typo, prefix, el) { |
| 3 |
if (!typo) return; |
| 4 |
if (typo.family) el.style.setProperty(prefix + 'font-family', "'" + typo.family + "', sans-serif"); |
| 5 |
if (typo.weight) el.style.setProperty(prefix + 'font-weight', typo.weight); |
| 6 |
if (typo.transform) el.style.setProperty(prefix + 'text-transform', typo.transform); |
| 7 |
if (typo.style) el.style.setProperty(prefix + 'font-style', typo.style); |
| 8 |
if (typo.decoration) el.style.setProperty(prefix + 'text-decoration', typo.decoration); |
| 9 |
var su = typo.sizeUnit || 'px'; |
| 10 |
if (typo.sizeDesktop !== '' && typo.sizeDesktop != null) el.style.setProperty(prefix + 'font-size-d', typo.sizeDesktop + su); |
| 11 |
if (typo.sizeTablet !== '' && typo.sizeTablet != null) el.style.setProperty(prefix + 'font-size-t', typo.sizeTablet + su); |
| 12 |
if (typo.sizeMobile !== '' && typo.sizeMobile != null) el.style.setProperty(prefix + 'font-size-m', typo.sizeMobile + su); |
| 13 |
var lhu = typo.lineHeightUnit || 'px'; |
| 14 |
if (typo.lineHeightDesktop !== '' && typo.lineHeightDesktop != null) el.style.setProperty(prefix + 'line-height-d', typo.lineHeightDesktop + lhu); |
| 15 |
if (typo.lineHeightTablet !== '' && typo.lineHeightTablet != null) el.style.setProperty(prefix + 'line-height-t', typo.lineHeightTablet + lhu); |
| 16 |
if (typo.lineHeightMobile !== '' && typo.lineHeightMobile != null) el.style.setProperty(prefix + 'line-height-m', typo.lineHeightMobile + lhu); |
| 17 |
var lsu = typo.letterSpacingUnit || 'px'; |
| 18 |
if (typo.letterSpacingDesktop !== '' && typo.letterSpacingDesktop != null) { el.style.setProperty(prefix + 'letter-spacing-d', typo.letterSpacingDesktop + lsu); el.style.setProperty(prefix + 'letter-spacing', typo.letterSpacingDesktop + lsu); } |
| 19 |
if (typo.letterSpacingTablet !== '' && typo.letterSpacingTablet != null) el.style.setProperty(prefix + 'letter-spacing-t', typo.letterSpacingTablet + lsu); |
| 20 |
if (typo.letterSpacingMobile !== '' && typo.letterSpacingMobile != null) el.style.setProperty(prefix + 'letter-spacing-m', typo.letterSpacingMobile + lsu); |
| 21 |
var wsu = typo.wordSpacingUnit || 'px'; |
| 22 |
if (typo.wordSpacingDesktop !== '' && typo.wordSpacingDesktop != null) { el.style.setProperty(prefix + 'word-spacing-d', typo.wordSpacingDesktop + wsu); el.style.setProperty(prefix + 'word-spacing', typo.wordSpacingDesktop + wsu); } |
| 23 |
if (typo.wordSpacingTablet !== '' && typo.wordSpacingTablet != null) el.style.setProperty(prefix + 'word-spacing-t', typo.wordSpacingTablet + wsu); |
| 24 |
if (typo.wordSpacingMobile !== '' && typo.wordSpacingMobile != null) el.style.setProperty(prefix + 'word-spacing-m', typo.wordSpacingMobile + wsu); |
| 25 |
} |
| 26 |
|
| 27 |
function initSlider(sliderEl, o, pair) { |
| 28 |
var isVert = o.orientation === 'vertical'; |
| 29 |
var pos = (o.startPosition || 50) / 100; // 0..1 |
| 30 |
|
| 31 |
// Create structure |
| 32 |
var beforeDiv = document.createElement('div'); |
| 33 |
beforeDiv.className = 'bkbg-bag-before'; |
| 34 |
var beforeImg = document.createElement('img'); |
| 35 |
beforeImg.src = pair.beforeUrl; |
| 36 |
beforeImg.alt = pair.beforeAlt || 'Before'; |
| 37 |
beforeImg.loading = 'lazy'; |
| 38 |
beforeDiv.appendChild(beforeImg); |
| 39 |
|
| 40 |
var afterDiv = document.createElement('div'); |
| 41 |
afterDiv.className = 'bkbg-bag-after'; |
| 42 |
var afterImg = document.createElement('img'); |
| 43 |
afterImg.src = pair.afterUrl; |
| 44 |
afterImg.alt = pair.afterAlt || 'After'; |
| 45 |
afterImg.loading = 'lazy'; |
| 46 |
afterDiv.appendChild(afterImg); |
| 47 |
|
| 48 |
var lineEl = document.createElement('div'); |
| 49 |
lineEl.className = 'bkbg-bag-line'; |
| 50 |
lineEl.style.background = o.lineColor || '#ffffff'; |
| 51 |
lineEl.style.width = isVert ? '100%' : (o.lineWidth || 2) + 'px'; |
| 52 |
lineEl.style.height = isVert ? (o.lineWidth || 2) + 'px' : '100%'; |
| 53 |
|
| 54 |
// Handle |
| 55 |
var handleEl = document.createElement('div'); |
| 56 |
handleEl.className = 'bkbg-bag-handle'; |
| 57 |
handleEl.style.width = (o.handleSize || 44) + 'px'; |
| 58 |
handleEl.style.height = (o.handleSize || 44) + 'px'; |
| 59 |
handleEl.style.background = o.handleBg || '#ffffff'; |
| 60 |
handleEl.style.color = o.handleColor || '#374151'; |
| 61 |
handleEl.style.fontSize = Math.round((o.handleSize || 44) * 0.36) + 'px'; |
| 62 |
|
| 63 |
var handleStyle = o.handleStyle || 'circle-arrows'; |
| 64 |
if (handleStyle === 'circle-arrows') { |
| 65 |
handleEl.innerHTML = isVert ? '↕' : '↔'; // ↕ or ↔ |
| 66 |
} else if (handleStyle === 'double-arrow') { |
| 67 |
handleEl.innerHTML = isVert ? '⤈' : '⤇'; |
| 68 |
} else if (handleStyle === 'line') { |
| 69 |
handleEl.style.width = isVert ? '40px' : '3px'; |
| 70 |
handleEl.style.height = isVert ? '3px' : '40px'; |
| 71 |
handleEl.style.borderRadius = '3px'; |
| 72 |
} |
| 73 |
|
| 74 |
sliderEl.appendChild(beforeDiv); |
| 75 |
sliderEl.appendChild(afterDiv); |
| 76 |
sliderEl.appendChild(lineEl); |
| 77 |
sliderEl.appendChild(handleEl); |
| 78 |
|
| 79 |
// Labels |
| 80 |
if (o.showLabels) { |
| 81 |
var lbBefore = document.createElement('span'); |
| 82 |
lbBefore.className = 'bkbg-bag-label bkbg-bag-label-before'; |
| 83 |
lbBefore.textContent = pair.beforeLabel || 'Before'; |
| 84 |
lbBefore.style.background = o.labelBg || 'rgba(0,0,0,0.55)'; |
| 85 |
lbBefore.style.color = o.labelColor || '#ffffff'; |
| 86 |
sliderEl.appendChild(lbBefore); |
| 87 |
|
| 88 |
var lbAfter = document.createElement('span'); |
| 89 |
lbAfter.className = 'bkbg-bag-label bkbg-bag-label-after'; |
| 90 |
lbAfter.textContent = pair.afterLabel || 'After'; |
| 91 |
lbAfter.style.background = o.labelBg || 'rgba(0,0,0,0.55)'; |
| 92 |
lbAfter.style.color = o.labelColor || '#ffffff'; |
| 93 |
sliderEl.appendChild(lbAfter); |
| 94 |
} |
| 95 |
|
| 96 |
function applyPos(p) { |
| 97 |
var pct = Math.max(0, Math.min(100, p * 100)); |
| 98 |
if (isVert) { |
| 99 |
afterDiv.style.clipPath = 'inset(' + pct + '% 0 0 0)'; |
| 100 |
lineEl.style.top = pct + '%'; |
| 101 |
lineEl.style.transform = 'translateY(-50%)'; |
| 102 |
handleEl.style.top = pct + '%'; |
| 103 |
handleEl.style.left = '50%'; |
| 104 |
handleEl.style.transform = 'translate(-50%, -50%)'; |
| 105 |
} else { |
| 106 |
afterDiv.style.clipPath = 'inset(0 0 0 ' + pct + '%)'; |
| 107 |
lineEl.style.left = pct + '%'; |
| 108 |
lineEl.style.transform = 'translateX(-50%)'; |
| 109 |
handleEl.style.left = pct + '%'; |
| 110 |
handleEl.style.top = '50%'; |
| 111 |
handleEl.style.transform = 'translate(-50%, -50%)'; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
applyPos(pos); |
| 116 |
|
| 117 |
function getPos(e, rect) { |
| 118 |
var clientX = e.touches ? e.touches[0].clientX : e.clientX; |
| 119 |
var clientY = e.touches ? e.touches[0].clientY : e.clientY; |
| 120 |
if (isVert) { |
| 121 |
return (clientY - rect.top) / rect.height; |
| 122 |
} |
| 123 |
return (clientX - rect.left) / rect.width; |
| 124 |
} |
| 125 |
|
| 126 |
var dragging = false; |
| 127 |
|
| 128 |
function onStart(e) { |
| 129 |
e.preventDefault(); |
| 130 |
dragging = true; |
| 131 |
var rect = sliderEl.getBoundingClientRect(); |
| 132 |
pos = getPos(e, rect); |
| 133 |
applyPos(pos); |
| 134 |
} |
| 135 |
function onMove(e) { |
| 136 |
if (!dragging) return; |
| 137 |
e.preventDefault(); |
| 138 |
var rect = sliderEl.getBoundingClientRect(); |
| 139 |
pos = getPos(e, rect); |
| 140 |
applyPos(pos); |
| 141 |
} |
| 142 |
function onEnd() { dragging = false; } |
| 143 |
|
| 144 |
handleEl.addEventListener('mousedown', onStart); |
| 145 |
sliderEl.addEventListener('mousedown', onStart); |
| 146 |
window.addEventListener('mousemove', onMove); |
| 147 |
window.addEventListener('mouseup', onEnd); |
| 148 |
|
| 149 |
handleEl.addEventListener('touchstart', onStart, { passive: false }); |
| 150 |
sliderEl.addEventListener('touchstart', onStart, { passive: false }); |
| 151 |
window.addEventListener('touchmove', onMove, { passive: false }); |
| 152 |
window.addEventListener('touchend', onEnd); |
| 153 |
} |
| 154 |
|
| 155 |
function init() { |
| 156 |
document.querySelectorAll('.bkbg-bag-app').forEach(function (appEl) { |
| 157 |
if (appEl.dataset.rendered) return; |
| 158 |
appEl.dataset.rendered = '1'; |
| 159 |
var opts; |
| 160 |
try { opts = JSON.parse(appEl.dataset.opts || '{}'); } catch (e) { opts = {}; } |
| 161 |
var o = Object.assign({ |
| 162 |
pairs: [], |
| 163 |
columns: 2, |
| 164 |
gap: 20, |
| 165 |
borderRadius: 12, |
| 166 |
aspect: '4/3', |
| 167 |
startPosition: 50, |
| 168 |
orientation: 'horizontal', |
| 169 |
showLabels: true, |
| 170 |
showCaptions: true, |
| 171 |
handleStyle: 'circle-arrows', |
| 172 |
handleSize: 44, |
| 173 |
lineWidth: 2, |
| 174 |
labelSize: 12, |
| 175 |
captionSize: 13, |
| 176 |
handleBg: '#ffffff', |
| 177 |
handleColor: '#374151', |
| 178 |
lineColor: '#ffffff', |
| 179 |
labelBg: 'rgba(0,0,0,0.55)', |
| 180 |
labelColor: '#ffffff', |
| 181 |
captionBg: '#f8fafc', |
| 182 |
captionColor: '#374151', |
| 183 |
}, opts); |
| 184 |
|
| 185 |
if (!o.pairs || !o.pairs.length) return; |
| 186 |
|
| 187 |
var aspectParts = (o.aspect || '4/3').split('/'); |
| 188 |
var aspectRatio = parseInt(aspectParts[1], 10) / parseInt(aspectParts[0], 10) * 100; |
| 189 |
|
| 190 |
var outer = document.createElement('div'); |
| 191 |
outer.className = 'bkbg-bag-outer'; |
| 192 |
|
| 193 |
var grid = document.createElement('div'); |
| 194 |
grid.className = 'bkbg-bag-grid'; |
| 195 |
grid.style.gridTemplateColumns = 'repeat(' + o.columns + ', 1fr)'; |
| 196 |
grid.style.gap = o.gap + 'px'; |
| 197 |
|
| 198 |
o.pairs.forEach(function (pair) { |
| 199 |
if (!pair.beforeUrl || !pair.afterUrl) return; |
| 200 |
|
| 201 |
var item = document.createElement('div'); |
| 202 |
item.className = 'bkbg-bag-item'; |
| 203 |
item.style.borderRadius = o.borderRadius + 'px'; |
| 204 |
item.style.overflow = 'hidden'; |
| 205 |
item.style.boxShadow = '0 2px 12px rgba(0,0,0,0.10)'; |
| 206 |
|
| 207 |
var slider = document.createElement('div'); |
| 208 |
slider.className = 'bkbg-bag-slider' + (o.orientation === 'vertical' ? ' orient-vertical' : ''); |
| 209 |
slider.style.position = 'relative'; |
| 210 |
slider.style.paddingTop = aspectRatio + '%'; |
| 211 |
|
| 212 |
initSlider(slider, o, pair); |
| 213 |
item.appendChild(slider); |
| 214 |
|
| 215 |
if (o.showCaptions && pair.caption) { |
| 216 |
var cap = document.createElement('div'); |
| 217 |
cap.className = 'bkbg-bag-caption'; |
| 218 |
cap.textContent = pair.caption; |
| 219 |
cap.style.color = o.captionColor || '#374151'; |
| 220 |
cap.style.background = o.captionBg || '#f8fafc'; |
| 221 |
item.appendChild(cap); |
| 222 |
} |
| 223 |
grid.appendChild(item); |
| 224 |
}); |
| 225 |
|
| 226 |
outer.appendChild(grid); |
| 227 |
typoCssVarsForEl(opts.labelTypo, '--bkbg-bag-label-', outer); |
| 228 |
typoCssVarsForEl(opts.captionTypo, '--bkbg-bag-caption-', outer); |
| 229 |
appEl.parentNode.insertBefore(outer, appEl); |
| 230 |
appEl.style.display = 'none'; |
| 231 |
}); |
| 232 |
} |
| 233 |
|
| 234 |
if (document.readyState !== 'loading') { init(); } |
| 235 |
else { document.addEventListener('DOMContentLoaded', init); } |
| 236 |
})(); |
| 237 |
|