Advertisement
BratokHR

Untitled

Jun 16th, 2020
1,728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* CSS */
  2. #navigation-menu {
  3.             position: fixed;
  4.             left: 100px;
  5.             top: 100px;
  6.             width: 300px;
  7.         }
  8.  
  9.         .header-menu .title {
  10.             background: #eee8db;
  11.             text-indent: 10px;
  12.         }
  13.  
  14.         .header-menu .subheader-menu {
  15.             text-indent: 20px;
  16.             font-size: 0.8em;
  17.         }
  18.  
  19.         .header-menu .title:hover,
  20.         .header-menu .subheader-menu:hover {
  21.             text-decoration: underline;
  22.             cursor: pointer;
  23.         }
  24.  
  25.         .header-menu #active {
  26.             color: red;
  27.         }  
  28. /* HTML */
  29. <div id="navigation-menu">
  30.             <div class="header-menu">
  31.                 <div class="title" id='active'>Заголовок 1</div>
  32.                 <div class="subheader-menu" id='active'>Подпункт 1</div>
  33.                 <div class="subheader-menu">Подпункт 2</div>
  34.                 <div class="subheader-menu">Подпункт 3</div>
  35.                 <div class="subheader-menu">Подпункт 4</div>
  36.             </div>
  37.             <div class="header-menu">
  38.                 <div class="title">Заголовок 2</div>
  39.                 <div class="subheader-menu">Подпункт 1</div>
  40.                 <div class="subheader-menu">Подпункт 2</div>
  41.             </div>
  42.             <div class="header-menu">
  43.                 <div class="title">Заголовок 4</div>
  44.                 <div class="subheader-menu">Подпункт 1</div>
  45.                 <div class="subheader-menu">Подпункт 2</div>
  46.                 <div class="subheader-menu">Подпункт 3</div>
  47.                 <div class="subheader-menu">Подпункт 4</div>
  48.                 <div class="subheader-menu">Подпункт 5</div>
  49.                 <div class="subheader-menu">Подпункт 6</div>
  50.                 <div class="subheader-menu">Подпункт 7</div>
  51.             </div>
  52.         </div>
  53.  
  54. /* JAVASCRIPT */
  55. $(function() {
  56.  
  57.     /*$(".accordion dd").hide().prev().click(function() {
  58.         $(".accordion dd").not(this).slideUp();
  59.         $(this).next().not(":visible").slideDown();
  60.         $(".accordion .c-menu-acc").slideUp();
  61.     });
  62.    
  63.     $(".accordion .c-menu-acc").hide().prev().click(function() {
  64.         $(".accordion .c-menu-acc").not(this).slideUp();
  65.         $(this).next().not(":visible").slideDown();
  66.     });*/
  67.  
  68. })
  69.  
  70. /*$(".h-menu-acc").each( function() {
  71.     console.log( this.innerHTML );
  72. } );*/
  73.  
  74. let headers = [];
  75.  
  76. $(".accordion dl").each( function() {
  77.     let header = this.getElementsByTagName('dt')[0];
  78.     let subheader = this.getElementsByClassName('h-menu-acc');
  79.  
  80.     let new_header = {
  81.         'title': header,
  82.         'subheader': subheader
  83.     };
  84.     headers.push( new_header );
  85. } );
  86.  
  87. let $window = $(window);
  88. $window.on('load scroll', function() {
  89.     let top = $window.scrollTop();
  90.     let height = $window.height();
  91.     let y = top + height / 2;
  92.  
  93.     let found_i = -1;
  94.     let found_k = -1;
  95.  
  96.     for ( let i = 0; i < headers.length; i++ )
  97.     {
  98.         if ( y >= headers[i]['title'].offsetTop )
  99.         {
  100.             found_i = i;
  101.             found_k = -1;
  102.         }
  103.  
  104.         for ( let k = 0; k < headers[i]['subheader'].length; k++ )
  105.         {
  106.             if ( y >= headers[i]['subheader'][k].offsetTop )
  107.                 found_k = k;
  108.         }
  109.     }
  110.  
  111.     if ( found_i == -1 )
  112.         found_i = 0;
  113.  
  114.     console.log( found_i, found_k );
  115.     select_tab( found_i, found_k );
  116.     update_navmenu();
  117. });
  118.  
  119. update_navmenu();
  120.  
  121. function select_tab( h, s )
  122. {
  123.     for ( let i = 0; i < headers.length; i++ )
  124.     {
  125.         headers[i]['active'] = false;
  126.         headers[i]['active_sub'] = -1;
  127.  
  128.         if ( i == h && s == -1 )
  129.             headers[i]['active'] = true;
  130.         else if ( i == h )
  131.             headers[i]['active_sub'] = s;
  132.     }
  133. }
  134.  
  135. function navmenu_to( h, s ) {
  136.     if ( s < 0 )
  137.         headers[h]['title'].scrollIntoView( { block: "center", inline: "center", behavior: 'smooth' } );
  138.     else
  139.         headers[h]['subheader'][s].scrollIntoView( { block: "center", inline: "center", behavior: 'smooth' } );
  140. }
  141.  
  142. function update_navmenu()
  143. {
  144.     let navmenu = document.getElementById('navigation-menu');
  145.     let html = '';
  146.  
  147.     for ( let i = 0; i < headers.length; i++ )
  148.     {
  149.         html += '<div class="header-menu"><div class="title" onclick="navmenu_to(' + i + ',-1)"';
  150.        
  151.         if ( headers[i]['active'] )
  152.             html += 'id="active"'
  153.            
  154.         html += '>' + headers[i]['title'].innerHTML + '</div>'
  155.  
  156.         for ( let k = 0; k < headers[i]['subheader'].length; k++ )
  157.         {
  158.             html += '<div class="subheader-menu" onclick="navmenu_to(' + i + ', ' + k + ')"';
  159.            
  160.             if ( headers[i]['active_sub'] == k )
  161.                 html += 'id="active"';
  162.            
  163.             html += '>' + headers[i]['subheader'][k].innerHTML + '</div>';
  164.         }
  165.  
  166.         html += '</div>'
  167.     }
  168.     navmenu.innerHTML = html;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement