2

I am trying to create an anchor link that when clicked will scroll down the page.

the script:

<script type="text/javascript">                         
$(document).ready(function() {
    function scrollWin(){
        $('html,body').animate({
            scrollTop: $("#scrollToHere").offset().top
        }, 800);
    }
});
</script>

the link:

<a class="icon_button" href="#"  onclick="scrollWin();" ><i class="icon-chevron-down "></i> </a>

and then the div to be scrolled to:

<div id="scrollToHere">
Scroll to here
</div>

What am I missing?

11
  • 4
    The function is called scrollWin() and you missed "r" in onclick event on the anchor tag - scollWin().
    – Mario Dian
    Commented Aug 16, 2013 at 17:20
  • 4
    try removing the $(document).ready(function() { wrapper
    – bluetoft
    Commented Aug 16, 2013 at 17:24
  • 1
    I agree with Bluetoft, I think wrapping it in doc ready made it in that scope so the onclick call does not work since the function does not exist
    – Huangism
    Commented Aug 16, 2013 at 17:25
  • 1
    Works fine here jsfiddle.net/j08691/NMSFf
    – j08691
    Commented Aug 16, 2013 at 17:25
  • 1
    and doesn't work here jsfiddle.net/NMSFf/1 which loads the function when dom is ready
    – Huangism
    Commented Aug 16, 2013 at 17:26

2 Answers 2

4

http://jsfiddle.net/mA34T/

$('#foo').click(function () {
    $('html,body').animate({
        scrollTop: $("#scrollToHere").offset().top
    }, 800);
});

Would that fix your issue?

0

try removing the '$(document).ready(function() {' wrapper

There is no need to wrap this as you're just declaring a function.

Not the answer you're looking for? Browse other questions tagged or ask your own question.