简体   繁体   English

根据 jQuery 中的垂直滚动值加载具有多个背景的长页面?

[英]Loading a long page with multiple backgrounds based on vertical scroll value in jQuery?

The design I've been given to work with is 960px wide by around 7000px tall, cut into five vertically-stacked segments at arbitrary points.我使用的设计是 960 像素宽,约 7000 像素高,在任意点切成五个垂直堆叠的段。 There's a fixed-placed sidebar that scrolls to each segment, depending on which navigation link is clicked.有一个固定放置的侧边栏,可以滚动到每个部分,具体取决于单击的导航链接。 Atop this are a bunch of sliders, transparent PNGs, headlines and paragraphs, predominantly positioned in a relative fashion.最上面是一堆滑块、透明 PNG、标题和段落,主要以相对的方式定位。

I need to ultimately do two things:我最终需要做两件事:

  1. Hide the corresponding quick-nav links in the sidebar until its related segment's background image has loaded在侧边栏中隐藏相应的快速导航链接,直到其相关段的背景图像已加载

  2. Load (and ideally fade in) the transparent PNGs in each section as needed -- the user scrolls between two vertical scroll values and stops for a second, causing that section's transparent PNGs to then load and fade in.根据需要在每个部分中加载(理想情况下淡入)透明 PNG - 用户在两个垂直滚动值之间滚动并停止一秒钟,导致该部分的透明 PNG 然后加载并淡入。

I'm currently using softscroll.js to achieve a smooth scrolling effect for when the sidebar links are clicked (thus scrolling to the related anchors).我目前正在使用softscroll.js在单击侧边栏链接时实现平滑的滚动效果(从而滚动到相关的锚点)。 Thus, lazy loading techniques that begin to load images as you scroll by won't work -- if I click the last link in the sidebar nav and it scrolls me to the bottom, I don't want every image between the bottom segment and the top loading as a result.因此,在您滚动时开始加载图像的延迟加载技术将不起作用 - 如果我单击侧边栏导航中的最后一个链接并将我滚动到底部,我不希望底部段之间的每个图像和结果是顶部加载。

While I'll need to figure out point 1 sooner rather than later, I'm more interested in the second question.虽然我需要尽快弄清楚第 1 点,但我对第二个问题更感兴趣。

How would one use jQuery to load images inside a certain element if and only if the user has paused between two specific vertical scroll values?当且仅当用户在两个特定的垂直滚动值之间暂停时,如何使用 jQuery 在某个元素内加载图像?

Thank you!谢谢!

(BTW, please don't respond with appelsiini's lazyload jQuery plugin . It's unsupported by the developer and doesn't appear to work in modern browsers. Thanks!) (顺便说一句,请不要使用appelsiini 的lazyload jQuery 插件来回应。开发人员不支持它,并且在现代浏览器中似乎不起作用。谢谢!)

Check the user's position using scrollTop() .使用scrollTop()检查用户的 position 。 You should be able to do this inside a setInterval() callback.您应该能够在setInterval()回调中执行此操作。

function loadBackground() {
    var userTop = $(window).scrollTop();
    var userBtm = userTop + $(window).height();
    var elemTop = $('#element').scrollTop();
    var elemBtm = elemTop + $('#element').height();

    if ((userBtm >= elemTop) && (userTop <= elemBtm))
    {
        // Load images
    }
}

$('document').ready(function(){
    setInterval(loadBackground,500);
}

(This is untested code, but you get the idea.) (这是未经测试的代码,但你明白了。)

Edit: Adjusted the conditional so that if any part of the element is in the window it will fire.编辑:调整条件,以便如果元素的任何部分在 window 中,它将触发。

A slightly more full fat solution to the already great one suggested by Justin is to use jQuery Waypoints to manage the in viewport events.对于 Justin 所建议的已经很不错的解决方案,一个稍微更完整的解决方案是使用jQuery Waypoints来管理视口内事件。

You may run into issues if you're rewritting the scroll mechanism on mobile browsers using something like iScroll or scrollability.如果您使用 iScroll 或可滚动性之类的东西重写移动浏览器上的滚动机制,您可能会遇到问题。 In which case you'll need to use their APIs to investigate a fix.在这种情况下,您需要使用他们的 API 来调查修复。

Hide the corresponding quick-nav links in the sidebar until its related segment's background image has loaded在侧边栏中隐藏相应的快速导航链接,直到其相关段的背景图像已加载

Haven't tested it but you should be able to just do this by sticking a couple of <img> s in with the same src as the background (with display: none; of course) and testing the.complete property of each image, on a short setInterval loop, until they're all loaded.尚未对其进行测试,但您应该能够通过将几个<img>与背景相同的 src (当然display: none; )并测试每个图像的 .complete 属性来做到这一点,在一个短的 setInterval 循环上,直到它们都被加载。 Don't use onload, it tends to be unreliable on images.不要使用 onload,它在图像上往往不可靠。

Load (and ideally fade in) the transparent PNGs in each section as needed -- the user scrolls between two vertical scroll values and stops for a second, causing that section's transparent PNGs to then load and fade in.根据需要在每个部分中加载(理想情况下淡入)透明 PNG - 用户在两个垂直滚动值之间滚动并停止一秒钟,导致该部分的透明 PNG 然后加载并淡入。

Justin 's solution should work for detecting when you're in a given section. Justin的解决方案应该适用于检测您何时处于给定部分。 Just set a flag to false before you do the softscroll, and true once it stops- and then only mark a section as active when the flag is true.只需在执行软滚动之前将标志设置为 false,一旦停止,将标志设置为 true - 然后仅在标志为 true 时将部分标记为活动。

I would "disable" the images by pointing their src attribute to a 1x1 blank gif, and setting their data-src attribute to the real src.我会通过将它们的src属性指向 1x1 空白 gif 并将它们的data-src属性设置为真实的 src 来“禁用”图像。 Then just do something like:然后只需执行以下操作:

$('.selected-section img').each(function () {
    $(this).attr('src', $(this).data('src'));
});

You'll have to be sure to set the size of the "disabled" images to the size that they'll be once their image has loaded, or else the page will jump around a lot.您必须确保将“禁用”图像的大小设置为加载图像后的大小,否则页面会跳动很多。

You could use the window.onscroll event handler to detect when you're scrolling, but this is generally a bad idea.可以使用 window.onscroll 事件处理程序来检测您何时滚动,但这通常不是一个好主意。 For discussion on this see: http://ejohn.org/blog/learning-from-twitter/有关此问题的讨论,请参见: http://ejohn.org/blog/learning-from-twitter/

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

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