滚动时将标题粘贴并替换到窗口顶部
在我正在进行的项目中,我有许多带有照片的 div,每个 div 都有一个标签,其中列出了其中照片的 YYYY/MM/DD。
<div class="photoCollection">
<div class="label">
2010-12-24
</div>
<div class="photos">
<img>
...
<img>
</div>
</div>
我试图实现的功能是在您滚动浏览时将当前 div 的标签粘贴到窗口顶部,以便您知道您正在查看哪些照片。当滚动浏览下一个照片 div 时,此标签会在到达窗口顶部时替换卡住的标签。
1)我正在努力寻找有效的方法来检测标签何时位于窗口顶部。我不喜欢以下内容,因为查看每个标签似乎有点矫枉过正(“stuckToTop”类为标签添加了固定定位)。
$('.label').each(function()
{
if ( $(this).offset().top < $(window.)scrollTop() )
{
$(this).addClass('stuckToTop');
}
我也尝试过:
$('.label').filter(function()
{
return $(this).offset().top < $(window.)scrollTop;
}).addClass('stuckToTop');
是否有更优雅的方法的建议?
2)我还希望新标签在页面向上滚动时将先前卡住的标签向上推。当您滚动浏览 Picasa 的照片库时,您会看到同样的效果,因为新的部分标签将以前的部分标签推开。
我猜这意味着每次新标签向顶部滚动时,我都必须向上调整 StickToTop 标签的位置,最终替换它?
谢谢!
In the project I'm working on, I have a number of divs with photos, each with a label that lists the YYYY/MM/DD of the photos within.
<div class="photoCollection">
<div class="label">
2010-12-24
</div>
<div class="photos">
<img>
...
<img>
</div>
</div>
The functionality I'm trying achieve is to have the label of the current div stick to the top of the window as you scroll through so you know which photos you're looking at. When the next div of photos is scrolled through, this label replaces the stuck label as it reaches the top of the window.
1) I'm struggling to find efficient ways to detect when a label is at the top of the window. I don't like the following since looking through every label seem like overkill (the "stuckToTop" class adds fixed positioning to the label).
$('.label').each(function()
{
if ( $(this).offset().top < $(window.)scrollTop() )
{
$(this).addClass('stuckToTop');
}
I have also tried:
$('.label').filter(function()
{
return $(this).offset().top < $(window.)scrollTop;
}).addClass('stuckToTop');
Are there any suggestions for a more elegant approach?
2) I would also like the new label to push the previously stuck label upwards as the page is being scrolled up. You'll see this same effect when you're scrolling through Picasa's photo gallery, as new section labels push the previous ones out of the way.
I'm guessing this means I'll have to adjust the position of the stuckToTop label upwards every time the new label gets scrolled higher towards the top, eventually replacing it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议,对于固定滚动标题,观看 Remy Sharp 的 jQuery for Designers 视频(此处演示),尽管他将其与 iPhone 的联系人菜单进行了比较,而不是比 Picasa 强,但两者听起来很相似。
我认为他的教程解决了您的两个问题。
I'd suggest, for fixed-scrolling headers, watching Remy Sharp's jQuery for Designers video-cast (Demo here), albeit he compares it to the iPhone's contacts-menu, rather than Picasa, but the two sound similar enough.
I think his tutorial addresses both of your question's points.