Difference between revisions of "Template:Linkoping Sweden/Javascript"

Line 1: Line 1:
$(document).ready(function() {
+
$(document).ready(function () {
     $('.team-container').find('img').click(function() {
+
     $('.team-container').find('img').click(function () {
 
         var $imgHost = $(this).parent();
 
         var $imgHost = $(this).parent();
 
         var $container = $imgHost.parent();
 
         var $container = $imgHost.parent();
Line 16: Line 16:
 
     });
 
     });
  
     $('.info-close').click(function() {
+
     $('.info-close').click(function () {
 
         var $container = $(this).parent().parent();
 
         var $container = $(this).parent().parent();
  
Line 24: Line 24:
 
     });
 
     });
  
     $('.menu-button').click(function() {
+
     $('.menu-button').click(function () {
 
         $('.closed-menu').toggle();
 
         $('.closed-menu').toggle();
 
         window.scrollTo(0, 0);
 
         window.scrollTo(0, 0);
 
     });
 
     });
 +
 +
    if ($('#toc').length > 0) {
 +
        $('.content').addClass('toc-exist');
 +
 +
        var element = $('#toctitle');
 +
 +
        var $tableItems = $('#toc').find('li');
 +
 +
        var $linkElems = [];
 +
        var triggerHeights = [];
 +
 +
        for (var i = 0; i < $tableItems.length; i++) {
 +
            var $elem = $($tableItems[i]);
 +
            var $linkElem = $elem.find('> a');
 +
            var $headingElem = $($linkElem.attr('href'));
 +
 +
            var elemTop = $headingElem.offset().top;
 +
            triggerHeights.push(elemTop - 200);
 +
            $linkElems.push($linkElem);
 +
        }
 +
 +
        var oldSelectedIndex = -1;
 +
        var electNewIndex = function () {
 +
            var scrollTop = $(window).scrollTop();
 +
            var newIndex = 0;
 +
            for (var i = 0; i < triggerHeights.length; i++) {
 +
                if (scrollTop > triggerHeights[i]) {
 +
                    newIndex = i;
 +
                }
 +
            }
 +
 +
            if (newIndex != oldSelectedIndex) {
 +
                if (oldSelectedIndex != -1) {
 +
                    $linkElems[oldSelectedIndex].removeClass('toc-selected');
 +
                }
 +
 +
                oldSelectedIndex = newIndex;
 +
                $linkElems[newIndex].addClass('toc-selected');
 +
            }
 +
        }
 +
        electNewIndex();
 +
 +
        $(window).scroll(function () {
 +
            electNewIndex();
 +
        });
 +
 +
        var eTop = element.offset().top;
 +
        $(window).scroll(function () { //when window is scrolled
 +
            var positionOnScreen = eTop - $(window).scrollTop();
 +
            if (positionOnScreen < 40) {
 +
                $('#toc').addClass('toc-fixed');
 +
            } else {
 +
                $('#toc').removeClass('toc-fixed');
 +
            }
 +
        });
 +
    }
 
});
 
});

Revision as of 21:16, 12 October 2016

$(document).ready(function () {

   $('.team-container').find('img').click(function () {
       var $imgHost = $(this).parent();
       var $container = $imgHost.parent();
       // Show details interface
       $imgHost.toggleClass('active');
       $container.toggleClass('displaying');
       // Copy information content for the selected team member
       var memberName = $imgHost.attr('data-member');
       var $memberInfo = $('#member-info').find('#member-' + memberName);
       var $memberContainer = $container.find('.member-info');
       $memberContainer.html($memberInfo.html());
   });
   $('.info-close').click(function () {
       var $container = $(this).parent().parent();
       // Close details interface
       $container.removeClass('displaying');
       $container.find('.active').removeClass('active');
   });
   $('.menu-button').click(function () {
       $('.closed-menu').toggle();
       window.scrollTo(0, 0);
   });
   if ($('#toc').length > 0) {
       $('.content').addClass('toc-exist');
       var element = $('#toctitle');
       var $tableItems = $('#toc').find('li');
       var $linkElems = [];
       var triggerHeights = [];
       for (var i = 0; i < $tableItems.length; i++) {
           var $elem = $($tableItems[i]);
           var $linkElem = $elem.find('> a');
           var $headingElem = $($linkElem.attr('href'));
           var elemTop = $headingElem.offset().top;
           triggerHeights.push(elemTop - 200);
           $linkElems.push($linkElem);
       }
       var oldSelectedIndex = -1;
       var electNewIndex = function () {
           var scrollTop = $(window).scrollTop();
           var newIndex = 0;
           for (var i = 0; i < triggerHeights.length; i++) {
               if (scrollTop > triggerHeights[i]) {
                   newIndex = i;
               }
           }
           if (newIndex != oldSelectedIndex) {
               if (oldSelectedIndex != -1) {
                   $linkElems[oldSelectedIndex].removeClass('toc-selected');
               }
               oldSelectedIndex = newIndex;
               $linkElems[newIndex].addClass('toc-selected');
           }
       }
       electNewIndex();
       $(window).scroll(function () {
           electNewIndex();
       });
       var eTop = element.offset().top;
       $(window).scroll(function () { //when window is scrolled
           var positionOnScreen = eTop - $(window).scrollTop();
           if (positionOnScreen < 40) {
               $('#toc').addClass('toc-fixed');
           } else {
               $('#toc').removeClass('toc-fixed');
           }
       });
   }

});