Guest User

Untitled

a guest
Aug 19th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //other math stuff
  3. function rangeFinder(x, min, max, margin) {
  4.     return (x > (min - margin) && x <= (max - margin));
  5. }
  6.  
  7.  
  8.  
  9. //******************************************************************************************************
  10. //finds section by range in heights
  11. function setSection() {
  12.     var freshRun = true;
  13.     var sections = [
  14.         'section#content>section#heim',
  15.         'section#content>section#tilbud',
  16.         'section#content>section#galleri',
  17.         'section#content>section#feedback'
  18.     ];
  19.     var sectionsID = [
  20.         '#heim',
  21.         '#tilbud',
  22.         '#galleri',
  23.         '#feedback'
  24.     ];
  25.    
  26.     var lastSection = 'body>section#content>section#heim';
  27.     var obj = new Object()
  28.  
  29.     //desides the section
  30.     function getCurrentSection(o, m, offsets) {
  31.         var section = 0;
  32.         var margin = m;
  33.  
  34.         if (rangeFinder(o, 0, offsets[1], margin)) { section = 0; }
  35.         else if (rangeFinder(o, offsets[1], offsets[2], margin)) { section = 1; }
  36.         else if (rangeFinder(o, offsets[2], offsets[3], margin)) { section = 2; }
  37.         else { section = 3; }
  38.  
  39.         return section;
  40.     }
  41.  
  42.     //set and removes attributes from relevant <li>
  43.     function setAttributes(a) {
  44.         $('nav#main>ul.positionLinks>li#current').removeAttr('id');
  45.         $('nav#main>ul.positionLinks>li[data-gotoSection="' + sectionsID[a] + '"]').attr('id', 'current');
  46.         return false;
  47.     }
  48.  
  49.     //fade out inactive sections
  50.     function fadeUnseenSections() {
  51.  
  52.         for (var i = 0; i < sections.length; i++) {
  53.             if (sectionArray[i] != section) $(sectionArray[i]).fadeOut(250);
  54.         }
  55.  
  56.         return false;
  57.     }
  58.  
  59.     //desides if the right functions should be called
  60.     function construct(topOffset,offsets) {
  61.         var menuHeight = $('nav#main').height();
  62.         var offset = $('nav#main').offset().top + menuHeight;
  63.         if (topOffset != offset || (topOffset <= menuHeight+2 && freshRun == true) || freshRun == true) {
  64.             freshRun = false;
  65.             var sectionKey = getCurrentSection(offset, 1, offsets);//offset, margin, offsets
  66.             setAttributes(sectionKey);
  67.  
  68.  
  69.             $('#tull').html('x: ' + offset + '<br>margin: 3<br>min: ' + 0 + '<br>max: ' + $('section#tilbud').offset().top + '<br>section: ' + sectionsID[sectionKey]);
  70.             lastSection = sectionsID[sectionKey];//sets the last section
  71.         }
  72.     }
  73.  
  74.     //return object so inner functions can be accessed
  75.     obj.construct = construct;
  76.     return obj;
  77. }
  78.  
  79.  
  80. /********************************************************************/
  81. //starter alt
  82.  
  83. function offsetsFunc(){
  84. return [$('body>section#content>section#heim').offset().top,
  85.     $('body>section#content>section#tilbud').offset().top,
  86.     $('body>section#content>section#galleri').offset().top,
  87.     $('body>section#content>section#feedback').offset().top];
  88. }
  89. var topOffset=$('nav#main').offset().top+$('nav#main').height();
  90. var offsets = offsetsFunc();
  91.  
  92. var section = new setSection();
  93. window.setInterval(function(){section.construct(topOffset,offsets);},50);
Advertisement
Add Comment
Please, Sign In to add comment