<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0;padding: 0;list-style: none; } .green{ width: 100%; height: 150px; background: green; } .red { width: 100%; height: 50px; background: red; } body{ overflow: scroll; overflow-x: hidden; height: 3000px; /*position: fixed;*/ } </style> </head> <body> <div class="green"></div> <div class="red"></div> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> </ul> <script> window.onscroll=function(){ var red=document.getElementsByClassName("red")[0]; if(document.documentElement.scrollTop>150){ red.style.position="fixed"; red.style.top="0px"; } if(document.documentElement.scrollTop<150){ red.style.position=""; } } </script> </body> </html>
05-04
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交