Javascript 如何滚动到第一个子div,而不是逐个滚动到所有目标子div?

Javascript 如何滚动到第一个子div,而不是逐个滚动到所有目标子div?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我的目标是多个容器div中的多个类似子div。随后,页面向第一个目标子div滚动 不幸的是,页面没有滚动到第一个子div,然后停止滚动。页面一个接一个地向所有子div滚动 如果运气不好,我正试图改变这种行为。我希望页面在第一个子分区时停止滚动 要查看我的实际问题,请单击标题中的任意年份: 当前jQuery代码: var $sections = $('section'); $('#index_year p, section p').on('click', function(e) { let y

我的目标是多个容器div中的多个类似子div。随后,页面向第一个目标子div滚动

不幸的是,页面没有滚动到第一个子div,然后停止滚动。页面一个接一个地向所有子div滚动

如果运气不好,我正试图改变这种行为。我希望页面在第一个子分区时停止滚动

要查看我的实际问题,请单击标题中的任意年份:

当前jQuery代码:

var $sections = $('section');
$('#index_year p, section p').on('click', function(e) {
  let year = $(this).data('y');
  $sections.each(function() {
    let $section = $(this);
    let $target = $('.' + year, $section);
    if ($target.length) {
      $('html, body').animate({
        scrollTop: $target.offset().top - 128
      }, 2000);
      $section.removeClass('yearNotFound');
      $section.animate({
        scrollLeft: $section.scrollLeft() + $target.position().left
      }, 2000);
    } else {
      $section.addClass('yearNotFound');
    };
  });
});
到目前为止,我试图:

  • 给我目标类的子div,然后滚动到该类的第一个子div,如下所示:
  • 添加
    返回,false在当前滚动代码之后,如下所示:
  • 使用:第一个选择器,如下所示:
  • 将“滚动代码”放置在循环之外,方法是制作一个单独的
  • 添加
    scrollIntoView
    ,我无法想象在何处以及如何使用它

  • 到目前为止,它对我不起作用,因此我正在寻求帮助。

    我会将具有“year”类的第一幅画存储在另一个变量中,以便在循环后处理到卷轴

    但是,每个部分都有一个匹配日期的动画必须在循环中

    $('#index_year p, section p').on('click', function(e) {
      let year = $(this).data('y');
      var $storedTarget;
    
      $sections.each(function() {
        let $section = $(this);
        let $target = $('.' + year, $section);
        if ($target.length) {
    
          // Store the target here.
          if(typeof($storedTarget) == "undefined"){
            $storedTarget = $target.first();  // The first one!
          }
    
          // Animate sections left/right if there's a match
          $section.animate({
            scrollLeft: $section.scrollLeft() + $target.position().left
          }, 1500);
    
          $section.removeClass('yearNotFound');
        } else {
          $section.addClass('yearNotFound');
        }
      });
    
      // Make sure there is at least a result..
      if($storedTarget.length>0){
    
        // Scrolling to the first section that has a match
        $('html, body').animate({
          scrollTop: $storedTarget.offset().top - 128
        }, 1500);
    
      }
    });
    

    $(“$target”)
    -意思是
    $(“#target”)
    ?您只需退出
    .each()
    循环作为sso,因为存在
    $target.length
    。因此,在
    if
    中,添加一个
    返回true在末尾。我明白了,我应该把返回值放在哪里?《代码》2000年)?就在
    }else之前{
    …在
    if
    中(是的,我想我们谈论的是同一个地方;)--这不是
    return,false;
    --而是
    return true;
    no coma!。同样,在这里,持续了40个小时,这就成功了,尽管现在目标年份中没有绘画的行没有消失。只是其中的一部分。我想我们现在正在防止所有部分被临时找到。。。。(感谢您的时间和努力!)评论不用于长时间的讨论;本次对话已经结束。让我们继续使用漂亮的解决方案。效果很好。谢谢@Louyspatricebesette
    $('html, body').animate({
        scrollTop: $target.offset().top - 128
    }, 2000); return, false;
    
    $( "$target:first" )`
    
    $('#index_year p, section p').on('click', function() {
        $('html, body').animate({
            scrollTop: $target.offset().top - 128
        }, 2000);
    });
    
    $('#index_year p, section p').on('click', function(e) {
      let year = $(this).data('y');
      var $storedTarget;
    
      $sections.each(function() {
        let $section = $(this);
        let $target = $('.' + year, $section);
        if ($target.length) {
    
          // Store the target here.
          if(typeof($storedTarget) == "undefined"){
            $storedTarget = $target.first();  // The first one!
          }
    
          // Animate sections left/right if there's a match
          $section.animate({
            scrollLeft: $section.scrollLeft() + $target.position().left
          }, 1500);
    
          $section.removeClass('yearNotFound');
        } else {
          $section.addClass('yearNotFound');
        }
      });
    
      // Make sure there is at least a result..
      if($storedTarget.length>0){
    
        // Scrolling to the first section that has a match
        $('html, body').animate({
          scrollTop: $storedTarget.offset().top - 128
        }, 1500);
    
      }
    });