Advertisement
here2share

Limit a DIV to Certain Scroll Height

Mar 7th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML>
  2. <html>
  3. <style type="text/css">
  4.  
  5. #fixbot {
  6. bottom:0px;
  7. position: fixed;
  8. }
  9.  
  10. #fixright {
  11. right:0px;
  12. position: absolute;
  13. }
  14.  
  15. #fixtop {
  16. top:0px;
  17. position: fixed;
  18. }
  19.  
  20. </style>
  21.  
  22. <script>
  23.  
  24. var offset;
  25.  
  26. window.onscroll = function() {
  27.  
  28.  if (typeof(window.pageYOffset) == 'number') {
  29.   offset = window.pageYOffset;
  30.  }
  31.  else if (document.documentElement) {
  32.   offset = document.documentElement.scrollTop;
  33.  }
  34.  else if (document.body) {
  35.   offset = document.body.scrollTop;
  36.  }
  37.  if (offset > 170) {
  38.   document.getElementById('fixright').style.position = 'fixed';
  39.   document.getElementById('fixright').style.top = '170px';
  40.  }
  41.  else {
  42.   document.getElementById('fixright').style.position = 'absolute';
  43.   document.getElementById('fixright').style.top = '50%';
  44.  }
  45.  document.getElementById('notice').innerHTML = offset;
  46. }
  47.  
  48. </script>
  49.  
  50. <body bgcolor="#aabbcc" marginheight=0 marginwidth=0 leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 onload="window.scrollTo(0,0);">
  51.  
  52. <div id="fixbot" style="border:0px solid #333; width:100%; height:50px; background-color:#ddd; text-align:center; padding:0px; ">
  53. <p>Hello I am at the bottom!</p>
  54. </div>
  55.  
  56. <div id="fixright" style="border:0px; width:100px; height:200px; text-align:center; padding:0px; top: 50%; margin-top: -100px; background-color:#cba;">
  57. <table width=100% height=100%><tr><td valign=middle><p>Right!</p></td></tr></table>
  58. </div>
  59.  
  60. <div id="fixtop" style="border:0px solid #333; width:100%; height:70px; background-color:#ddd; text-align:center; padding:0px; "> <p>Hello I am at the top! <span id="notice"></span></p>
  61. </div>
  62.  
  63. <table width="95%" align=center><tr><td>
  64. <br>line 1<br>line 2<br>line 3<br>line 4<br>line 5<br>line 6<br>line 7<br>line 8<br>line 9<br>line 10 <br>line 11<br>line 12<br>line 13<br>line 14<br>line 15<br>line 16<br>line 17<br>line 18<br>line 19<br>line 20 <br>line 21<br>line 22<br>line 23<br>line 24<br>line 25<br>line 26<br>line 27<br>line 28<br>line 29<br>line 30 <br>line 31<br>line 32<br>line 33<br>line 34<br>line 35<br>line 36<br>line 37<br>line 38<br>line 39<br>line 40 <br>line 41<br>line 42<br>line 43<br>line 44<br>line 45<br>line 46<br>line 47<br>line 48<br>line 49<br>line 50 <br>line 51<br>line 52<br>line 53<br>line 54<br>line 55<br>line 56<br>line 57<br>line 58<br>line 59<br>line 60 <br>line 61<br>line 62<br>line 63<br>line 64<br>line 65<br>line 66<br>line 67<br>line 68<br>line 69<br>line 70<br>line 71<br>line 72<br>line 73<br>line 74<br>line 75<br>line 76<br>line 77<br>line 78<br>line 79<br>line 80 </td></tr></table>
  65.  
  66. <script>
  67. setTimeout(function() {window.scrollTo(0, 0);},100); // To make sure when the page is reloaded, the scroll is back to 0
  68. document.getElementById('fixright').style.top = '50%';
  69.  
  70. </script>
  71.  
  72. </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement