document.addEventListener("DOMContentLoaded", function(event) { var iframe = document.getElementById('partner_deltareisen'); var partner_id = iframe.dataset.partner_id; var iframe_url = 'https://partner.deltareisen.cz'; iframe.setAttribute('src', iframe_url + '/?ta_id=' + partner_id); var scroll_top = function (pos) { if (pos) { //set if ('scrollY' in window) { window.scroll(0, pos); } else { document.documentElement.scrollTop = pos; } } else { //get if ('scrollY' in window) { return window.scrollY; } else { return document.documentElement.scrollTop; } } }; function scrollTo(Y) { var easingFunction = function (t) { return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; }; var start = Date.now(), from = scroll_top(); if (from === Y) { return; } function min(a, b) { return a < b ? a : b; } function scroll() { var currentTime = Date.now(), time = min(1, ((currentTime - start) / 500)), easedT = easingFunction(time); scroll_top((easedT * (Y - from)) + from); if (time < 1) requestAnimationFrame(scroll); } requestAnimationFrame(scroll); } var iframe_position = iframe.getBoundingClientRect().top + scroll_top(); window.addEventListener && window.addEventListener("message", OnMessage, false) || window.attachEvent && window.attachEvent("onmessage", OnMessage); function OnMessage(e) { //Při přijetí zprávy z iframu switch (e.data.type) { case 'resize': iframe.style.height = e.data.value + 'px'; break; case 'get_url_params': iframe.contentWindow.postMessage({ type: 'url_params', value: location.search }, iframe_url); break; case 'set_url_params': history.pushState(null, null, e.data.value); break; case 'scroll_to_iframe_position': iframe.contentWindow.postMessage({ // Do iframu se nastaví pozice ze které se scroluje type: 'last_scroll', value: scroll_top() - iframe_position }, iframe_url); scrollTo(iframe_position + e.data.value); break; } } var last_call = (new Date()); window.onscroll = function () { if (((new Date()) - last_call) > 50) { // nejvýše každých 50ms last_call = new Date(); iframe.contentWindow.postMessage({ // V iframe se nastaví scroll ve které se nachází type: 'iframe_scroll', value: scroll_top() - iframe_position }, iframe_url); } }; });