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
2 Answers
Reset to default 4http://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
版权声明:本文标题:javascript - jquery scroll to position on page on anchor click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470400a2659721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论