Skip to content

Instantly share code, notes, and snippets.

@fanian
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanian/a8cda136ed16cf38d5f3 to your computer and use it in GitHub Desktop.
Save fanian/a8cda136ed16cf38d5f3 to your computer and use it in GitHub Desktop.
//fix menu
$(window).scroll(function(){
var js_scroll = $(window).scrollTop();
var SCROLL_MIN = 820;
var SCROLL_MAX = 5480;
if ( js_scroll > SCROLL_MIN ) {
$(".js-top-nav").addClass('fixed');
if(js_scroll > SCROLL_MAX) {
$(".js-top-nav").removeClass('fixed');
};
}
else {
$(".js-top-nav").removeClass('fixed');
};
});
//scroll to
$(".navbar a, .js-btn-top").click(function (){
var page = $(this).attr("href");
$('html, body').animate({
scrollTop: $(page).offset().top + 20
}, 600);
return false;
});
// highlight active link when scrolling
$(function(){
var sections = {},
_height = $(window).height(),
i = 0;
// Grab positions of our sections
$('.section').each(function(){
sections[this.name] = $(this).offset().top;
});
$(document).scroll(function(){
var $this = $(this),
pos = $this.scrollTop();
for(i in sections){
if(sections[i] > pos && sections[i] < pos + _height){
$('a').removeClass('active');
$('#nav_' + i).addClass('active');
}
}
});
});
//yet another scrolling with hightlight effect (works for me)
//each marker link would be with section class
//.page is class of page without top menu )
// Navigation
$('.js-nav a').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top - 20
}, 500, 'swing', function () {
// window.location.hash = target;
});
});
function navScroll(){
$('.section').each(function(){
var pos = $(this).offset().top;
var id = $(this).attr('id');
var top = ($('.page').offset().top - 80);
if( $(window).scrollTop() >= (pos - 79)){
$('.js-nav li').removeClass('is-active');
$('[href = #'+id+']').parent().addClass('is-active');
}
if($(window).scrollTop() < top){
$('.js-nav li').removeClass('is-active');
}
});
}
$(window).scroll(function() {
navScroll();
});
////////==================================================
//another working method for sticky nav, scroll and hightligt links where scrolling
//.target - class where to scrolling
//=========================================================
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash, $target = $(target);
var offset = ($(window).scrollTop() <= 0) ? 120 : 40;
$('html, body').stop().animate({
'scrollTop': ($target.offset().top - offset)
}, 900, 'swing', function () {
window.location.hash = target;
});
});
var $anchors = $('.anchor');
var $nav = $('.top-nav');
var sticky = function() {
var scrollTop = $(window).scrollTop();
if (scrollTop > 0) {
$nav.addClass('stick');
} else {
$nav.removeClass('stick');
}
$nav.find('a').removeClass('active');
//Highlight the navigation item
var $nearest = null;
$anchors.each(function(index, item){
var $item = $(item);
var id = $item.prop('id');
if (($item.offset().top - 40) <= scrollTop) {
$nearest = $nav.find('a[href^="#'+ id +'"]');
}
});
if ($nearest !== null) {
$nearest.addClass('active');
}
}
$(window).scroll(sticky);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment