Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using an anchor as a trigger not possible #77

Closed
inquiztr opened this issue Nov 24, 2023 · 1 comment
Closed

using an anchor as a trigger not possible #77

inquiztr opened this issue Nov 24, 2023 · 1 comment
Labels
question Further information is requested

Comments

@inquiztr
Copy link

I have been having trouble using an anchor tag as a trigger.
The use case for this is I have 5-10 frequently asked questions and i would like to have anchors to each question and also a mechanism to scroll to the correct question if the url contains a hash parameter.

In the past (using bootstrap3) this was possible and I simply return false or event.preventdefault on the anchor allow the accordion override the anchor functionality.

I have worked around it by wrapping my anchor with another div and using that as a trigger and using css to prevent clicks on anchor tag obscuring/intercepting the wrapping trigger event listener but this is not ideal.

Another issue I noticed is when I initialize the accordion to have a specific question open, via the openOnInit parameter, it does not fire the onOpen or beforeOpen event which I was relying on to begin the scroll down the page.

I have been able to work around both issues but it would be nice to have better support on those two features built in.
Thanks for your time and thanks for this great accordion script.

@michu2k michu2k added the question Further information is requested label Nov 26, 2023
@michu2k
Copy link
Owner

michu2k commented Nov 26, 2023

Hi @inquiztr

Auto-scrolling to the specified element is possible by adding a unique ACCORDION_ID and using the #ACCORDION_ID hash in the URL. This behavior is unrelated to the Accordion package, and I don't see why a link with href="#ACCORDION_ID" wouldn't work.

On the other hand, if you want to scroll to the element if the element has been opened, you can use onOpen fn

onOpen: (currentElement) => {
    window.scrollTo({left: 0, top: currentElement.offsetTop, behavior: 'smooth'});
}

When it comes to the second issue, methods such as onOpen won't be triggered when openOnInit is set, but if you want to bypass the default behavior, you can use methods like accordion.open(idx) triggered by the window's load event or other external functions.

@michu2k michu2k closed this as completed Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants