I'm working on a photo gallery, and I would like the ability to have a gif preloader show before the main image is loaded, and then show the main image.
So, this is what I got:
I have a #photo_place which is the div that holds the main photo. This changes depending on what thumbnail the user selects. When the user does select a thumbnail, this function is triggered:
function gallery(icon){
$('#photo_place').css('background-image','url("../images/'+icon+'.png")');
}
Now, what I want, is to first, show a preloader gif at the #photo_place, load in the selected image ... somewhere, and when that image is loaded, replace the preloader, with the main image.
So maybe something like this?
function gallery(icon){
$('#photo_place').css('background-image','url("../images/preloader.gif")');
load.in.image'images/'+icon+'.png';
if(load.in.image == true){
$('#photo_place').css('background-image','url("loaded image")');
}
}
Of course, that wasn't real JS, but something like that should work right?
Any ideas?
Thanks
Perhaps you're looking for something like this?
function gallery(icon) {
var preLoader = '../images/preloader.gif';
var imagePath = '../images/' + icon + '.png';
$('#photo_place').css('background-image','url("../images/preloader.gif")');
var image = new Image();
image.onload = function() {
$('#photo_place').css('background-image', imagePath);
}
image.src = imagePath;
}
Also checkout Image class reference: http://www.javascriptkit.com/jsref/image.shtml
Related
I have this code here where I have a slider with thumbnails and the sliders large image takes it's next attr('src') from the click on a thumb, but I'd like to change that src only when the image has loaded. This is the source that I've come up with so far:
$('.thumb').click(function(){
topImageSrc = $(this).data("bigimage");
$(topImageSrc).load(function(){
coverMain.attr('src',topImageSrc);
main.fadeOut("slow");
})
});
Sadly this doesn't work as the topImageSrc is just a path variable to the large image, like so: /uploads/largeimages/pic1.jpg
How could I force it to load and then check if it's done, after that - apply the new image?
You can do it like this:
$('.thumb').click(function () {
topImageSrc = $(this).data("bigimage");
var topImage = $('<img>').attr('src', topImageSrc);
$(topImage).load(function () {
coverMain.attr('src', topImageSrc);
main.fadeOut("slow");
});
});
This creates an image (without appending it anywhere), sets its source to the topImageSrc URL and checks for the load event.
Let's assume a bunch of images are preloaded via something like this:
function loadNextImage() {
var img = new Image();
img.src = '/path/to/imageN.jpg';
img.onload = function() {
if(stillLoading()) {
loadNextImage();
} else {
printLoadedImages();
}
}
}
What can be used for printLoadedImages() to make them print (ultimately as a series- one per page, but for starters let's just say one image)
When I try setting the image source to a div, I can see the image- but trying printThis or printArea jquery plugins did not do it... seems to expect that the image was loaded via page load, not at runtime
I've been having a problem caused by the previous image staying on the screen until the next is loaded.
My program uses a flowchart where various images are needed for certain questions. I've been using the following code to change the source from one to another.
HTML:
<img class= 'right' id= 'imageBox' style= 'width: 20%; height: auto;' src= www.1stimage.com/>
javascript:
document.getElementById("imageBox").src = 'www.2ndimagesite.com';
If the computer has a slow connection, the first image could stay on the screen for up to 10 seconds before the next one shows up. How do I get it to not display anything until it's finished?
Thanks.
Preload the image and update the src after it's loaded:
var img = new Image();
var newsrc = 'www.2ndimagesite.com';
img.onload = function () {
document.getElementById("imageBox").src = newsrc;
};
img.src = newsrc;
You can change aproach a bit to achieve what you want.
You can preload images and after that just select what image to show. You can read more about this here: http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/
You can make start loading new image async and change current image to image like loading spinner or some image, which shows that something is loading at the moment (example: ) On onload handler you will rewrite this spinner to loaded image.
I wanted to write ~ same that #nedt wrote. Btw, I don't think that his code will help you. I think you will achieve same effect as you said in answer. Anyway, he was first and his answer was close, so I will just use his example.
document.getElementById("imageBox").src = "loading image link"; // load spinner
var img = new Image(); // load image asynchronously
var newsrc = 'www.2ndimagesite.com';
img.onload = function () { // onload handler
document.getElementById("imageBox").src = newsrc;
};
img.src = newsrc;
So, old image was loaded on page loaded. You did some action, for example pressed button. If you have low speed, loading spinner will be shown and after new image is loaded async, new image will be shown. If you have enought speed, new image will appear immediately.
Hope this will help you!
document.images[i].complete
will be true if picture[i] source is loaded.
you could preload all pictures an dont show it until the status change.
I have a page that swaps some fairly large images in and out. There are too many to preload when the page initially loads so that is not an option. So what I need to do is load them as they are requested by the user. Right now I'm using jQuery to replace the img's src. This works fine but the images I am loading can be around 500KB and it looks bad as they paint down the screen as they are downloading. What I'd like to do is pop a loading gif on the page when the image is in the process of loading then have the loading gif disappear once the image is loaded. I'm struggling to find a way to do that though. Here is the JS/jQuery code that I have that just replaces the src.
var product = "bowl";
var image = "dog.jpg"; //this is actually pulled from a data attribute, but its just hardcoded here for an example
$("#images img[data-product="+product+"]").attr("src", "/img/tablesetting/"+image);
I made a working jsfiddle showing this principle
http://jsfiddle.net/kasperfish/c72RT/4/
I recently needed to do the same thing. Basically I wrapped the image in a container div. within the container I've added a span element with my ajax loader gif embedded. this span has to be hidden initially but gets visible when an ajax request is made. The span gets removed when the image is fully loaded.
before ajax call
$('#your_image_container').find('span').show();
on success
$('#your_image').attr('src', 'your/image/url').load(function() {
$('#your_image_container').find('span').fadeOut();
});
I made a jsfiddle showing this principle
http://jsfiddle.net/kasperfish/c72RT/4/
Preload the image.
var product = "bowl";
var imageSrc = "dog.jpg";
var imgEl = $("#images img[data-product="+product+"]");
// show loading graphic only if it's needed
var timer = setTimeout(function(){
imgEl.attr("src", "/img/loading.gif");
},50);
// preload image
var img = new Image();
img.onload = function() {
clearTimeout(timer);
imgEl.attr("src",imageSrc);
}
img.src = imageSrc;
$img.attr("src", newImage);
if (!$img.get(0).complete) {
$img
.hide()
.after("<img src=throbber>")
.on("load", function () {
$(this).show().next().remove();
});
}
I want to make a JavaScript (jQuery), HTML gallery, which has like 60 pictures. I want them to load one by one. They are named 1.jpg, 2.jpg, 3.jpg... etc.
So I started with making a <img id="loader" style="display:none;" src=""/>
this contains the actually loading image, I give src for it with a script, then wait for it to load, then give the same src to its real destination:
<img class="active_pic" id="a1" src="img/load.gif"/>
When it is done I start loading the second picture. How do I do that?
I tried making it with a recursion, a function which calls itself, but it wouldn't wait for the image to load. I used this:
$("#loader").attr("src",pic);
$("#loader").ready(function(){$("#a"+active).attr("src",pic);
You can load your images from JS code with
var img = new Image();
img.onload = function() {
// do stuff after image is loaded
};
img.src = pic_url;
Update
JS function stub and usage example
<script>
function load_image(img_url)
{
// show overlay, spinner, whatever
var img = new Image();
img.onload = function() {
// add <img> tag
// hide overlay, spinner, whatever
};
img.src = img_url;
}
</script>
load image
you can use the onload event of the picture. But if your image is cached it will not work, so you can use it:
$('img[id|="a"]').each(function() {
if(this.complete) $(this).trigger('load');
});
var doOnLoad=function(){
var id=parseInt($(this).attr('id').substr(1));
$(this).removeClass("active_pic");
id++;
$('#a'+id).attr("src",pic[id]).one('load', doOnLoad).addClass("active_pic");
};
$(".active_pic").one('load', doOnLoad);
pic must be an array with id as key and src as value.