【问题标题】:How can I scroll to a particular element in jQuery?如何滚动到 jQuery 中的特定元素?
【发布时间】:2022-10-14 18:33:56
【问题描述】:

上下文:webflow 有一个内置的下拉菜单,当它打开时,它有一个“w--open”类。在这里,我为类名为“hover-close”的按钮提供了通过删除“w--open”来关闭该下拉菜单的能力。

它可以工作,但是当它关​​闭时,它会一直滚动到页面顶部,而不是滚动到具有“dropdown-list-9 w-dropdown-list”类的下拉元素。

  (function ($) {
    'use strict';
    $(".hover-close").on("click", function () {
      $(".dropdown-list-9.w-dropdown-list").removeClass(" w--open");
      $(this).next().slideToggle(300);
      $(".dropdown-list-9 w-dropdown-list").not($(this).next()).slideUp("fast");
      //Scroll to top for each accordion that is clicked.
      // The "180" is the distance from top. The "300" is the scroll speed. The "400" is for the animation delay depending on inner content length.
      setTimeout(() => {
        $('html').animate({scrollTop: $(this).offset().top-3000}, 300);
      }, 400);
    });
  }(jQuery));
</script>```

【问题讨论】:

  • 你能显示你的html代码吗?也许您正在使用&lt;a href="#"&gt;&lt;/a&gt;

标签: jquery dropdown webflow


【解决方案1】:

您需要创建&lt;a&gt; 元素并将属性设置为此标签并单击此标签运行时间。

    var a = document.createElement('a');
    a.href = '#sectionId';
    document.body.append(a);
    a.click();
    a.remove();
<p>on load this page will scroll to the section</p>
<pre>
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
</pre>
<section id="sectionId">
Section
</section>

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2019-10-08
    • 2013-02-28
    • 2010-10-04
    • 2019-12-27
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多