-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue when using jQuery.Lazy() Plugin with a function AJAX call #253
Comments
First, keep in mind, if you do it like this, you will create a new Lazy instance every time the user searches and you add all images to a new instance over and over again. May this is not the best idea. ;) The reason why this doesn't work is, that Lazy listens to the page load to start loading images. But in your case, the page load has already happen when you create the instance. Therefore you may use the let lazyInstance = $(".lazyLoadImage").Lazy({
chainable: false,
bind: 'event',
effect: "fadeIn",
effectTime: 1000,
threshold: 0
}); But as said before, this is not the best idea. So, maybe do it like this: let lazyInstance = $(".lazyLoadImage").Lazy({
chainable: false,
effect: "fadeIn",
effectTime: 1000,
threshold: 0
});
function mySearch() {
$('#myList').hide();
let pMyString = $('#myString').val();
$("#myList").load("mySearch/mySearchListPartial", { __RequestVerificationToken: '@token', "pMyString": pMyString, ... }, function (responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
lazyInstance.addItems(".lazyLoadImage");
lazyInstance.update();
$('#myList').slideDown();
}
else if (statusTxt == "error") {
$('#myList').show().html('<p class="error-details">Error: ' + xhr.status + ': ' + xhr.statusText + '</p>');
}
});
} |
Very good point. I have created an instance and just call the addItems/update now (as you suggested above). But I am still having an issue with images not fading in (still need to click/scroll the vertical scrollbar to get them to start appearing). Just and FYI: Initially, when I changed to an instance, the images wound NOT display at all. After some debugging/testing I tracked it down to an AJAX load issue. I think what is happening is that when the page first loads, .lazy does NOT see any reference on the page to the Just so we're on the same page, what happens is that there is a simple But the original problem still remains: I still need to click/scroll the vertical scrollbar to get the images to start appearing after the mySearch() call? Is there any way for me to trigger .lazy manually (so that it thinks the scroll bar has been moved)? Just throwing things out there .... :) As I mentioned originally, on a simple page without an AJAX call, .lazy works awesome. Unfortunately, almost all my pages use AJAX to get the data. Oh, and BTW: I'm not sure if this is relevant, but the latest version of the library shows it to be 1.7.11 on github, but the actual jquery.lazy.js I have (and redownloaded to make sure) says jQuery & Zepto Lazy - v1.7.10? |
OK... so I'm not sure if this is going to help... but... I also noticed that in the first page I mentioned above (where AJAX is used to retrieve a record list via AJAX as soon as the page loads), the images do NOT always appear (now that I started using an instance). What happens now is usually the first time I open the page, images show up. But when I click the refresh button on the browser, most of the time images are blank (probably the place holders are shown). It's sporadic, and if I keep clicking the refresh button, eventually the images come up. Here is what I'm using: let lazyInstance = $(".lazyLoadImage").Lazy({
chainable: false,
effect: "fadeIn",
effectTime: 1000,
threshold: 0
}
});
getlazyLoadImage();
function getlazyLoadImage() {
$('#myList').hide();
let pMyString = $('#myString').val();
$("#myList").load("mySearch/mySearchListPartial", { __RequestVerificationToken: '@token', "pMyString": pMyString, ... }, function (responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
lazyInstance.addItems(".lazyLoadImage");
lazyInstance.update();
$('#myList').slideDown();
}
else if (statusTxt == "error") {
$('#myList').show().html('<p class="error-details">Error: ' + xhr.status + ': ' + xhr.statusText + '</p>');
}
});
} Just to test, I removed the instance as the code shows below and that works fine. getlazyLoadImage();
function getlazyLoadImage() {
$('#myList').hide();
let pMyString = $('#myString').val();
$("#myList").load("mySearch/mySearchListPartial", { __RequestVerificationToken: '@token', "pMyString": pMyString, ... }, function (responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
$(".lazyLoadImage").Lazy({
chainable: false,
effect: "fadeIn",
effectTime: 1000,
threshold: 0
});
$('#myList').slideDown();
}
else if (statusTxt == "error") {
$('#myList').show().html('<p class="error-details">Error: ' + xhr.status + ': ' + xhr.statusText + '</p>');
}
});
} Do I need to structure the instance call some other way for .lazy to work correctly? |
So I had a bit more time to test/think about this, and this morning I noticed that in my first page (where AJAX retrieves records as soon as the page loads), when I copied your suggestion above, I didn't notice that the But the second page, where AJAX is called via a 'Search' button, I'm still having problems. It makes sense to use an instance, but I think that instance 'array' list needs to be cleared out each time the 'Search' button is clicked. After all, the AJAX call may return a combination of previously displayed images and some new images. Maybe the instance just needs to be reset and start from scratch? Does that make sense? If that is the case, then how do I do that? Is there a public function to clear the array and leave the instance in place for the next search population? Sorry for this continued comments, but hopefully some of this will be helpful... :) |
Well... I kind of spoke too soon (in my previous message above), because the AJAX is still NOT working correctly. First, I did fix an issue by adding let lazyInstance = $(".lazyLoadImage").Lazy({
chainable: false,
bind: 'event',
effect: "fadeIn",
effectTime: 1000,
threshold: 0
}
});
getlazyLoadImage();
function getlazyLoadImage() {
$('#myList').hide();
let pMyString = $('#myString').val();
$("#myList").load("mySearch/mySearchListPartial", { __RequestVerificationToken: '@token', "pMyString": pMyString, ... }, function (responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
$("#myList").slideDown("fast", function () {
lazyInstance.addItems(".lazyLoadImage");
lazyInstance.update();
});
}
else if (statusTxt == "error") {
$('#myList').show().html('<p class="error-details">Error: ' + xhr.status + ': ' + xhr.statusText + '</p>');
}
});
} To further test, I also created 2 separate pages:
Here is what I have noticed:
Furthermore, in both pages, I also pulled out the lines: lazyInstance.addItems(".lazy");
lazyInstance.update(); into a separate .js file (in .net core you can't debug javascript when it's embedded in the HTML page), and what I found is that the Page 1) shows a list of 15 Is this then an issue with .lazy() not reading the Daniel... I tried to be as helpful as I can with my debugging/observations because I would really like to resolve this issue so that I can use this throughout my application. All my images come from the DB via AJAX, so unfortunately I can't use the AJAX extension you provide. Please let me know if this is something you can address, or do I need to just go without .lazy() because of AJAX? |
I have two different pages where I use jQuery.Lazy(). In the first page, AJAX is used to retrieve a record list via AJAX as soon as the page loads. Here .lazy works fine and the images fadein as soon as they show up in the viewport.
On the second page, the user can enter search criteria, and when they click the 'Search' button, a function is called that also uses AJAX to retrieve a record list with images ( see code below). Here though, the images do NOT fadein as soon as they are displayed. The placeholders are displayed, but the images don't fadein. You have to click/scroll the vertical scrollbar on the browser for the fadein to begin.
The ONLY difference that I can tell between the 2 pages is that the second one uses a function() to call the AJAX/retrieve, but the rest of the code is practically identical.
How do I call .lazy to start the fadein of images that are initially displayed on the viewport as soon as they are loaded (without having to click/drag the vertical scrollbar first)?
The text was updated successfully, but these errors were encountered: