简体   繁体   English

jQuery 模仿#anchor click

[英]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.我可以通过设置href="#accordion-id"来实现这一点,但是我必须标识整个站点上的所有手风琴列表元素。

Is there any way to imitate the #anchor click with jQuery?有什么方法可以用 jQuery 模仿 #anchor 点击?

You can do this by finding the elements position with some code like this:您可以通过使用如下代码找到元素 position 来做到这一点:

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.公平地说,如果您希望每个手风琴项目都有一个独特的动作,您可能需要任何一种方式的 ID 标签。 Hope this helps希望这可以帮助

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

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