Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cant navigate through sections #9

Open
DangerXbeart opened this issue Jul 4, 2022 · 13 comments
Open

I cant navigate through sections #9

DangerXbeart opened this issue Jul 4, 2022 · 13 comments

Comments

@DangerXbeart
Copy link

DangerXbeart commented Jul 4, 2022

HI guys. I have a problem that I cant navigate through pages with buttons.
I have checked my js file for thousand times and it still doesn't work.
Also I receive <TypeError: allSections.addEventListener is not a function> this error.
I would appreciate if some one help.

` //sections active class
allSections.addEventListener('click' , (e) => {
const id=e.target.dataset.id;
if(id){
//remove selected from the other btns
sectBtns.foreach((btn) =>{
btn.classlist.remove('active');
})
e.target.classlist.add('active');

		//hide other sections
		sections.foreach((section)=>{
			section.classlist.remove('active');
		})
		const element = document.getElementById(id);
		element.classlist.add('active');
		}
	});
`
@lalabrus1995
Copy link

Have you found an answer to this issue? I just found this tutorial and I stumbled on the same issue

@DangerXbeart
Copy link
Author

DangerXbeart commented Aug 5, 2022 via email

@lalabrus1995
Copy link

Will do... :) Thanks

@lalabrus1995
Copy link

Hi! if anyone's having an issue with this like I was... another viewer had answered this problem on the comment section for the video.
My issue was that I didn't have full understanding of how 'allSections' was supposed to register.
--const allSections = document.querySelectorAll('.main-content')[0]; --
once you change the allSections to the code above adding the [0] at the end the .addEventListener will work as intended.

@amarantak
Copy link

To me it worked to copy and paste the new js code in the project.

@BriInTech
Copy link

i had the same problem as well it didn't work for me adding the zeros. I just deleted everything and retyped the code fro the function page transitions () {

function PageTransitions(){
//Button click active class
for (let i = 0; i < sectBtn.length; i++){
sectBtn[i].addEventListener('click', function(){
let currentBtn = document.querySelectorAll('.active-btn');
currentBtn[0].className = currentBtn[0].className.replace('active-btn', '');
this.className += ' active-btn';
})
}
}

PageTransitions();

@Dyrits
Copy link
Contributor

Dyrits commented Jan 22, 2023

To me it worked to copy and paste the new js code in the project.

Yes, I recommend people to read the README.md on this page: https://github.com/Dyrits/PORTFOLIO/tree/main-fork-update
Some of the changes I've made here I've been added to the project, so it is not exactly the same as the video on YouTube.

The original code is available here: https://github.com/Maclinz/JS_CSS_PortfolioProject/tree/1f1c0205c0b3a8caa623c96402775974f39ab5cb

@hakunamatata97k
Copy link

@Dyrits could you figure out how to achieve full-page scroll? I liked your version and have been working on achieving that for hours and still no progress! Could you please give me some tips?
I have tried the overflow: scroll; but it didn't help :(

@Dyrits
Copy link
Contributor

Dyrits commented May 1, 2023

You want to scroll from a section to another ?

@DangerXbeart
Copy link
Author

DangerXbeart commented May 1, 2023 via email

@Dyrits
Copy link
Contributor

Dyrits commented May 1, 2023

The best way would be to set margin: 1px 0; or padding to the body or increase the min-height, in order to allow some scrolling. Then you add an even and check the window.scrollY to setup a change to the next section.

@Dyrits
Copy link
Contributor

Dyrits commented May 1, 2023

@Dyrits could you figure out how to achieve full-page scroll? I liked your version and have been working on achieving that for hours and still no progress! Could you please give me some tips? I have tried the overflow: scroll; but it didn't help :(

(function () {
    [...document.querySelectorAll(".control")].forEach(button => {
        button.addEventListener("click", function() {
            document.querySelector(".active-btn").classList.remove("active-btn");
            this.classList.add("active-btn");
            document.querySelector(".active").classList.remove("active");
            document.getElementById(button.dataset.id).classList.add("active");
            window.scrollTo(0, 1)
        })
    });
    document.querySelector(".theme-btn").addEventListener("click", () => {
        document.body.classList.toggle("light-mode");
    })
    scroll();
})();

function scroll() {
    document.addEventListener("scroll", function() {
        let target;
        if (window.scrollY >= window.scrollMaxY) {
            target = document.querySelector(".active-btn").nextElementSibling;
        } else if (window.scrollY <= 0) {
            target = document.querySelector(".active-btn").previousElementSibling;
        }
        target && target.click();
    });
}

Here is the JavaScript code I made very quickly for that (so it is not the best), updated with the scrolling. To make it work, you have to add something to the body in the CSS: min-width: 101vh in order to get a scrollable window.
Note: The content of a section takes some delay to appears, so if you scroll fast it will go over the section before displaying the content.

@gashiray
Copy link

hey I ran into the same issue where the code wasn't running due to an error <TypeError: allSections.addEventListener is not a function> I tried to rectify it I looked in the comments I tried all the suggestions it didn't work, I copied the code and it still didn't work. Hopefully you can assist me with the java script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants