javascript equivalent of jQuery's animate -


first, here's code snippet:

$('a[href^="#"]').on('click', function(event) {      var target = $( $(this).attr('href') );      if( target.length ) {         event.preventdefault();         $('body').animate({             scrolltop: target.offset().top         }, 1000);     }  }); 

i attempting convert javascript (since not yet know in regards jquery).

and have reached far:

var x = document.getelementsbytagname('a'); (var = 0; < x.length; i++) {     x[i].addeventlistener('click', function (event) {          var target = document.getelementbyid(event.target.getattribute('href'));         if (target) {             event.preventdefault();             document.getelementsbytagname('body')[0].animate({                 scrolltop: target.getboundingclientrect().top             }, 1000);         }     }); } 

as might have noticed, having problem in section:

$('body').animate({     scrolltop: target.offset().top }, 1000); 

what javascript's alternative of animate? know there's setinterval wondering if there's simpler way achieve this?


Comments