0

So I have one button I'm trying to get multiple functions to work with. On page load the above div is hidden, once the button is clicked it will make the div visible and scroll to the top. I can get both working separately but not together. Can someone point out what I'm doing wrong?

$(function () {
    $("#work").hide();
    $(".button").bind("click", function () {
        $("#work").show();
    });
    $(".button").click(function () {
        $('html, body').animate({
            scrollTop: $("#work").offset().top
        }, 2000);
    });
});

UPDATE 1:

Added a fiddle: http://jsfiddle.net/5qQJN/1/

UPDATE 2:

So it manages to work but only after you click it once. Say you click it, jumps to the top, you scroll down click again and then it will scroll with animation.. so does that mean I need a window load?

0

2 Answers 2

3
$(function () {
    $("#work").hide();
    $(".button").click(function () {
        $("#work").show();
        $('html, body').animate({
            scrollTop: $("#work").offset().top
        }, 2000);
    });
});

Combining them should work since nothing appears to be loading dynamically.

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

4 Comments

^ this -- I was wondering myself why two click handlers are necessary at all, it may be an oversimplification in the OP's example code.
Thanks for the reply and the edit. However still no dice, when I click the button the div will appear and it will scroll to the top but with no animation, just a straight jump.
Can you update your question with your HTML, and if possible a jsFiddle that illustrates the problem.
So it manages to work but only after you click it once. Say you click it, jumps to the top, you scroll down click again and then it will scroll with animation.. so does that mean I need a window load?
0

I made a Fiddle for you: http://jsfiddle.net/FaC4r/

Looks like it's working there. It would help though if you provided your HTML.

THE DIV IS HIDDEN UP HERE - SCROLL DOWN FOR BUTTON
<div id="work"></div>

<div id="spacer"></div>

<input type="button" class="button" value="CLICK ME TO SHOW 'WORK' AND SCROLL UP TO IT">

[UPDATE BASED ON COMMENTS]

I edited the Fiddle for you with your new HTML: http://jsfiddle.net/FaC4r/2/

Seems like your CSS is specifying a work div with padding of 0, that, combined with the fact that there isn't anything in the div, means that it's invisible! You can see in the fiddle above, that I use 2em in padding in order to ensure that I'm actually seeing the div grow and expand.

I'd say - and this is true for basically everything in your programming career - use the working example as a base and incrementally add your specific requirements. Test early and often. Add one thing and test to make sure it did what you expected. Once you build up a certain comfort level, you'll be able to extend that iterative cycle.

Hope that helps! :)

1 Comment

Appreciate the help.. It's strange I feel like it should be working but for some reason it isn't. I threw up a fiddle if you notice anything lemme know, I'm still fairly new at code. Thanks again.

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.