Skip to content

update reactivity of menu on scroll #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update reactivity of menu on scroll
  • Loading branch information
Dhruvi16 committed Oct 6, 2020
commit d8db8544fb87bdd976dc22957bb434c6abdde38e
74 changes: 56 additions & 18 deletions webpack/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,81 @@ $(document).ready(function () {
patchAssetIntoDom('/assets/logos/products/open_source.svg');

// Check for click events on the navbar burger icon
$(".navbar-burger").click(function() {
$(".navbar-burger").click(function () {
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
$(".navbar-burger").toggleClass("is-active");
$(".navbar-menu").toggleClass("is-active");
});

$(".navbar-item.has-dropdown").click(function() {
$(".navbar-item.has-dropdown").click(function () {
if ($(".navbar-burger").is(':visible')) {
$(this).toggleClass("is-active");
}
});
$(".navbar-item > .navbar-link").click(function(e) {
$(".navbar-item > .navbar-link").click(function (e) {
if ($(".navbar-burger").is(':visible')) {
e.preventDefault();
}
});
$(window).resize(function() {
$(window).resize(function () {
if (!$(".navbar-burger").is(':visible') && $(".navbar-item.has-dropdown.is-active").length) {
$(".navbar-item.has-dropdown.is-active").removeClass('is-active');
}
});

// Internal Navigation for table of contents and table of progress component
let hash = window.location.hash;
if (hash.length > 0) {
$('.menu-list').find('a[href=\"' + hash + '\"]').addClass('is-active');
$('.step').find('a[href=\"' + hash + '\"]').find('.number').addClass('is-active');
}
$(window).scroll(function () {
// Get container scroll position
var fromTop = $(this).scrollTop() + topMenuHeight;

// Get id of current scroll item
var cur = scrollItems.map(function () {
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length - 1];
var id = cur && cur.length ? cur[0].id : "";

console.log(window.location.pathname);
if (lastId !== id) {
lastId = id;
// Set/remove active class
id = '#' + id;
$('.menu-list a[href*="#"]').closest('a').removeClass('is-active');
$('.menu-list').find('a[href=\"' + id + '\"]').addClass("is-active");
$('.step a[href*="#"]').closest('a').find('.number').removeClass('is-active');
$('.step').find('a[href=\"' + id + '\"]').find('.number').addClass('is-active');
}
});

// Cache selectors
var lastId

if(window.location.pathname == '/cc-search/contribution-guide/')
var topMenu = $(".step")
else
topMenu = $(".menu-list")

$(window).on('hashchange', function() {
let hash = window.location.hash;
$('.menu-list a[href*="#"]').closest('a').removeClass('is-active');
$('.menu-list').find('a[href=\"' + hash + '\"]').addClass('is-active');
var topMenuHeight = topMenu.outerHeight() + 1,
// All list items
menuItems = topMenu.find('a[href*="#"]'),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function () {
var item = $($(this).attr("href"));
if (item.length) {
return item;
}
});

$('.step a[href*="#"]').closest('a').find('.number').removeClass('is-active');
$('.step').find('a[href=\"' + hash + '\"]').find('.number').addClass('is-active');
// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function (e) {
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top - topMenuHeight + 1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 850);
e.preventDefault();
});
});

Expand All @@ -70,5 +109,4 @@ const patchAssetIntoDom = (asset, version = null) => {
}

ajax.send();
}

}