I am new to JavaScript, and trying to ensure the i write the best possible code instead of "copy & paste".
I hope i am asking correctly, want to know if my code I have written is done correctly or could it be done better, the code works, so nothing wrong with the end result, just not sure if I'm on the right track.
$(document).ready(function() {
"use strict";
var menu_btn = $('a.menu-trigger');
var menu = $('#main_nav > ul');
var w = $(window).width();
// Adding the ScrollTo Function on the menu items
$('#main_nav > ul a[href^="#"],.logo a[href^="#"]').on('click', function(e) {
var target = $( $(this).attr('href') );
if(target.length ) {
e.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
}
if (w < 768) {
$('#main_nav > ul').css('display', 'none');
}
});
// Create the Sticky Header
$(window).scroll(function() {
if ($(this).scrollTop() > 1){
$('.header-inner').addClass("sticky");
}else{
$('.header-inner').removeClass("sticky");
}
});
// Mobile Menu Functionality
menu_btn.on('click', function(e) {
e.preventDefault();
menu.slideToggle ();
});
$(window).resize(function () {
if (w > 768 && menu.is(':hidden')) {
menu.removeAttr('style');
}
});
});