views:

92

answers:

1

Hey, What I would like is to use the jQuery plugin: LazyLoad

But, rather than trigger the event on Window Scroll - I would like to trigger it when the elements come into view.

(I have no scroll bars on my site, so scroll bars are not an option).

Help me before I have no hair left.

EDIT: Ok, so I got it to work - SORT OF - it only seems to work vertically and ONLY WITH THE OVERFLOW SHOWING! *

+2  A: 

Your best bet is to set a timer and repeatedly check for image visibility. Or you can make the tracker function public method and then call it from the outside if something alters.

Change LazyLoad to something like this:

// ...
var elements = this;
if ("scroll" == settings.event) { // <-- you may want to remove conditional block
     var tracker = function(event) {  // <-- give it a name
        var counter = 0;
        elements.each(function() {
            if ($.abovethetop(this, settings) ||
                $.leftofbegin(this, settings)) {
                    /* Nothing. */
            } else if (!$.belowthefold(this, settings) &&
                !$.rightoffold(this, settings)) {
                    $(this).trigger("appear");
            } else {
                if (counter++ > settings.failurelimit) {
                    return false;
                }
            }
        });
        /* Remove image from array so it is not looped next time. */
        var temp = $.grep(elements, function(element) {
            return !element.loaded;
        });
        elements = $(temp);
    };

    (function repeat(){              
      tracker(); // <-- so you can use it here
      if (elements.length) 
        setTimeout(repeat, 100); // <-- check every 100ms
    })();
}
// ...
galambalazs
Hi, cheers for this. It seems that this is still binding the visibility to the window scroll method? I have no scroll bars on my site.
Neurofluxation
you can remove event binding and only define `tracker` function there. But the idea is the same. See my update.
galambalazs
I removed the `if ("scroll" == settings.event) {` line and the corresponding `}`, but it does nothing. I've initialised it with `$('img.link').lazyload();` - no errors, no functionality...
Neurofluxation
Curiously, if I use `$('img').lazyload();` it works fine :/
Neurofluxation
*I only showed you the door, you need to walk through it.* :) works then?
galambalazs
haha, sort of - I've updated my question. It only works when `overflow` is not `hidden`
Neurofluxation
can you post an example on jsbin.com?
galambalazs
http://jsbin.com/egope - jQuery isn't included, I've put LazyLoad in the javascript alongside the `$(document).ready` function. Styles are there (no images, so best of luck)
Neurofluxation
please correct the urls of the images or set `<base>` + some `<style>` would be good too :)
galambalazs
unfortunetely my client forbids me to upload the images until the site is live. I've added in what little styles I could let go... sorry to be a pain
Neurofluxation
press `new revision` if you make a change, in order to save the updated version
galambalazs
http://jsbin.com/egope/5 <- there you go :S oops!
Neurofluxation
I get the feeling that this question is going to be buried as well :(
Neurofluxation
I think you've got a solid starting point.
galambalazs
@galambalazs - thanks, I've accepted your answer (primarily because i want a 100% acceptance rate and you actually tried, which is more than I can say for the other thousands of SO peeps. :)
Neurofluxation
@Neurofluxation: there's no reason to shoot for 100% accept rate. If you accept an answer prematurely, it can discourage someone else from coming along later and adding a better answer.
Ether