var url = "javascript:void(0)"; var floatImg = "float/float.jpg" function setFloatImg() { var div = document.createElement("div"); var a = document.createElement("a"); var img = document.createElement("img"); div.appendChild(a); a.appendChild(img); document.body.appendChild(div); div.style.borderRadius = "6px"; div.style.position = "fixed"; div.style.top = "0"; div.style.left = "0"; div.style.zIndex = "999999"; div.style.fontSize = "0"; div.style.display = "inline-block"; div.style.overflow = "hidden"; img.src = floatImg; img.style.width = "300px"; a.href = url; a.target = "_blank"; var boxData = { x: 0, y: 0, lastRunTime: null, xSpeed: 40, ySpeed: 40, timer: null } function _run() { if (boxData.lastRunTime) { var duration = (Date.now() - boxData.lastRunTime) / 1000; boxData.x += boxData.xSpeed * duration; boxData.y += boxData.ySpeed * duration; if (boxData.x >= window.innerWidth - div.offsetWidth) { boxData.xSpeed = - boxData.xSpeed; boxData.x = window.innerWidth - div.offsetWidth; } if (boxData.y >= window.innerHeight - div.offsetHeight) { boxData.ySpeed = - boxData.ySpeed; boxData.y = window.innerHeight - div.offsetHeight; } if (boxData.x <= 0) { boxData.xSpeed = - boxData.xSpeed; boxData.x = 0; } if (boxData.y <= 0) { boxData.ySpeed = - boxData.ySpeed; boxData.y = 0; } } boxData.lastRunTime = Date.now(); div.style.left = boxData.x + "px"; div.style.top = boxData.y + "px"; if ('requestAnimationFrame' in window){ boxData.timer = requestAnimationFrame(function(){ _run(); }) }else{ boxData.timer = setTimeout(function () { _run(); }, 1000 / 60); } } div.onmouseleave = function () { boxData.lastRunTime = null; _run(); } div.onmouseover = function () { if ('cancelAnimationFrame' in window){ cancelAnimationFrame(boxData.timer) }else{ clearTimeout(boxData.timer) } } _run(); } window.onload = function () { setFloatImg(); }