Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //other math stuff
- function rangeFinder(x, min, max, margin) {
- return (x > (min - margin) && x <= (max - margin));
- }
- //******************************************************************************************************
- //finds section by range in heights
- function setSection() {
- var freshRun = true;
- var sections = [
- 'section#content>section#heim',
- 'section#content>section#tilbud',
- 'section#content>section#galleri',
- 'section#content>section#feedback'
- ];
- var sectionsID = [
- '#heim',
- '#tilbud',
- '#galleri',
- '#feedback'
- ];
- var lastSection = 'body>section#content>section#heim';
- var obj = new Object()
- //desides the section
- function getCurrentSection(o, m, offsets) {
- var section = 0;
- var margin = m;
- if (rangeFinder(o, 0, offsets[1], margin)) { section = 0; }
- else if (rangeFinder(o, offsets[1], offsets[2], margin)) { section = 1; }
- else if (rangeFinder(o, offsets[2], offsets[3], margin)) { section = 2; }
- else { section = 3; }
- return section;
- }
- //set and removes attributes from relevant <li>
- function setAttributes(a) {
- $('nav#main>ul.positionLinks>li#current').removeAttr('id');
- $('nav#main>ul.positionLinks>li[data-gotoSection="' + sectionsID[a] + '"]').attr('id', 'current');
- return false;
- }
- //fade out inactive sections
- function fadeUnseenSections() {
- for (var i = 0; i < sections.length; i++) {
- if (sectionArray[i] != section) $(sectionArray[i]).fadeOut(250);
- }
- return false;
- }
- //desides if the right functions should be called
- function construct(topOffset,offsets) {
- var menuHeight = $('nav#main').height();
- var offset = $('nav#main').offset().top + menuHeight;
- if (topOffset != offset || (topOffset <= menuHeight+2 && freshRun == true) || freshRun == true) {
- freshRun = false;
- var sectionKey = getCurrentSection(offset, 1, offsets);//offset, margin, offsets
- setAttributes(sectionKey);
- $('#tull').html('x: ' + offset + '<br>margin: 3<br>min: ' + 0 + '<br>max: ' + $('section#tilbud').offset().top + '<br>section: ' + sectionsID[sectionKey]);
- lastSection = sectionsID[sectionKey];//sets the last section
- }
- }
- //return object so inner functions can be accessed
- obj.construct = construct;
- return obj;
- }
- /********************************************************************/
- //starter alt
- function offsetsFunc(){
- return [$('body>section#content>section#heim').offset().top,
- $('body>section#content>section#tilbud').offset().top,
- $('body>section#content>section#galleri').offset().top,
- $('body>section#content>section#feedback').offset().top];
- }
- var topOffset=$('nav#main').offset().top+$('nav#main').height();
- var offsets = offsetsFunc();
- var section = new setSection();
- window.setInterval(function(){section.construct(topOffset,offsets);},50);
Advertisement
Add Comment
Please, Sign In to add comment