Difference between revisions of "Template:TU Darmstadt/JS"

 
(4 intermediate revisions by the same user not shown)
Line 22: Line 22:
 
   });
 
   });
  
   for (var m of ul.getElementsByClassName('multiple')) {
+
   var mul = ul.getElementsByClassName('multiple');
 +
  for (var i = 0; i < mul.length; i++) {
 +
    var m = mul[i];
 
     m.addEventListener('click', function () {
 
     m.addEventListener('click', function () {
       for (var r of ul.getElementsByClassName('multiple')) {
+
       for (var j = 0; j < mul.length; j++) {
         r.classList.remove('active');
+
        var r = mul[j];
 +
         r.classList.remove('clicked');
 
       }
 
       }
  
       this.classList.add('active');
+
       this.classList.add('clicked');
 
     });
 
     });
 
   }
 
   }
Line 36: Line 39:
  
 
   var as = document.getElementById('toc').getElementsByTagName('a');
 
   var as = document.getElementById('toc').getElementsByTagName('a');
   for (var a of as) {
+
   for (var i = 0; i < as.length; i++) {
 +
    var a = as[i];
 
     a.addEventListener('click', function () {
 
     a.addEventListener('click', function () {
       for (var oa of as) {
+
       for (var j = 0; j < as.length; j++) {
 +
        var oa = as[j];
 
         oa.classList.remove('active');
 
         oa.classList.remove('active');
 
       }
 
       }
Line 46: Line 51:
 
   }
 
   }
  
 +
  // thanks to wikimedia replacing two ampersands with &amp;&amp;
 +
  function and(a, b) {
 +
    if (a == true) {
 +
      if (b == true) {
 +
        return true;
 +
      }
 +
    }
 +
    return false;
 +
  }
  
   document.addEventListener('scroll', function () {
+
   function setScrollyThing() {
 
     var activeA = null;
 
     var activeA = null;
     for (var a of as) {
+
    var nextIsActive = false;
 +
     for (var i = 0; i < as.length; i++) {
 +
      var a = as[i];
 
       a.classList.remove('active');
 
       a.classList.remove('active');
 +
 +
      if (nextIsActive || and(!a.parentElement.classList.contains('toclevel-1'), !activeA)) {
 +
        activeA = a;
 +
        nextIsActive = false;
 +
        continue;
 +
      }
  
 
       var id = a.href.replace(/.+?#/, '');
 
       var id = a.href.replace(/.+?#/, '');
Line 62: Line 84:
 
       var active = y < 200;
 
       var active = y < 200;
 
       if (active) {
 
       if (active) {
 +
        if (a.parentElement.classList.contains('toclevel-1')) nextIsActive = true;
 
         activeA = a;
 
         activeA = a;
 
       }
 
       }
 
     }
 
     }
  
    console.log(activeA);
 
 
     activeA.classList.add('active');
 
     activeA.classList.add('active');
   });
+
   }
 +
 
 +
  document.addEventListener('scroll', setScrollyThing);
 +
  setScrollyThing();
 
    
 
    
 
});
 
});
 
</script>
 
</script>
 
</html>
 
</html>

Latest revision as of 17:18, 17 October 2018