這篇文章主要為大家展示了“JS如何實現右側懸浮框效果”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“JS如何實現右側懸浮框效果”這篇文章吧。
具體內容如下
讓一個div始終懸浮在右下角
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #div1{ width: 100px; height: 150px; background: red; position: absolute; bottom: 0px; right: 0px; } </style> <script> window.onscroll = function(){ var oDiv = document.getElementById('div1'); var scrollTop =document.documentElement.scrollTop||document.body.scrollTop;//瀏覽器兼容 startmove(document.documentElement.clientHeight - oDiv.offsetHeight + scrollTop) // document.documentElement.clientHeight 頁面可視區高度 } var timer = null; //懸浮框緩沖運動 function startmove(iTarget){ var oDiv = document.getElementById('div1'); clearInterval(timer); timer = setInterval(function(){ var speed = (iTarget-oDiv.offsetTop)/4; 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 > <div id="div1"></div> </body> </html>
讓div懸浮在右側中間
只需要更改start move()函數中的數據
startmove(parseInt((document.documentElement.clientHeight - oDiv.offsetHeight )/2)+ scrollTop)//對聯懸浮
以上是“JS如何實現右側懸浮框效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。