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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
(function (root, factory) { root.SimpleRellax = factory(); })(window, function () { const SimpleRellax = function (el, options) { const self = Object.create(SimpleRellax.prototype); self.options = { speed: -2, };
if (options) { Object.keys(options).forEach(function (key) { self.options[key] = options[key]; }); }
const elements = document.querySelectorAll(el); self.elems = elements;
let screenY; let posY = 0;
const setPosition = () => { const oldY = posY;
posY = document.documentElement.scrollTop;
if (oldY !== posY) { return true; }
return false; };
const updatePosition = (percentageY, speed) => { const result = {};
const valueY = speed * (100 * (1 - percentageY));
result.y = Math.round(valueY * 100) / 100;
return result; };
const createBlock = (el) => { const dataSpeed = el.getAttribute("data-rellax-speed");
const elPosY = 0;
const blockTop = elPosY + el.getBoundingClientRect().top; const blockHeight = el.clientHeight;
const percentageY = (elPosY + screenY - blockTop) / (screenY + blockHeight);
const speed = dataSpeed ? dataSpeed : self.options.speed;
const bases = updatePosition(percentageY, speed);
const style = el.style.cssText; let transform = "";
const searchResult = /transform\s*:/i.exec(style); if (searchResult) { const index = searchResult.index;
const trimmedStyle = style.slice(index); const delimiter = trimmedStyle.indexOf(";");
if (delimiter) { transform = " " + trimmedStyle.slice(11, delimiter).replace(/\s/g, ""); } else { transform = " " + trimmedStyle.slice(11).replace(/\s/g, ""); } }
return { baseY: bases.y, top: blockTop, height: blockHeight, speed: speed, style, transform, }; };
let blocks = []; const cacheBlocks = () => { for (let i = 0; i < self.elems.length; i++) { const block = createBlock(self.elems[i]); blocks.push(block); } };
const animate = () => { for (let i = 0; i < self.elems.length; i++) { const percentageY = (posY + screenY - blocks[i].top) / (screenY + blocks[i].height);
const position = updatePosition(percentageY, blocks[i].speed);
const positionY = position.y - blocks[i].baseY;
const translate = `translate3d(0, ${positionY}px, 0)` + blocks[i].transform; self.elems[i].style["transform"] = translate; } };
let loopId = null; const loop = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || function (callback) { return setTimeout(callback, 1000 / 60); };
const deferredUpdate = () => { window.removeEventListener("resize", deferredUpdate); window.removeEventListener("orientationchange", deferredUpdate); window.removeEventListener("scroll", deferredUpdate); window.removeEventListener("touchmove", deferredUpdate);
loopId = loop(update); };
let pause = true; const update = () => { if (setPosition() && pause === false) { animate();
loopId = loop(update); } else { window.addEventListener("resize", deferredUpdate); window.addEventListener("orientationchange", deferredUpdate); window.addEventListener("scroll", deferredUpdate); window.addEventListener("touchmove", deferredUpdate); } };
const init = () => { screenY = window.innerHeight;
setPosition();
blocks = []; cacheBlocks();
animate();
if (pause) { pause = false; window.addEventListener("resize", init); update(); } };
init(); };
return SimpleRellax; });
|