How do I link my javascript to this html to work -



How do I link my javascript to this html to work -

i new coding , working on website , can't javascript work. javascript used create transitions between different sections smooth , nice. awesome if guys help me. know problem simple please help out noobie in need. :)

html:

<html lang="en"> <head> <link rel="stylesheet" type="text/css" href="style.css"> <title><!-- insert title here --></title> </head> <body> <nav> <ul> <li><a href="#1">first</a></li> <li><a href="#2">second</a></li> <li><a href="#3">third</a></li> <li><a href="#4">fourth</a></li> <li><a href="#5">fifth</a></li> </ul> </nav> <div class="sections"> <section id="1"><h1>first</h1></section> <section id="2"><h1>second</h1></section> <section id="3"><h1>third</h1></section> <section id="4"><h1>fourth</h1></section> <section id="5"><h1>fifth</h1></section> </div> <footer></footer> <script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"> </script> </body> </html>

javascript:

var sections = $('section') , nav = $('nav') , nav_height = nav.outerheight(); $(window).on('scroll', function () { var cur_pos = $(this).scrolltop(); sections.each(function() { var top = $(this).offset().top - nav_height, bottom = top + $(this).outerheight(); if (cur_pos >= top && cur_pos <= bottom) { nav.find('a').removeclass('active'); sections.removeclass('active'); $(this).addclass('active'); nav.find('a[href="#'+$(this).attr('id')+'"]').addclass('active'); } }); }); nav.find('a').on('click', function () { var $el = $(this) , id = $el.attr('href'); $('html, body').animate({ scrolltop: $(id).offset().top - nav_height }, 500); homecoming false; });

use <script></script> tags. set js code between these scripts on html page. shall work then.

another clean approach utilize create separate file of javascript. name illustration hello.js. in <head> tag of html, set line link javascript file.

<script src="hello.js"></script>

javascript html

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -