Skip to content

Instantly share code, notes, and snippets.

@levnhub
Last active January 9, 2018 12:41
Show Gist options
  • Save levnhub/cbd8a4e911b1e637f41924fd642d49c8 to your computer and use it in GitHub Desktop.
Save levnhub/cbd8a4e911b1e637f41924fd642d49c8 to your computer and use it in GitHub Desktop.
jQuery Snippets
// Masked Input
jQuery(function($){
$('input[type="tel"]').mask("+7 (999) 999–99–99");
});
// Fixed Nav
$(window).scroll(function(){
var nav = $('#Nav'),
fix = $('#NavFix'),
scroll = $(window).scrollTop(),
width = $('body').width();
if (scroll >= 265 && width >= 992) {
nav.addClass('fixed');
fix.addClass('active');
} else {
nav.removeClass('fixed');
fix.removeClass('active');
}
});
// Smooth Anchor Scroll
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top - 50
}, 350, function() {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
});
// Scroll to Top
$('#scroll2top').on('click', function () {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
// jQuery Media Queries
$(window).on('resize orientationChange', function(event) {
//check window.width()...
if($(window).width() > 992) {
// lg content...
} else {
// sm content...
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment