<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0px; padding: 0px; } div:first-child { width: 100%; height: 200px; background: pink; } div:nth-child(2) { width: 100%; height: 400px; background: green; } div:nth-child(3) { width: 100%; height: 400px; background: blue; } div:nth-child(4) { width: 100%; height: 200px; background: red; } .box { background: peru; width: 20px; height: 50px; position: absolute; top: 200px; right: 0px; } .jay { position: relative; } </style> </head> <body> <div class="jay"> <div> 我是head </div> <div> 我是banner </div> <div> 我是Body </div> <div> 我是footer </div> <div class="box"> </div> </div> <script> var box = document.querySelector('.box') window.onscroll = function () { // console.log(document.documentElement.scrollTop); var flag = window.pageYOffset console.log(flag); if (flag >= 200) { box.style.position = 'fixed' console.log(111); box.style.top = '0px' } else { box.style.position = 'absolute' box.style.top = '200px' } } </script> </body> </html>