为什么这个Javascript滚动函数不能工作?

Why won't this Javascript scroll function work?

本文关键字:不能 工作 函数 滚动 Javascript 为什么      更新时间:2023-09-26

我非常困惑为什么这不会运行。我正试图让点击滚动按钮向下滚动。

https://jsfiddle.net/n9jqvas9/

<script>
 $("button").click(function() {
    $('html,body').animate({
     scrollTop: $("s2text").offset().top},
    'slow');
  });
</script>

您缺少jQuery(在您的示例中)和s2text之前的点,因为它是一个类。

$("#button").click(function() {
    $('html,body').animate({
        scrollTop: $(".s2text").offset().top},
        'slow');
  });

您需要添加JQuery库并设置类选择器:

$(".s2text").offset().top

大家:

$("s2text").offset().top
工作的例子。

您缺少点(.)类名。

$("button").click(function() {
    $('html,body').animate({
        scrollTop: $(".s2text").offset().top},
        'slow');
  });