Jquery点击锚链接并滚动到并打开特定的accordion/tab

cyvaqqii  于 2023-04-05  发布在  jQuery
关注(0)|答案(1)|浏览(123)

我有一组包含内容的标签。标签转换为移动的上的 accordion 。这部分已经工作。第一个标签是页面加载时默认打开的标签。我在页面上有一个跳转链接,我正在尝试编码滚动到标签并打开(激活)相应的标签。
试图弄清楚如何让页面顶部的链接跳转到Accordion部分,同时还打开Tab 2data-actab-id=“2”

超文本标记语言

<section class="accordion">
  <!-- Tabs -->
  <section class="accordion-tabs">
    <button class="accordion-tab accordion-active" data-actab-group="0" data-actab-id="0">Title-one</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="1">Title-two</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="2">Title-three</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="3">Title-four</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="4">Title-five</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="5">Title-six</button>
  </section>

  <!-- Tabbed Content -->
  <section class="accordion-content">
    <article class="accordion-item accordion-active" data-actab-group="0" data-actab-id="0">
      <h4 class="accordion-item__label">Description</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="1">
      <h4 class="accordion-item__label">Specs</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="2">
      <h4 class="accordion-item__label">Reviews</h4>
      <div class="accordion-item__container">
          <div id="reviews">
          </div>
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="3">
      <h4 class="accordion-item__label">Q&A</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="4">
      <h4 class="accordion-item__label">Videos</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="5">
      <h4 class="accordion-item__label"><span class="chart-title-indicator">Size Chart</span></h4>
      <div class="accordion-item__container">
      </div>
    </article>
  </section>
</section>

控制打开和关闭选项卡/ accordion 的Javascript(这很有效)

<script>
  $(document).ready(function () {
    console.log("document ready");

    const labels = document.querySelectorAll(".accordion-item__label");
    const tabs = document.querySelectorAll(".accordion-tab");

    function toggleShow() {
      const target = this;
      const item = target.classList.contains("accordion-tab") ?
        target :
        target.parentElement;
      const group = item.dataset.actabGroup;
      const id = item.dataset.actabId;

      tabs.forEach(function (tab) {
        if (tab.dataset.actabGroup === group) {
          if (tab.dataset.actabId === id) {
            tab.classList.add("accordion-active");
          } else {
            tab.classList.remove("accordion-active");
          }
        }
      });

      labels.forEach(function (label) {
        const tabItem = label.parentElement;

        if (tabItem.dataset.actabGroup === group) {
          if (tabItem.dataset.actabId === id) {
            tabItem.classList.add("accordion-active");
          } else {
            tabItem.classList.remove("accordion-active");
          }
        }
      });
    }

    labels.forEach(function (label) {
      label.addEventListener("click", toggleShow);
    });

    tabs.forEach(function (tab) {
      tab.addEventListener("click", toggleShow);
    });

  });
</script>

我尝试了一个基本的#hash跳转链接,它将跳转到div,但由于div位于选项卡内,因此它只在该选项卡已经打开/活动时才起作用,而不是在它不活动时。
我可以使用跳转链接到外部div,但这只是向下跳转页面,并没有使适当的标签活动。

<a href="#reviews">Read Reviews</a>
or
<a href="#" class="reviews">Read Reviews</a>

with this function:

$(document).ready(function () {
  $("a.reviews").click(function (event) {
    event.preventDefault();
    $("html, body").animate(
      {
        scrollTop: $(".accordion").offset().top - 150,
      },
      500
    );
  });
});

但是这些解决方案并没有解决打开/激活标签页的问题。我在这里回顾了许多其他问题,但我不知道如何将滚动条集成到我现有的打开标签页的代码中。

kknvjkwl

kknvjkwl1#

对于我的答案,我首先打开选项卡,然后使用jquery的scrollTo滚动到选项卡中的div。
我通过一个数据属性传递选项卡ID,并使用HREF作为目标div。

const labels = document.querySelectorAll(".accordion-item__label");
const tabs = document.querySelectorAll(".accordion-tab");

function toggleShow() {
  const target = this;
  const item = target.classList.contains("accordion-tab") ?
    target :
    target.parentElement;
  const group = item.dataset.actabGroup;
  const id = item.dataset.actabId;

  tabs.forEach(function(tab) {
    if (tab.dataset.actabGroup === group) {
      if (tab.dataset.actabId === id) {
        tab.classList.add("accordion-active");
      } else {
        tab.classList.remove("accordion-active");
      }
    }
  });

  labels.forEach(function(label) {
    const tabItem = label.parentElement;

    if (tabItem.dataset.actabGroup === group) {
      if (tabItem.dataset.actabId === id) {
        tabItem.classList.add("accordion-active");
      } else {
        tabItem.classList.remove("accordion-active");
      }
    }
  });
}

labels.forEach(function(label) {
  label.addEventListener("click", toggleShow);
});

tabs.forEach(function(tab) {
tab.addEventListener("click", toggleShow);
});

$(document).ready(function() {
      $("a.scrollTo").click(function(event) {
        let tab = $(".accordion-item[data-actab-id='" + $(this).data("actab-id") + "']");
        let target = $(this).prop("hash");
        tab.addClass("accordion-active");
        
        event.preventDefault();
        $("html, body").animate({
            scrollTop: $(target).offset().top - 150            
          },
          500
        );
      });
});
.accordion{margin-top:300px;}
#reviews{margin-top:1500px}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#reviews" data-actab-id="2" class="scrollTo">Read Reviews</a>

<section class="accordion">
  <!-- Tabs -->
  <section class="accordion-tabs">
    <button class="accordion-tab accordion-active" data-actab-group="0" data-actab-id="0">Title-one</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="1">Title-two</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="2">Title-three</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="3">Title-four</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="4">Title-five</button>
    <button class="accordion-tab" data-actab-group="0" data-actab-id="5">Title-six</button>
  </section>

  <!-- Tabbed Content -->
  <section class="accordion-content">
    <article class="accordion-item accordion-active" data-actab-group="0" data-actab-id="0">
      <h4 class="accordion-item__label">Description</h4>
      <div class="accordion-item__container">de
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="1">
      <h4 class="accordion-item__label">Specs</h4>
      <div class="accordion-item__container">ss
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="2">
      <h4 class="accordion-item__label">Reviews</h4>
      <div class="accordion-item__container">
        <div id="reviews">
        REVIEWS!
        </div>
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="3">
      <h4 class="accordion-item__label">Q&A</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="4">
      <h4 class="accordion-item__label">Videos</h4>
      <div class="accordion-item__container">
      </div>
    </article>
    <article class="accordion-item" data-actab-group="0" data-actab-id="5">
      <h4 class="accordion-item__label"><span class="chart-title-indicator">Size Chart</span></h4>
      <div class="accordion-item__container">
      </div>
    </article>
  </section>
</section>

相关问题