| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function hexToRgb(hex) { |
| 5 |
if (!hex || hex.charAt(0) !== '#') return '0,0,0'; |
| 6 |
return parseInt(hex.slice(1, 3), 16) + ',' + parseInt(hex.slice(3, 5), 16) + ',' + parseInt(hex.slice(5, 7), 16); |
| 7 |
} |
| 8 |
|
| 9 |
function buildClipPath(cfg) { |
| 10 |
var topLeft, topRight, botLeft, botRight; |
| 11 |
var tOff = cfg.topAngle; |
| 12 |
var bOff = cfg.bottomAngle; |
| 13 |
|
| 14 |
if (cfg.skewTop) { |
| 15 |
if (cfg.topDir === 'left-low') { |
| 16 |
topLeft = tOff + '%'; |
| 17 |
topRight = '0%'; |
| 18 |
} else { |
| 19 |
topLeft = '0%'; |
| 20 |
topRight = tOff + '%'; |
| 21 |
} |
| 22 |
} else { |
| 23 |
topLeft = topRight = '0%'; |
| 24 |
} |
| 25 |
|
| 26 |
if (cfg.skewBottom) { |
| 27 |
if (cfg.bottomDir === 'left-low') { |
| 28 |
botLeft = '100%'; |
| 29 |
botRight = (100 - bOff) + '%'; |
| 30 |
} else { |
| 31 |
botLeft = (100 - bOff) + '%'; |
| 32 |
botRight = '100%'; |
| 33 |
} |
| 34 |
} else { |
| 35 |
botLeft = botRight = '100%'; |
| 36 |
} |
| 37 |
|
| 38 |
return 'polygon(0 ' + topLeft + ', 100% ' + topRight + ', 100% ' + botRight + ', 0 ' + botLeft + ')'; |
| 39 |
} |
| 40 |
|
| 41 |
function isMobile() { |
| 42 |
return window.innerWidth <= 768; |
| 43 |
} |
| 44 |
|
| 45 |
function applySection(wrap) { |
| 46 |
var cfg = { |
| 47 |
skewTop: wrap.dataset.skewTop === 'true', |
| 48 |
skewBottom: wrap.dataset.skewBottom === 'true', |
| 49 |
topAngle: parseFloat(wrap.dataset.topAngle) || 4, |
| 50 |
bottomAngle: parseFloat(wrap.dataset.bottomAngle) || 4, |
| 51 |
topDir: wrap.dataset.topDir || 'left-low', |
| 52 |
bottomDir: wrap.dataset.bottomDir || 'left-high', |
| 53 |
bg: wrap.dataset.bg || '#1e40af', |
| 54 |
gradient: wrap.dataset.gradient === 'true', |
| 55 |
bgEnd: wrap.dataset.bgEnd || '#7c3aed', |
| 56 |
gradAngle: parseInt(wrap.dataset.gradAngle, 10) || 135, |
| 57 |
bgImg: wrap.dataset.bgImg || '', |
| 58 |
bgSize: wrap.dataset.bgSize || 'cover', |
| 59 |
bgPos: wrap.dataset.bgPos || 'center center', |
| 60 |
overlayColor: wrap.dataset.overlayColor || '#1e40af', |
| 61 |
overlayOp: parseFloat(wrap.dataset.overlayOpacity) || 0, |
| 62 |
minHeight: parseInt(wrap.dataset.minHeight, 10) || 400, |
| 63 |
pt: parseInt(wrap.dataset.pt, 10) || 100, |
| 64 |
pb: parseInt(wrap.dataset.pb, 10) || 100, |
| 65 |
extraPad: wrap.dataset.extraPad !== 'false', |
| 66 |
maxWidth: parseInt(wrap.dataset.maxWidth, 10) || 1100, |
| 67 |
textColor: wrap.dataset.textColor || '#ffffff', |
| 68 |
textAlign: wrap.dataset.textAlign || 'left', |
| 69 |
}; |
| 70 |
|
| 71 |
var mobile = isMobile(); |
| 72 |
|
| 73 |
/* Background */ |
| 74 |
if (cfg.bgImg) { |
| 75 |
var overlayStr = cfg.overlayOp > 0 |
| 76 |
? 'linear-gradient(rgba(' + hexToRgb(cfg.overlayColor) + ',' + (cfg.overlayOp / 100) + '),rgba(' + hexToRgb(cfg.overlayColor) + ',' + (cfg.overlayOp / 100) + ')),' |
| 77 |
: ''; |
| 78 |
wrap.style.backgroundImage = overlayStr + 'url(' + cfg.bgImg + ')'; |
| 79 |
wrap.style.backgroundSize = cfg.bgSize; |
| 80 |
wrap.style.backgroundPosition = cfg.bgPos; |
| 81 |
} else if (cfg.gradient) { |
| 82 |
wrap.style.background = 'linear-gradient(' + cfg.gradAngle + 'deg,' + cfg.bg + ',' + cfg.bgEnd + ')'; |
| 83 |
} else { |
| 84 |
wrap.style.background = cfg.bg; |
| 85 |
} |
| 86 |
|
| 87 |
/* Clip-path — skip on mobile */ |
| 88 |
if (!mobile) { |
| 89 |
var cp = buildClipPath(cfg); |
| 90 |
wrap.style.clipPath = cp; |
| 91 |
wrap.style.webkitClipPath = cp; |
| 92 |
} else { |
| 93 |
// Ensure we remove any previously applied clip-path when |
| 94 |
// resizing from desktop to mobile. |
| 95 |
wrap.style.clipPath = 'none'; |
| 96 |
wrap.style.webkitClipPath = 'none'; |
| 97 |
} |
| 98 |
|
| 99 |
/* Min-height & padding */ |
| 100 |
wrap.style.minHeight = cfg.minHeight + 'px'; |
| 101 |
var extraTop = (cfg.skewTop && cfg.extraPad && !mobile) ? (cfg.topAngle * 6) : 0; |
| 102 |
var extraBottom = (cfg.skewBottom && cfg.extraPad && !mobile) ? (cfg.bottomAngle * 6) : 0; |
| 103 |
wrap.style.paddingTop = (cfg.pt + extraTop) + 'px'; |
| 104 |
wrap.style.paddingBottom = (cfg.pb + extraBottom) + 'px'; |
| 105 |
|
| 106 |
/* Text color */ |
| 107 |
wrap.style.color = cfg.textColor; |
| 108 |
|
| 109 |
/* Content inner restrictions */ |
| 110 |
var content = wrap.querySelector('.bksk-content'); |
| 111 |
if (content) { |
| 112 |
content.style.maxWidth = cfg.maxWidth + 'px'; |
| 113 |
content.style.margin = '0 auto'; |
| 114 |
content.style.padding = '0 24px'; |
| 115 |
content.style.textAlign = cfg.textAlign; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function init() { |
| 120 |
var wraps = document.querySelectorAll('.bksk-wrap[data-bg]'); |
| 121 |
wraps.forEach(function (wrap) { applySection(wrap); }); |
| 122 |
|
| 123 |
/* Recompute clip-path on resize (mobile breakpoint) */ |
| 124 |
var resizeTimer; |
| 125 |
window.addEventListener('resize', function () { |
| 126 |
clearTimeout(resizeTimer); |
| 127 |
resizeTimer = setTimeout(function () { |
| 128 |
wraps.forEach(function (wrap) { applySection(wrap); }); |
| 129 |
}, 150); |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
if (document.readyState === 'loading') { |
| 134 |
document.addEventListener('DOMContentLoaded', init); |
| 135 |
} else { |
| 136 |
init(); |
| 137 |
} |
| 138 |
|
| 139 |
}()); |
| 140 |
|