Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- ( function( $ ) {
- $( document ).ready( function() {
- var homepageLinks = $( '#menu-main-menu-desktoptablet .homepage-item' ),
- lastId,
- topMenu = $( '#menu-main-menu-desktoptablet' ),
- topMenuHeight = topMenu.outerHeight() + 1,
- menuItems = topMenu.find( '.homepage-item a' ),
- scrollItems = menuItems.map( function() {
- var item = $($( this ).attr( 'href' ));
- if ( item.length ) { return item; }
- } );
- console.log(scrollItems );
- if ( $( 'body' ).hasClass( 'home' ) ) {
- homepageLinks.each( function() {
- var link = $( this ).find( 'a' );
- $( this ).removeClass( 'current-menu-item' );
- $( '#menu-main-menu-desktoptablet .home-item' ).addClass( 'current-menu-item' );
- link.click( function() {
- homepageLinks.removeClass( 'current-menu-item' );
- $( this ).parent().addClass( 'current-menu-item' );
- } )
- } );
- // Bind to scroll
- $( 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 : '';
- if ( lastId !== id ) {
- lastId = id;
- // Set/remove active class
- menuItems
- .parent().removeClass( 'current-menu-item' )
- .end().filter( '[href="#' + id + '"]' ).parent().addClass( 'current-menu-item' );
- }
- } );
- }
- } );
- } )( jQuery );
- </script>
Advertisement
Add Comment
Please, Sign In to add comment