Danmarks Frie Forskningsfond

Erhverv

Forskere vil få din mobiloplader til at fylde en tiendedel

Forskere vil gøre mobilopladere grønnere og hurtigere. En sidegevinst vil være, at din mobiloplader kommer til at fylde en tiendedel…

Læs mere »
Back to top button
ANNONCE
/* Sticky for a second top ad script*/ ;(function () { var last_known_scroll_position = 0; var ticking = false; var floatingClass = 'js-floating'; var onTop = true; var floaters = []; var raf = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback){ window.setTimeout(callback, 1000 / 60) }; })(); function Floater(element) { this.element = element; this.ghost = null; this.isFloat = false; this.static_pos = null; this.headerHeight = 60; } Floater.prototype.shouldBeStatic = function(scroll_pos) { return scroll_pos < this.static_pos; } Floater.prototype.shouldBeFloating = function() { var boundingRect = this.element.getBoundingClientRect(); return boundingRect.top <= this.headerHeight; } Floater.prototype.switchToFloating = function(scroll_pos) { var boundingRect = this.element.getBoundingClientRect(); this.ghost = createGhost(this.element); this.isFloat = true; this.element.style.top = this.headerHeight + 'px'; this.element.style.width = this.element.offsetWidth + 'px'; this.element.style.zIndex = '10000'; // set orig to be fixed this.element.style.position = 'fixed'; // set orig to be fixed this.ghost.style.width = boundingRect.width + 'px'; this.ghost.style.height = boundingRect.height + 'px'; this.ghost.style.display = 'block'; // set ghost to be visible this.static_pos = scroll_pos; this.element.classList.add(floatingClass); } Floater.prototype.switchToStatic = function(scroll_pos) { this.isFloat = false; this.static_pos = null; this.element.style.width = 'auto'; this.element.style.position = 'static'; // set orig to be fixed this.element.style.width = 'auto'; if (this.ghost) { this.ghost.style.display = 'none'; // set ghost to be visible this.ghost.remove(); } this.element.classList.remove(floatingClass); } function initFloaters() { var elements = []; var leftSticky = document.getElementById('div-gpt-ad-sticky_1'); var rightSticky = document.getElementById('div-gpt-ad-sticky_2'); if (leftSticky) elements.push(leftSticky); if (rightSticky) elements.push(rightSticky); // var elements = Array.prototype.slice.call(document.querySelectorAll('.js-float-on-scroll')); floaters = elements.map(function(element) { return new Floater(element); }) } function createGhost(el) { var parent = el.parentNode; var ghost = document.createElement('div'); ghost.style.display = 'none'; parent.insertBefore(ghost, el); return ghost; } function testFloaters(scroll_pos) { for(var i = 0; i < floaters.length; i++) { var floatData = floaters[i]; if(floatData.isFloat) { floatData.shouldBeStatic(scroll_pos) && floatData.switchToStatic(scroll_pos); } else { floatData.shouldBeFloating() && floatData.switchToFloating(scroll_pos); } } } function throttle(func, limit) { var inThrottle; return function () { var args = arguments; var context = this; if (!inThrottle) { func.apply(context, args); inThrottle = true; setTimeout(function() { inThrottle = false }, limit); } } } function update(scroll_pos) { testFloaters(scroll_pos) } function init() { window.addEventListener('scroll', throttle(function(e) { if (last_known_scroll_position === window.scrollY) return; last_known_scroll_position = window.scrollY; if (!ticking) { raf(function() { update(last_known_scroll_position); ticking = false; }); ticking = true; } }, 25)); initFloaters(); } window.addEventListener('DOMContentLoaded', function (event){ init(); }); }());