Skip to content

Commit 76eb2db

Browse files
Merge pull request #157 from untrobotics/URW-176-botathon-nav-code-cleanup
Cleanup botathon/index.php navigation panel script
2 parents d7d7617 + e5648ed commit 76eb2db

File tree

1 file changed

+88
-63
lines changed

1 file changed

+88
-63
lines changed

botathon/index.php

Lines changed: 88 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@
9595

9696
.robot-kit-row {
9797
}
98+
9899
.robot-kit-entry > div {
99100
height: 100%;
100101
border: 1px dotted black;
101102
margin: 2px;
102103
}
104+
103105
.robot-kit-entry h4 {
104106
text-align: center;
105107
}
108+
106109
.robot-kit-list li {
107110
background-color: #e1e1e1;
108111
padding: 5px;
@@ -591,68 +594,19 @@ class="rd-parallax-layer section-top-75 section-md-top-150 section-lg-top-260">
591594
footer(false);
592595
?>
593596
<script>
594-
// var currentNav = "#nav-info"
595-
// var currentAnchor = '#info'
596-
var anchors = ['info','register','gameplay','schedule','parts-list','teams','sponsors','contacts']
597-
var curIndex = 0
598-
var lastScrollTop = $(window).scrollTop()
599-
const downTolerance = 0.25
600-
const upTolerance = 0.5
601-
602-
$(window).scroll(function(){
603-
windoo = $(this)
604-
let currentScrollTop = windoo.scrollTop()
605-
let windowHeight = windoo.height()
606-
let scrollBottom = currentScrollTop + windowHeight
607-
function elementInView(elem)
608-
{
609-
let elemTop = $(elem).offset().top + parseInt($(elem).css('padding-top'),10)
610-
var elemBottom = elemTop + $(elem).height()
611-
return (elemTop <= currentScrollTop && elemBottom >= currentScrollTop) || (elemTop >= currentScrollTop && elemTop <= scrollBottom) || (elemBottom >= currentScrollTop && elemBottom <= scrollBottom)
612-
}
613-
function getTop(e){return $(e).offset().top + parseInt($(e).css('padding-top'),10)}
614-
function getBottom(e) {return getTop(e)+ $(e).height()}
615-
function switchActive(e,i){
616-
$('#nav-'.concat(anchors[curIndex])).removeClass('active')
617-
$('#nav-'.concat(e)).addClass('active')
618-
curIndex = i
619-
}
620-
621-
if(currentScrollTop > lastScrollTop){ // scrolled down
622-
if(curIndex!==6 && Math.floor(getBottom('footer'))<= scrollBottom)
623-
{
624-
switchActive('contacts',6)
625-
lastScrollTop = currentScrollTop
626-
return
627-
}
628-
if(getBottom('#'.concat(anchors[curIndex]))<= currentScrollTop +windowHeight*downTolerance){
629-
if(curIndex===6) {
630-
lastScrollTop = currentScrollTop
631-
return
632-
}
633-
for(let i = curIndex+1;i<7;i++){
634-
if(elementInView('#'.concat(anchors[i]))){
635-
switchActive(anchors[i],i)
636-
break
637-
}
638-
}
639-
}
640-
} else{ // scrolled up
641-
if(getTop('#'.concat(anchors[curIndex]))>=currentScrollTop+windowHeight*(1-upTolerance)){
642-
if(curIndex===0) {
643-
lastScrollTop = currentScrollTop
644-
return
645-
}
646-
for(let i = curIndex-1;i>=0;i--){
647-
if (elementInView('#'.concat(anchors[i]))) {
648-
switchActive(anchors[i], i)
649-
break
650-
}
651-
}
652-
}
653-
}
654-
lastScrollTop = currentScrollTop
655-
});
597+
// these should be ordered top to bottom in the page layout, hopefully
598+
const navLinks = $('.botathon-navigation li').toArray()
599+
let pageAnchors = []
600+
navLinks.forEach((element)=>{
601+
pageAnchors.push($(element.id.replace('nav-','#')))
602+
})
603+
604+
let footer = $('footer')
605+
let _window = $(window)
606+
let lastScrollTop = _window.scrollTop()
607+
const downTolerance = 0.25
608+
const upTolerance = 0.5
609+
$(window).scroll(navChange);
656610

657611
$(document).ready(function () {
658612
// Add smooth scrolling to all links
@@ -671,11 +625,82 @@ function switchActive(e,i){
671625
$('html, body').animate({
672626
scrollTop: $(hash).offset().top
673627
}, 800, function () {
674-
675628
// Add hash (#) to URL when done scrolling (default click behavior)
676629
window.location.hash = hash;
677630
});
678631
} // End if
679632
});
633+
let scrollTop = _window.scrollTop()
634+
let scrollBottom = scrollTop + _window.height()
635+
for (let i = 0; i < pageAnchors.length; i++) {
636+
let anchor = pageAnchors[i];
637+
if (elementInView(anchor, scrollTop, scrollBottom)) {
638+
switchActive(i, 0)
639+
break
640+
}
641+
}
680642
});
643+
644+
function navChange() {
645+
let currentScrollTop = _window.scrollTop()
646+
let windowHeight = _window.height()
647+
let scrollBottom = currentScrollTop + windowHeight
648+
649+
function getTop(elem) {
650+
return elem.offset().top + parseInt(elem.css('padding-top'), 10)
651+
}
652+
653+
function getBottom(elem) {
654+
return getTop(elem) + elem.height()
655+
}
656+
657+
if (currentScrollTop > lastScrollTop) { // scrolled down
658+
for (let i = 0; i < pageAnchors.length; i++){
659+
if(navLinks[i].classList.contains('active')){
660+
if(getBottom(pageAnchors[i])<= currentScrollTop +windowHeight*downTolerance) { // checks if the scroll caused the active section to leave
661+
if (Math.floor(getBottom(footer)) <= scrollBottom) { // sets last link as active if bottom of footer is scrolled into view
662+
switchActive(navLinks.length-1, i)
663+
}
664+
else {
665+
for (let j = i; j < pageAnchors.length; j++) { // find the first section that's in view
666+
if (elementInView(pageAnchors[j], currentScrollTop, scrollBottom)){
667+
switchActive(j, i)
668+
break
669+
}
670+
}
671+
}
672+
}
673+
break;
674+
}
675+
}
676+
} else { // scrolled up
677+
// for loops iterate backwards since upward scroll can only bring in previous elements
678+
for(let i = pageAnchors.length-1; i >= 0 ;i--){
679+
if(navLinks[i].classList.contains('active')){
680+
681+
if (getTop(pageAnchors[i]) >= currentScrollTop + windowHeight * (1 - upTolerance)) { // if the scroll caused the active section to leave
682+
for(let j = i; j >= 0; j--){
683+
if (elementInView(pageAnchors[j], currentScrollTop, scrollBottom)) {
684+
switchActive(j,i);
685+
break
686+
}
687+
}
688+
}
689+
break;
690+
}
691+
}
692+
}
693+
lastScrollTop = currentScrollTop
694+
}
695+
696+
function elementInView(elem, currentScrollTop, scrollBottom) {
697+
let elemTop = elem.offset().top + parseInt(elem.css('padding-top'), 10)
698+
let elemBottom = elemTop + elem.height()
699+
return (elemTop <= currentScrollTop && elemBottom >= currentScrollTop) || (elemTop >= currentScrollTop && elemTop <= scrollBottom) || (elemBottom >= currentScrollTop && elemBottom <= scrollBottom)
700+
}
701+
702+
function switchActive(newIndex, oldIndex) {
703+
navLinks[oldIndex].classList.remove('active')
704+
navLinks[newIndex].classList.add('active')
705+
}
681706
</script>

0 commit comments

Comments
 (0)