简体   繁体   English

如何从另一页打开CSS单选按钮手风琴上的特定手风琴部分?

[英]How to open a specific accordion section on a CSS radio button accordion from another page?

I have links on a page (index.html). 我在页面上有链接(index.html)。 The links go to a gallery page. 链接转到图库页面。 The accordion section I want to open is on the gallery page. 我要打开的手风琴部分在图库页面上。

<a href="gallery#accordion1">Accordion 1</a>
<a href="gallery#openModal2">Open Model 4</a>
<a href="gallery#ac-2">Accordion 1 section 2</a>

On the gallery page there are CSS accordions with CSS modals inside of the accordion sections that open. 在图库页面上,打开的手风琴部分中有带CSS模态的CSS手风琴。

The first link above opens the gallery page and brings accordion one to the top of the page, with the first section open (by default, as it is 'checked'.) A similar link to the second accordion also works the same way. 上方的第一个链接打开图库页面,并将手风琴一个带到页面顶部,第一部分处于打开状态(默认情况下为“选中”)。第二个手风琴的相似链接也以相同的方式工作。 The second link leads to the gallery page, and opens the modal, which would be great if that's what I wanted. 第二个链接指向图库页面,并打开模式,如果那是我想要的,那将是很棒的。

But the third link will only go to the gallery page. 但是第三个链接只会转到图库页面。 It won't open the section, and similar links to sections in another accordion won't bring that accordion to the top. 它不会打开该节,并且到另一个手风琴中节的类似链接也不会将该手风琴推到顶部。 (The gallery page is also responsive and has media queries, which bring the appropriate accordion to the top when I test it out by shrinking the browser width before clicking the link on the index page.) (图库页面也具有响应性,并具有媒体查询功能,当我通过单击索引页面上的链接之前缩小浏览器宽度进行测试时,将相应的手风琴放在顶部。)

What I want to happen is: click the link on the index page, go to the gallery page, bring the appropriate accordion to the top and open the corresponding accordion section. 我要发生的是:单击索引页面上的链接,转到图库页面,将合适的手风琴放在顶部,然后打开相应的手风琴部分。

I've looked at numerous answers on Stack Overflow. 我看过关于Stack Overflow的众多答案。 The one that comes closest to what I'm trying to do is here: Open jQuery Accordion 最接近我想做的事情是: Open jQuery Accordion

I also looked at many others and part of why I'm having difficulty figuring this out may be that I'm not using jQuery UI syntax (header/div/header/div/etc.) 我还查看了许多其他内容,为什么我很难弄清楚这一点的部分原因可能是我没有使用jQuery UI语法(header / div / header / div / etc。)。

<section class="gal-container" id="accordion1">
<!--start accordion one section 1 -->
<div class="modalDialog" id="openModal1">
    <div>
        <a class="modal-close" href="#close">&times;</a> Stuff
    </div>
</div><!-- end modal one -->
<div>
    <input checked id="ac-1" name="accordion-1" type="radio"> <label for="ac-1">Title</label>
    <article>
        <a href="#openModal1">Open Modal </a>
        <p>yadda information yadda</p>
    </article>
</div><!--start accordion one section 2 -->
<div class="modalDialog" id="openModal2">
    <div>
        <a class="modal-close" href="#close">&times;</a> Stuff
    </div>
</div>
<div>
    <input id="ac-2" name="accordion-1" type="radio"> <label for="ac-2">Title</label>
    <article>
        <a href="#openModal2">Open Modal </a>
        <p>yadda information yadda</p>
    </article>
</div>

I've tried to figure this out using both javaScript and jQuery. 我试图使用javaScript和jQuery弄清楚这一点。 What I've wound up with at the moment is trying to use the link with the specific section, making the hash my variable, and then making the radio button checked. 目前,我要解决的问题是尝试使用特定部分的链接,将哈希值设为变量,然后选中单选按钮。

// find the id from the link
var radio = window.location.hash;
function checkRadio {
  if radio === ("ac-1" || "ac-2" || "ac-3" || "ac-4") {
  document.getElementById("accordion1").onclick = function() {
    var openAcc = window.open(this.href);
  } else {
    document.getElementByID("accordion2").onclick = function() {
      var openAcc = window.open(this.href);
    }

    // check the radio button
    document.getElementById(radio).checked = true;
  }

My Shortish fiddle 我的矮小提琴

I was able to construct a partial answer using jQuery. 我能够使用jQuery构建部分答案。 I added this in the header of the page with the accordions on it. 我在页面的标题中添加了手风琴。

 jQuery(document).ready(function() {
// get the #section from the URL
var hash = window.location.hash; 
// open accordion
 $(hash).prop("checked", true); });

This will open the correct accordion panel, from an external page, using the third link, but the correct accordion won't come to the top of the page when it is resized for mobile. 这将使用第三个链接从外部页面打开正确的手风琴面板,但是在为移动设备调整大小时,正确的手风琴不会出现在页面顶部。


This works: 这有效:

Editing because I figured out the answer (based on the answer to this question: Loading page based on external link ) 正在编辑,因为我找出了答案(基于此问题的答案: 基于外部链接的加载页面

If the link looks like this: 如果链接如下所示:

 <a class="open-local" href="#ac-1">Item 1</a>

This will open the section from any link on the same page with the open-local class: 这将打开同一页面上带有open-local类的任何链接的部分:

  //For local link to accordion
  jQuery(document).ready(function() {
  $('a.open-local').click(function(){
// get the #section from the URL
var hash = window.location.hash; 
// open accordion
 $(hash).prop("checked", true);
 //scroll
var accordionTop =  $(hash).closest('section');
$('html, body').animate({
scrollTop: $(accordionTop).offset().top
    }, 500);
  });

For links coming from an external page: 对于来自外部页面的链接:

var hash = window.location.hash; 
$(hash).prop("checked", true);
var accordionTop =  $(hash).closest('section');
$('html, body').animate({
        scrollTop: $(accordionTop).offset().top
    }, 500);
});

My revised fiddle is here 我修改过的小提琴在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM