点击锚链接,滚动并打开特定的手风琴/选项卡。

huangapple go评论65阅读模式
英文:

Jquery click on anchor link and scroll to and open specific accordion/tab

问题

以下是翻译好的代码部分:

HTML

<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>

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>

希望这些翻译有帮助。如果您有其他问题,可以随时提出。

英文:

I have a set of tabs that contain content. The tabs convert to accordions on mobile. This part is already working. The first tab is the default open tab on page load. I have a jump link on the page that I'm trying to code to scroll to the tabs and open (make active) the appropriate tab.

Trying to figure out how to have a link at the top of the page jump to the accordion section and also open tab 2 data-actab-id="2"

HTML

&lt;section class=&quot;accordion&quot;&gt;
&lt;!-- Tabs --&gt;
&lt;section class=&quot;accordion-tabs&quot;&gt;
&lt;button class=&quot;accordion-tab accordion-active&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;0&quot;&gt;Title-one&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;1&quot;&gt;Title-two&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;2&quot;&gt;Title-three&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;3&quot;&gt;Title-four&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;4&quot;&gt;Title-five&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;5&quot;&gt;Title-six&lt;/button&gt;
&lt;/section&gt;
&lt;!-- Tabbed Content --&gt;
&lt;section class=&quot;accordion-content&quot;&gt;
&lt;article class=&quot;accordion-item accordion-active&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;0&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Description&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;1&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Specs&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;2&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Reviews&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;div id=&quot;reviews&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;3&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Q&amp;A&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;4&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Videos&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;5&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;&lt;span class=&quot;chart-title-indicator&quot;&gt;Size Chart&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;/section&gt;
&lt;/section&gt;

Javascript that controls open and closing tabs/accordions (this works)

&lt;script&gt;
$(document).ready(function () {
console.log(&quot;document ready&quot;);
const labels = document.querySelectorAll(&quot;.accordion-item__label&quot;);
const tabs = document.querySelectorAll(&quot;.accordion-tab&quot;);
function toggleShow() {
const target = this;
const item = target.classList.contains(&quot;accordion-tab&quot;) ?
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(&quot;accordion-active&quot;);
} else {
tab.classList.remove(&quot;accordion-active&quot;);
}
}
});
labels.forEach(function (label) {
const tabItem = label.parentElement;
if (tabItem.dataset.actabGroup === group) {
if (tabItem.dataset.actabId === id) {
tabItem.classList.add(&quot;accordion-active&quot;);
} else {
tabItem.classList.remove(&quot;accordion-active&quot;);
}
}
});
}
labels.forEach(function (label) {
label.addEventListener(&quot;click&quot;, toggleShow);
});
tabs.forEach(function (tab) {
tab.addEventListener(&quot;click&quot;, toggleShow);
});
});
&lt;/script&gt;

I've tried just a basic #hash jump link that will go to the div, but since the div is inside a tab, it will only work when that tab is already open/active, and not when it's not active.

I can use a jump link to go to the outside div but that just jumps down the page and doesn't make the appropriate tab active.

&lt;a href=&quot;#reviews&quot;&gt;Read Reviews&lt;/a&gt;
or
&lt;a href=&quot;#&quot; class=&quot;reviews&quot;&gt;Read Reviews&lt;/a&gt;
with this function:
$(document).ready(function () {
$(&quot;a.reviews&quot;).click(function (event) {
event.preventDefault();
$(&quot;html, body&quot;).animate(
{
scrollTop: $(&quot;.accordion&quot;).offset().top - 150,
},
500
);
});
});

But these solutions don't solve the issue of also opening/making the tab active. I've reviewed many other questions on here but I can't figure out how to address integrating the scroll to into my existing code of opening the tab.

答案1

得分: 0

以下是您要翻译的内容:

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>

希望这对您有所帮助。如果您需要更多的翻译,请告诉我。

英文:

For my answer, I'm first opening the tab, then using jquery's scrollTo to scroll to the div within the tab.

I'm passing the tab ID via a data attribute and using the HREF as the target div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const labels = document.querySelectorAll(&quot;.accordion-item__label&quot;);
const tabs = document.querySelectorAll(&quot;.accordion-tab&quot;);
function toggleShow() {
const target = this;
const item = target.classList.contains(&quot;accordion-tab&quot;) ?
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(&quot;accordion-active&quot;);
} else {
tab.classList.remove(&quot;accordion-active&quot;);
}
}
});
labels.forEach(function(label) {
const tabItem = label.parentElement;
if (tabItem.dataset.actabGroup === group) {
if (tabItem.dataset.actabId === id) {
tabItem.classList.add(&quot;accordion-active&quot;);
} else {
tabItem.classList.remove(&quot;accordion-active&quot;);
}
}
});
}
labels.forEach(function(label) {
label.addEventListener(&quot;click&quot;, toggleShow);
});
tabs.forEach(function(tab) {
tab.addEventListener(&quot;click&quot;, toggleShow);
});
$(document).ready(function() {
$(&quot;a.scrollTo&quot;).click(function(event) {
let tab = $(&quot;.accordion-item[data-actab-id=&#39;&quot; + $(this).data(&quot;actab-id&quot;) + &quot;&#39;]&quot;);
let target = $(this).prop(&quot;hash&quot;);
tab.addClass(&quot;accordion-active&quot;);
event.preventDefault();
$(&quot;html, body&quot;).animate({
scrollTop: $(target).offset().top - 150            
},
500
);
});
});

<!-- language: lang-css -->

.accordion{margin-top:300px;}
#reviews{margin-top:1500px}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a href=&quot;#reviews&quot; data-actab-id=&quot;2&quot; class=&quot;scrollTo&quot;&gt;Read Reviews&lt;/a&gt;
&lt;section class=&quot;accordion&quot;&gt;
&lt;!-- Tabs --&gt;
&lt;section class=&quot;accordion-tabs&quot;&gt;
&lt;button class=&quot;accordion-tab accordion-active&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;0&quot;&gt;Title-one&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;1&quot;&gt;Title-two&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;2&quot;&gt;Title-three&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;3&quot;&gt;Title-four&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;4&quot;&gt;Title-five&lt;/button&gt;
&lt;button class=&quot;accordion-tab&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;5&quot;&gt;Title-six&lt;/button&gt;
&lt;/section&gt;
&lt;!-- Tabbed Content --&gt;
&lt;section class=&quot;accordion-content&quot;&gt;
&lt;article class=&quot;accordion-item accordion-active&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;0&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Description&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;de
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;1&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Specs&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;ss
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;2&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Reviews&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;div id=&quot;reviews&quot;&gt;
REVIEWS!
&lt;/div&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;3&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Q&amp;A&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;4&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;Videos&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;article class=&quot;accordion-item&quot; data-actab-group=&quot;0&quot; data-actab-id=&quot;5&quot;&gt;
&lt;h4 class=&quot;accordion-item__label&quot;&gt;&lt;span class=&quot;chart-title-indicator&quot;&gt;Size Chart&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;accordion-item__container&quot;&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;/section&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 03:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定