jQuery Accordion - 它会滚动到打开项目的顶部吗?
使用 jQuery 手风琴控件,当它离开屏幕时,如何让它滚动到我选择的项目?
何时:
- 我有一个手风琴项目,其内容大于可视窗口
- 我向下滚动到第二个手风琴项目
- 我单击第二个手风琴项目以显示它
- 第一个手风琴选项折叠,第二个打开,但滑出屏幕。
手风琴是否有滚动到第二项的选项?
With the jQuery accordion control, how can I have it scroll to an item I've selected when it is off the screen?
When:
- I have an accordion item with contents larger than the viewable window
- I scroll down to the second accordion item
- I click the second accordion item to display it
- The first accordion option collapses, and the second opens, but slides off screen.
Is there an option for the accordion to scroll to the second item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
它对我有用并经过测试,
http://jsfiddle.net/ilyasnone/aqw613em/
It works for me and tested,
http://jsfiddle.net/ilyasnone/aqw613em/
您可以尝试使用 scrollTo jQuery 插件。它可以让您执行如下操作:
将
ScrollTo()
绑定到accordionactivate
事件,如下所示:accordionactivate
事件何时触发?参考文献: jQuery UI 手风琴
You can try using the scrollTo jQuery plugin. It lets you do things like this:
Bind
ScrollTo()
to theaccordionactivate
event, like this:When is the
accordionactivate
event triggered?References: jQuery UI Accordion
因为我希望折叠为真,所以我添加了一个 if 测试来消除所有项目被折叠的错误。我也不希望标题正好位于页面顶部,因此我将scrollTop 位置降低了100:
As I want collapse to be true I added an if test to cancel out the error of all items being collapsed. I also didn't want the header to be exactly at the top of the page, so I lowered the scrollTop location by 100:
我知道这个问题已经很老了,但以上都没有达到我的预期效果。我就是这样完成的。 -50 是为了防止它出现在 iPad 或 iPhone Web 应用程序中,这样页面就不会滚动状态栏后面的手风琴标题顶部。
I know this question is old, but none of the above worked as desired for me. This is how I accomplished it. The -50 was just in case this was going to appear in an iPad or iPhone webapp so that the page didn't scroll the top of the accordion header behind the status bar.
请参考这个< /a> 由 techfoobar 回答
通过上述修改它对我有用。
Please refer this answer by techfoobar
It is working for me with above modification.
当您使用手风琴单击关闭功能时,我在绑定事件时遇到问题。只需添加一个 if 语句就可以解决这个问题。
I had problems with binding the event when you used the accordion click to close function. Adding just a single if statement fixed it thought.
马丁的解决方案效果很好。但是,当您添加此代码时,无论您的手风琴是否在页面上可见,它都会始终滚动到顶部。
如果您只想在手风琴内容大于可视窗口时滚动到顶部,请使用以下代码:
Solution from Martin works great. However when you add this code it will scroll to the top always, no matter if your accordion is visible on the page or not.
If you want to scroll to the top only when your accordion content larger than the viewable window, then use next code:
我实现了第一个答案,并且最喜欢这个选项,因为它是所有手风琴面板的一个功能。但是,我注意到,当尝试(重新)关闭同一个手风琴面板时,我不断收到错误 - 它会在 ScrollTo 插件中的这一行停止脚本:
The val 返回为空,所以我在这里找到了另一个答案:检查它是否为空并添加/替换了此功能 - 所以它现在可以工作了。
I implemented the first answer and liked this option best because it is one function for all accordion panels. But, I noticed that I kept getting an error when trying to (re)close the same accordion panel - it would halt the script at this line in the ScrollTo plugin:
The val was returning empty, so I found another answer here that said to check for it empty and added / replaced this function - so it works now.
嘿确实有同样的问题。这是对我有用的至少看起来最简单的方法。
使用scrollTo插件。
希望它对某人有用。
Hey did have the same issue. Here is what worked for me at least what seems the easiest way.
Using the scrollTo plugin.
Hope it might be of use for someone.
上使用此函数即可
只需在 window.load Perfectl wokring
Simply use this function on window.load
perfectl wokring
不需要scrollTo插件,你可以这样做:
It's not necessary scrollTo plugin, you can do that: