简体   繁体   中英

jQuery imitate #anchor click

I'm working on a site with a accordion list. I want the viewport to "scroll" to the clicked accordion, so the list starts here.

I can achieve this by setting href="#accordion-id" but I then have to ID all the accordion list elements on the whole site.

Is there any way to imitate the #anchor click with jQuery?

You can do this by finding the elements position with some code like this:

var item = $("#accordion-item").position();
$(window).scrollTop(item.top);

In fairness, you're probably going to need ID tags either way if you want a unique movement for each accordion item. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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