Jquery SlideToggle 和滚动
我的页面底部出现的滑动切换链接有问题。 实际上,我希望用户单击以显示/隐藏隐藏的 div (效果很好)
问题是页面不会向下滚动以显示现在的,而不是隐藏的 div
作为 JQuery 新手,有什么方法可以切换 div然后将其置于页面中央?或者至少向下滚动,而不会让事情变得太复杂?
谢谢
I'm having a problem with a slidetoggle link which appears at the bottom of my page.
Effectively I want the user to click to show/hide the hidden div (which works fine)
The problem is the page doesn't scroll down to show the now, not hidden div
As a JQuery novice, is there any way to toggle the div and then center it on the page? Or at least scroll down, without things getting too complicated?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要执行此功能,我建议使用 slipToggle 调用中的回调来使用 scrollTop 函数。您将能够通过使用 offset 来确定scrollTop setter的值来获取顶部位置相对于页面的切换容器。我建议将滚动行为限制为仅在显示元素时触发,而不是隐藏时触发。
一般来说,直接设置页面滚动可能会造成稍微不和谐的用户体验。出于这个原因,我实际上将向您提供对scrollTop 进行动画处理的代码,而不是使用scrollTop 函数直接设置它,但这种方法不是必需的,因为直接调用scrollTop 将平等地定位页面。我只是认为作为用户我宁愿看到逐渐滚动而不是突然的位置变化。
例如,代码将采用以下形式:
您可能希望实际使用
$(this).offset().top - 50
或类似的内容,以便滚动设置在顶部上方几个像素处容器的,但这取决于你。我发现我不喜欢我的元素紧贴窗口的顶部边框。很抱歉,我没有为此创建测试用例,因为我只是草率地进行拍摄,但如果它不像广告中那样工作,请告诉我,我将调整代码。
To perform this function, I'd recommend using a callback from your slideToggle call to set the document scroll using the scrollTop function. You'll be able to determine the value to the scrollTop setter by using offset to get the top position of the toggled container relative to the page. I'd suggest restricting the scroll behavior to only fire when the element is shown, not hidden.
In general, directly setting page scroll can be a slightly jarring UX. For that reason I'm actually going to give you code that animates scrollTop rather than straight up sets it using the scrollTop function, but this approach is not necessary as a direct call to scrollTop will equally position the page. I just think as a user I'd rather see a gradual scroll rather than a sudden positional change.
The code, for instance, would take the form:
You may want to actually use
$(this).offset().top - 50
or something similar so the scroll's set just a few pixels above the top of the container, but that's up to you. I find I don't like my elements to be butting up against the top border of the window.I apologize I haven't created a test case for this as I'm shooting from the hip, but if it doesn't work as advertised, let me know and I'll adjust the code.