JAIL JQuery Plugin (jQuery Asynchronous Image Loader) - php

I would really appreciate if anyone can spot anything that I am doing wrong here!
I am attempting to use this plug-in: http://www.sebastianoarmelibattana.com/projects/jail
I have included it in my web page which is a search form that submits via an AJAX request and displays the search results in a table. This table happens to be another plugin called dataTables.
Here are the relevant bits of code:
The re-initialisation of the plug-ins after the AJAX request has been successfully completed (the same initialisation is done in the actual HTML page aswell):
// re-initialise lazyload, lightbox and dataTable plugins
$('img.lazy').jail();
$('a.lightbox').lightBox();
$('#test-docs-table').dataTable( {
"bPaginate": false,
"sDom": 'Rlfrtip',
"aoColumns": [
null,
null,
{ "sType": "title-numeric" },
null
]
});
This is the line of php that displays the relevant table cell with the thumbnail in it:
$shortname = $row['filename'];
$doc = "/central/testdocs/".$row['filename'];
$thumb = "include/doc-thumbnail.php?doc=$doc";
echo("\n<div class=\"thumbnail\">\n<td class=\"center\"><a class=\"lightbox\" title=\"$shortname\" href=\"$thumb&dpi=96\"><img class=\"lazy\" data-href=\"$thumb\" src=\"images/blank.png\" width=\"50\" height=\"50\"/></a></td>\n</div>\n");
Notice that another plug-in is involved - lightbox so not sure if that affects the situation.
At the moment it looks like the JAIL plug-in is doing something but all the images are loaded as soon as the page has loaded.
The lightbox still works when you click on the thumbnails - the page actually works just the same as it did before except that before the page is fully loaded the thumbnail cell is empty.

I discovered what was wrong, the silliest of things!
Because I tried to use the original lazy load plug-in I had an existing rule in my CSS hiding images of class lazy so the plug-in wasn't able to do it's thing.
If anyone else runs into this problem that's what to do!

Related

After page has already loaded, load another image and use it to replace the page's current BackGround image

The front page of my site has quite a big image as a background, of course I've made it as small as possible but it's still going to take a moment to load on slow connections.
So if the user stays on my site for a while I want to change that background image to another one. I've seen some of the plugins which are available to have something similar but they all seem to load the images all first. Which would be slow for my purposes
So I would like to just load the page as normal. Then after a little delay behind the scenes load another image and once it's loaded, replace the original image. Is this possible?
Sure set an image tag in the body like this: <img id="bgqueue" />
Your CSS would hide that:
#bgqueue {
display : none;
}
And your jQuery would look something like this:
setTimeout(function() {
$('#bgqueue').attr('src', 'http://someOtherjpg').on('load', function() {
$('body').css({background: 'url(' + $(this).attr('src') + ')'});
});
}, 30000);
So the image is loaded into that image tag behind the scenes after 30 seconds in that example, and once it's loaded, the callback fires and sets the body background image to the cache file.
Demo: http://jsfiddle.net/seancannon/TpPkk/

How do I use URLs of images from a text file in a javascript slideshow?

I'm creating a slideshow where I'm displaying images based on their urls. I've used PHP to extract the image urls from web pages and I've used JavaScript to display them in a slideshow format. Only thing is, the first picture takes a lot of time to load so I decided to cache the urls by storing them in a text file, but I don't know how to read the urls from the text file in my JavaScript bit?
Could anyone point me in the right direction as to how I should proceed. I couln't find anything helpful online.
My JS code is like this:
<script language="JavaScript1.1">
var slideimages=new Array()
slideshowimages("<?php echo join("\", \"", $image_urls); ?>") <--this is where I was initially echoing the array or image urls from php, but it proves slow for the first few images
function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}
var slideshowspeed1=30000
var whichimage1=0
function slideit1(){
if (!document.images)
return
document.images.slide1.src=slideimages[whichimage1].src
if (whichimage1<slideimages.length-1)
whichimage1++
else
whichimage1=0
setTimeout("slideit1()",slideshowspeed1)}slideit1()
</script>
Thanks!
Why are you pulling from an external website? You generally will get a lot more speed if you pull them locally. I do believe that once it pulls the images once or so, it will cache for users when it shows up again. What you could do is to use that list you pull and create the images hidden on the page so they load with the page. Then when going through the slideshow, the user should have had time to cache the images and the slideshow will have sped up.
Just make a CSS class known as hidden and visability:hidden;it. Most browsers will still try to load the data.

Don't load content In X element

My layout is like the above.
1, 2, 3 and 4 divs can be dragged and dropped into any position 1-4 and in the dock. Now these are just php includes. When i drop them in the dock i use css to:
display:none;
This hides everything in the div except the title. Perfect. However the content is still being loaded in the "background" and i plan on having plenty of these "widgets" and i can see this becoming a serious issue with loading all these content.
What i would like to happen is:
If dropped into dock then don't load everything between:
<p><?php include('example.php'); ?></p>
But if dragged back into 1, 2, 3 or 4 then allow the content in the paragraph to be loaded.
I was thinking maybe getting the parent div using jQuery.
I just need a little push in the right direction in regards which language could solve my issue and maybe some simple examples. Now another thing would this be "dynamic" as in as soon as i drag it from the dock to 1, 2, 3 or 4. It would instantly update or would i need to constantly reload either the page or this div to allow the content to check its location and decide whether it is allowed to load or not.
Thank you.
If you want to find out what element you are landing in when you do the drop, it's ->
$('element').droppable({
//other logic
drop: function(event, ui){
//$(this) is now the element you just dropped nito
if($(this).prop('id') == 'dock'){
console.log("This is the Dock...We don't add content here.");
}else{
//function to pull data to your container...$.load() or whatever
}
}
});

lightbox dynamic image retrieval

I am constructing a lighbox gallery, currently experimenting with FancyBox (http://fancybox.net) and ColorBox (http://colorpowered.com/colorbox).
By default you have to include a link to the large version of the image so that the Lightbox can display it. However, I am wanting to have the image link URLs pointing to a script rather than directly to the image file. So for example, instead of:
<a href="mysite/images/myimage.jpg">
I want to do:
<a href="mysite/photos/view/abc123">
The above URL points to a function:
public function actionPhotos($view)
{
$photo=Photo::model()->find('name=:name', array(':name'=>$view));
if(!empty($photo))
{
$user=$photo->user;
$this->renderPartial('_photo', array('user'=>$user, 'photo'=>$photo, true));
}
}
At some point in the future the function will also update the view count of the image.
Now this approach is working to an extent - most images load up but some do not load up (the lightbox gets displayed in a malformed state). I think the reason for this is because it is not processing the function quick enough. For example when I click the "next" button it needs to go to the URL, process the function and retreive/output the response.
Does anybody know how I can get this working properly?
I added "width" and "height" attributes on my image tags and it works fine now.

Lightbox image / link URL

Basically I have a slightly non-standard implementation of FancyBox. By default you have to include a link to the large version of the image so that the Lightbox can display it. However, in my implementation, the image link URLs point to a script rather than directly to the image file. So for example, instead of:
<a href="mysite/images/myimage.jpg" rel="gallery">
I have:
<a href="mysite/photos/view/abc123" rel="gallery">
The above URL points to a function:
public function actionPhotos($view)
{
$photo=Photo::model()->find('name=:name', array(':name'=>$view));
if(!empty($photo))
{
$this->renderPartial('_photo', array('photo'=>$photo, true));
}
}
The "$this->renderPartial()" bit simply calls a layout file which includes a standard HTML tag to output.
Now when the user clicks on a thumbnail, the above function is called and the large image is displayed in the Lightbox.
Now if the user right clicks on the thumbnail and selects "open in new tab/window" then the image is displayed in the browser as per normal, i.e. just the image. I want to change this so that it displays the image within a layout.
In the above code I can include the following and put it in an IF statement:
$this->render('photos', array('photo'=>$photo));
This will call the layout file "photos" which contains the layout to display the image in.
I have a specific limitation for this - the image URL must remain the same, i.e. no additional GET variables in the URL. However if we can pass in a GET variable in the background then that is OK.
I will most likely need to change my function above so that it calls a different file for this functionality.
EDIT: To demonstrate exactly what I am trying to do, check out the following:
http://www.starnow.co.uk/KimberleyMarren
Go to the photos tab and hover over a thumbnail - note the URL. Click the thumbnail and it will open up in the Lightbox. Next right click on that same thumbnail and select "open in new tab/new window". You will notice that the image is now displayed in a layout. So that same URL is used for displaying the image in the Lightbox and on its own page.
The way StarNow have done this is using some crazy long JavaScript functionality, which I'm not too keen on replicating.
The html link should point to the layout showing the image on a new page by default, e.g.:
<a href="mysite/images/show/123" rel="gallery">
Before the lightbox opens, append a query string to the url in order to distinguish it from the normal link and load the layout for the lightbox. As soon as the image is loaded in the lightbox, change the link back to its original state.
$("a[rel=gallery]").fancybox({
'onStart': function (selectedArray, selectedIndex, selectedOpts) {
var el = $(selectedArray[selectedIndex]);
el.attr('href', el.attr('href') + '?mode=lightbox');
},
'onComplete': function (currentArray, currentIndex, currentOpts) {
var el = $(currentArray[currentIndex]);
el.attr('href', el.attr('href').split("?")[0]);
}
});
You will then have to process the following link in order to return the lightbox layout:
<a href="mysite/images/show/123?mode=lightbox" rel="gallery">
You should be able to modify the JavaScript function that generates the HTML with the <img /> tag to link the image to such a page. Although, if you are trying to make it so that selecting "Open image in new tab" opens a page like this, then that might be impossible (unless there is some sort of crazy cookie/session implementation to alternate between the image script just passing an image and generating a page, which I think could be possible). To assign a new href for the link to have when you click "Open link in new tab" should be quite possible by just modifying the JavaScript function.
Could you clarify what exactly you are attempting to do? Open link in new tab or open image in new tab?
Edit: It appears that the FancyBox script is changing the href of your link to point directly to the image. You would need to find where in the script it is selecting each link tag with rel="gallery" and replacing the href to point to the images; you will want it to not change the href if you want it left as "mysite/photos/view/abc123", for example.
If you need the same functionality the demo site you posted is using, then this is easy to achieve, but keep in mind that the site is NOT using the same URL for both the pop-up and the standalone image page.
Click on any thumbnail with Firebug console is open, you'll notice that it's making an Ajax request to get the image from a different URL! which is an obvious behavior.
http://www.starnow.co.uk/profile/PhotosTrackView.aspx?photo_id=2129864
While the link is pointing to:
http://www.starnow.co.uk/KimberleyMarren/photos/2129864/
you see your links should point to the correct image page, in case of JS disabled browsing or right clicking (as you mentioned) AND using JS to override the link default behavior (which is redirecting you to the image page).
So for example you can have a method that will generate your image layout/page, and this should be used as href; and override the click event of the link to call a similar method (using ajax) but this time it'll retrieve the image itself to use it in your lightbox.

Categories