Advertisement
Guest User

Scroll

a guest
Jul 27th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  5. <title>HTML5, CSS3 and JavaScript demo</title>
  6. <style>
  7.  
  8.         html, body { margin: 0; padding: 0; width: 100%; height: 100%;}
  9.         header {
  10.             position: fixed;
  11.             top: 0;
  12.             width: 100%;
  13.             height: 80px;
  14.             background: #fff;
  15.         }
  16.         nav {
  17.             width: 960px;
  18.             height: 80px;
  19.             margin: 0 auto;
  20.         }
  21.         nav ul {
  22.             margin: 20px 0 0;
  23.         }
  24.         nav ul li {
  25.             display: inline-block;
  26.             margin: 0 30px 0 0;
  27.         }
  28.  
  29.         a { color: #4D4D4D;  font-family: sans-serif; text-transform: uppercase; text-decoration: none; line-height: 42px; }
  30.         .active { color: #2dbccb; }
  31.  
  32.         .content { width: 100%; height: 100%; }
  33.         .content > section { width: 100%; height: 100%; }
  34.  
  35.         #home { background: #2dbccb; }
  36.         #about { background: #f6c362; }
  37.         #services { background-color: #eb7e7f; }
  38.         #contact { background-color: #415c71; }
  39.  
  40. .lw { font-size: 60px; }
  41. </style>
  42. </head>
  43. <body>
  44. <!-- Start your code here -->
  45.  
  46. <header>
  47.         <nav>
  48.             <ul>
  49.                 <li><a class="active" href="#home">Home</a></li>
  50.                 <li><a href="#about">About</a></li>
  51.               <li><a href="http://blog.eduweb.pl/">Services</a></li>
  52.                 <li><a href="#contact">Contact</a></li>
  53.             </ul>
  54.         </nav>
  55. </header>
  56.   <div class="content">
  57.     <section id="home" style="height:400px">Section</section>
  58.         <section id="about" style="height:400px">Section</section>
  59.         <section id="services" style="height:400px">Section</section>
  60.         <section id="contact" style="height:400px">Section</section>
  61. </div>
  62.  
  63. <!-- End your code here -->
  64. </body>
  65. <script type="text/javascript">
  66.    
  67.     $(document).ready(function () {
  68.         $(document).on("scroll", onScroll);
  69.  
  70.         $('a[href^="#"]').on('click', function (e) {
  71.             e.preventDefault();
  72.             $(document).off("scroll");
  73.  
  74.             $('a').each(function () {
  75.                 $(this).removeClass('active');
  76.             })
  77.             $(this).addClass('active');
  78.  
  79.             var target = this.hash;
  80.             $target = $(target);
  81.             $('html, body').stop().animate({
  82.                 'scrollTop': $target.offset().top+2
  83.             }, 500, 'swing', function () {
  84.                 window.location.hash = target;
  85.                 $(document).on("scroll", onScroll);
  86.             });
  87.         });
  88.     });
  89.  
  90.     function onScroll(event){
  91.         var scrollPosition = $(document).scrollTop();
  92.         $('nav a[href^="#"]').each(function () {
  93.             var currentLink = $(this);
  94.             var refElement = $(currentLink.attr("href"));
  95.             if (refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
  96.                 $('nav ul li a').removeClass("active");
  97.                 currentLink.addClass("active");
  98.             }
  99.             else{
  100.                 currentLink.removeClass("active");
  101.             }
  102.         });
  103.     }
  104.  
  105. </script>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement