admin管理员组

文章数量:1426026

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?

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?

Share Improve this question edited Aug 16, 2013 at 17:23 Huangism 16.4k7 gold badges50 silver badges75 bronze badges asked Aug 16, 2013 at 17:17 SacklingSackling 1,8205 gold badges42 silver badges73 bronze badges 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/j08691/NMSFf – j08691 Commented Aug 16, 2013 at 17:25
  • 1 and doesn't work here jsfiddle/NMSFf/1 which loads the function when dom is ready – Huangism Commented Aug 16, 2013 at 17:26
 |  Show 6 more ments

2 Answers 2

Reset to default 4

http://jsfiddle/mA34T/

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

Would that fix your issue?

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

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

本文标签: javascriptjquery scroll to position on page on anchor clickStack Overflow