0

I'm currently trying to make a navigational menu that on click scrolls to a div positioned lower on the page. Initially it worked but as I added a CSS transition to highlight the text, the jQuery will no longer trigger the scrolling to happen upon clicking it.

$("#about, #about:hover").click(function() {
    $('html,body').animate({
        scrollTop: $("#box1").offset().top},
        'slow');
});

The #about id is a part of the nav menu and will scroll to #box1 on click. I've tried adding the hover state to the selector, changing it to .trigger rather than .click but I'm not seeing a simple solution. I've recreated the event in Chrome, Safari, and using JSFiddle.

Is the JQuery conflicting with the transition, the hover state, or my code in general?

EDIT: It is working in the Fiddle example but the event still isn't happening when produced locally (everything is linked and correct with no errors in console).

15
  • Have you tried using $.hover instead of :hover css selector? Commented Jun 11, 2016 at 16:49
  • 1
    In your JSFiddle you forgot to add the jQuery resource. After adding it - it scrolled updated JSFiddle Commented Jun 11, 2016 at 16:52
  • @JaqenH'ghar It is working in Fiddle but still isn't working locally. Using Google's 1.12.2 external link for the library here. Commented Jun 11, 2016 at 16:55
  • @Wikiti Haven't tried it, I'm more familiar with CSS transitions so that's what I used. Commented Jun 11, 2016 at 16:55
  • Check if you have any z-index in css which you recently added. May be z-index is stopping the event. Commented Jun 11, 2016 at 16:55

2 Answers 2

2

Your JS code runs before the DOM is fully loaded.

Put the JS inside document.ready();:

$(document).ready(function () {
    $("#about, #about:hover").click(function () {
        $('html,body').animate({
            scrollTop: $("#box1").offset().top
        },
            'slow');
    });

    $("#portfolio, #portfolio:hover").click(function () {
        $('html,body').animate({
            scrollTop: $("#box2").offset().top
        },
            'slow');
    });

    $("#social, #social:hover").click(function () {
        $('html,body').animate({
            scrollTop: $("#box3").offset().top
        },
            'slow');
    });

    $("#contact, #contact:hover").click(function () {
        $('html,body').animate({
            scrollTop: $("#box4").offset().top
        },
            'slow');
    });
});

See updated JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

It doesn't work if your jQuery link and/or functions link are in the header.

It works for me if the js links are towards the end of your html like before the closing body tag.

1 Comment

Documentation suggests that the library is linked in the header. I've been linking it in the header but my problem was purely syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.