链接锚和开放式手风琴

问题描述 投票:1回答:2

如何使用外部锚链打开手风琴面板?

我尝试使用锚链接,只是加载页面,而不打开面板。

我想要实现的是,当点击锚链接时,页面加载,滚动到面板,然后打开手风琴。

此链接将锚定到另一页并应打开手风琴:

 <a class="linkTo" href="/project#<?php the_sub_field('area_link'); ?>">

这是我用来打开手风琴的代码:

 $(document).ready(function() {
   $(".accordion .accord-header").click(function() {
     // for active header definition
     $('.accord-header').removeClass('on');
     $(this).addClass('on');

     // accordion actions
     if($(this).next("div").is(":visible")){
       $(this).next("div").slideUp(600);
       $(this).removeClass('on');
     } else {
       $(".accordion .accord-content").slideUp(600);
       $(this).next("div").slideToggle(600);
     }
  });
});

这是手风琴结构:

<div class="accordion">
  <div class="accord-header" id="<?php the_sub_field('area_link'); ?>">Accordion 1</div>
    <div class="accord-content">
        <!-- Content -->
    </div>
  </div>
</div>
javascript jquery html accordion
2个回答
0
投票

您可以在准备初始化手风琴的文档上使用window.location.hash

$(function () {
    var $accordionSecion = $(window.location.hash);
    if ($accordionSecion.length) {
       $(window).scrollTop($accordionSecion.offset().top);
       $accordionSecion.addClass('on');
    }
});

你可以使用与onhashschange监听器相同的处理程序来处理点击手风琴标题。

祝好运。 :)


0
投票
$(document).ready(function(){
 var hash = window.location.hash;
 if (hash) {
   var element = $(hash);
   if (element.length) {
   element.trigger('click');
 }
 }
});

在您要打开手风琴的页面上尝试上面的代码。

© www.soinside.com 2019 - 2024. All rights reserved.