javascript滚动不工作
我希望我的链接向下滚动页面到目标内容 这是我的脚本(在head标签中):javascript滚动不工作,javascript,jquery,html,ruby-on-rails,Javascript,Jquery,Html,Ruby On Rails,我希望我的链接向下滚动页面到目标内容 这是我的脚本(在head标签中): jQuery(文档).ready(函数($){ $(“.scroll”)。单击(函数(事件){ event.preventDefault(); $('html,body').animate({scrollTop:$(this.hash.offset().top},1200); }); }); 以下是我的链接(在body标签内): 这些代码块在我的application.html.erb中 提前谢谢你 检查$(thi
jQuery(文档).ready(函数($){
$(“.scroll”)。单击(函数(事件){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash.offset().top},1200);
});
});
以下是我的链接(在body标签内):
这些代码块在我的application.html.erb中
提前谢谢你 检查$(this.hash).offset().top值;
log($(this.hash).offset().top);
看这个 我的问题不在上面的代码中,但实际上我的application.js缺少require_树行,因此它不会加载其他js文件。虽然下面的答案不是我问题的解决方案(这就是为什么我没有选择它)@yodfz_cn非常有用,谢谢 它实际上指向目标内容,但动画不会工作,我的意思是,它不是一个平滑的过渡。这是我得到的:未捕获类型错误:无法读取未定义(索引)的属性“top”):80(匿名函数)到底什么是未定义的,你知道吗?this.hash未定义,编辑它,那么,this.hash从哪里来?或者什么是杂烩?
<script type="text/javascript">
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top},1200);
});
});
</script>
<%= link_to 'Plataformas', root_path + '#plataformas', class: "scroll" %>
<%= link_to 'Contato', root_path + '#contato', class: "scroll" %>