Blogger Information
Blog 47
fans 0
comment 2
visits 102521
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS缓冲运动案例:右侧居中悬浮窗
拾一枝樱花的博客
Original
391 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JS小案例:右侧缓冲悬浮框</title>
  6. <style>
  7. body {
  8. height: 2000px;
  9. margin: 0px;
  10. }
  11. #div1 {
  12. width: 100px;
  13. height: 150px;
  14. background: red;
  15. position: absolute;
  16. right: 0;
  17. bottom:calc(50% - 75px);
  18. }
  19. </style>
  20. <script>
  21. window.onscroll = function () {
  22. startMove();
  23. }
  24. var timer = null;
  25. function startMove() {
  26. var oDiv = document.getElementById('div1');
  27. clearInterval(timer);
  28. timer = setInterval(function () {
  29. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  30. var iTarget = (document.documentElement.clientHeight - oDiv.offsetHeight) / 2 + scrollTop;
  31. iTarget = Math.ceil(iTarget);
  32. var speed = (iTarget - oDiv.offsetTop) / 4;
  33. speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
  34. if (oDiv.offsetTop == iTarget) {
  35. clearInterval(timer);
  36. } else {
  37. oDiv.style.top = oDiv.offsetTop + speed + 'px';
  38. }
  39. }, 30)
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <div id='div1'></div>
  45. </body>
  46. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post