JS頁面懸浮廣告


這個其實可以通過position:fixed;來直接固定元素的位置,這里是用JS緩沖運動來寫的懸浮框,顯示到右側與瀏覽器保持居中位置。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>懸浮框</title>
<style>
#div1{width: 150px; height: 150px; background: blue; position: absolute;right:0;bottom: 0;}
</style>
<script type="text/javascript">
window.onscroll = function()
{
    var oDiv = document.getElementById('div1');
    var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;

    StartMove(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop));
}
var timer = null;
function StartMove(iTarget)
{
    clearInterval(timer);
    timer = setInterval(function(){
        var oDiv = document.getElementById('div1');
        var speed = (iTarget-oDiv.offsetTop)/5;
        speed=speed>0?Math.ceil(speed):Math.floor(speed);

        if(oDiv.offsetTop == iTarget)
        {
            clearInterval(timer);
        }
        else
        {
            oDiv.style.top = oDiv.offsetTop + speed + 'px';
        }
    },30)
}
</script>
</head>
<body style = "height:2000px">
<div id = "div1">
</div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM