Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. <div id="screen_1">
  2. <h1 style="color:white">sample text
  3. </h1>
  4. </div>
  5. <div id="sticky-anchor">
  6. </div>
  7. <div id="screen_2">
  8. <h1 style="color:white">sample text
  9. </h1>
  10. </div>
  11. <div id="screen_3">
  12. <h1 style="color:white">sample text
  13. </h1>
  14. </div>
  15.  
  16. h1{ position:absolute;
  17. top:50%;
  18. transform: translateY(-50%)}
  19.  
  20. #screen_1{
  21. width:100%;
  22. height:100vh;
  23. background-color:red;
  24. padding:0px !important;
  25. margin:0px !important;
  26. position:relative;
  27. z-index:-1;
  28. }
  29. #screen_2{
  30. width:100%;
  31. height:100vh;
  32. background-color:black;
  33. padding:0px;
  34. margin:0px;
  35. position:relative;
  36. }
  37. #screen_2.stick{
  38. width:100%;
  39. height:100vh;
  40. background-color:black;
  41. padding:0px;
  42. margin:0px;
  43. position:fixed;
  44. top:0px;
  45. z-index:-1;
  46. }
  47. #screen_3{
  48. width:100%;
  49. height:100vh;
  50. background-color:green;
  51. padding:0px;
  52. margin:0px;
  53. position:relative;
  54. }
  55.  
  56. function sticky_relocate() {
  57. var window_top = $(window).scrollTop();
  58. var div_top = $('#sticky-anchor').offset().top;
  59. console.log(div_top);
  60. console.log(window_top);
  61. if (window_top > div_top) {
  62. $('#screen_2').addClass('stick');
  63. $('#sticky-anchor').height($('#screen_2').outerHeight());
  64.  
  65. } else {
  66. $('#screen_2').removeClass('stick');
  67. $('#sticky-anchor').height(0);
  68. }
  69. };
  70.  
  71. $(function() {
  72. $(window).scroll(sticky_relocate);
  73. sticky_relocate();
  74.  
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement