but I need to use getAttribute to retrieve the info?
You are using jQuery so just .data(..).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
I went ahead and done the jquery and also managed to do my idea of displaying the individual product on a template page. I'm buzzin right now.
I didn't use the data- attribute. I stuck with title. You're information won't go forgotten though. I added the extra field to my database. When retrieving the products on the products page, I fill the 'title' attribute of the image with that data. The jquery seems to work even without the array you guys created me. I put that code in there and suddenly realized I had done it without it.
Managed to get the text to align to the bottom of the image during the hover effect.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
People seem to pre-cache images by running a script in the head tag - that is, as the page is constructed - so that they are available as soon as the page is loaded.
I notice that we can also specify the size of the image(s):
Code:
image1 = new Image(480, 320);
I like this
Hi andrew, I have completed the products page and was wondering if you could help me with caching the images to help the loading of the page. My live version pretty much crashes my browser because of the amount/size of the images on my products page. I am going to send all my images through a site I found which optimizes the image file (I want them as small in size as possible) but I want to further help the loading by caching the files so the users have a better experience.
I myself got quite annoyed when the browser has 3 second delays even for scrolling down the page. I don't think many users would stay on the site or even bother looking at my products if this is happening to them.
Are there any good tutorials out there which can guide me through?
I might consider pre-loading just a few images at the beginning (script in the head tag) and, after the page has loaded, continue loading/caching the remaining images.
Code:
var myImgs = ['path/to/img1.jpg', 'path/to/img2.gif'];
var x = 0, y = 5;
function preload(imgs, x, y) {
var img;
for (var i = x; i < y; ++i) {
img = new Image(); // if you know the size of the images (and they are the same) use Image(200, 300);
img.src = imgs[i];
}
}
preload(myImgs, x, y);
window.onload = function () {
// or just put this code before the closing body tag
preload(myImgs, y+1, myImgs.length);
}
In terms of a tutorial, I would just be Googling for one, but perhaps someone else has a recommendation.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
.. and, not forgetting the best advice, don't put so many images on one page . If I hit such a page and my computer starts struggling, I hit the magic Back button.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Thank you for the code, unfortunately I couldn't get it to work at first so slightly modified it to simplify it for me. I have kept the preload function though.
So is this the preloading complete?
Here is my code:
Code:
function images(){
setInterval("change_image()",5000);
}
function preload(imgs, x, y) {
var img;
for (var i = x; i < y; ++i) {
img = new Image(); // if you know the size of the images (and they are the same) use Image(200, 300);
img.src = imgs[i];
console.log(imgs[i]);
console.log(img);
}
}
function change_image(){
var index = Math.floor((Math.random() * array_length));//generates a random number between 0-length of array
var image = "<img src='"+image_names[index]+"' alt='"+image_names[index]+"' title='"+image_names[index]+"' id='random_image' />";
document.getElementById("image_div").innerHTML = image;
document.getElementById("text").innerHTML = product_text;
}
var image_names = new Array("core/product_files/bench1.jpg","core/product_files/bench2.jpg","core/product_files/bench3.jpg","core/product_files/bench4.jpg","core/product_files/bench5.jpg",
"core/product_files/binstore1.jpg","core/product_files/binstore2.jpg","core/product_files/binstore3.jpg","core/product_files/binstore4.jpg","core/product_files/binstore5.jpg",
"core/product_files/birdhouse1.jpg","core/product_files/birdhouse2.jpg","core/product_files/birdhouse3.jpg","core/product_files/birdhouse4.jpg","core/product_files/birdhouse5.jpg",
"core/product_files/gate1.jpg","core/product_files/gate2.jpg","core/product_files/gate3.jpg","core/product_files/gate4.jpg","core/product_files/gate5.jpg",
"core/product_files/pethousing1.jpg","core/product_files/pethousing2.jpg","core/product_files/pethousing3.jpg","core/product_files/pethousing4.jpg","core/product_files/pethousing5.jpg",
"core/product_files/planter1.jpg","core/product_files/planter2.jpg","core/product_files/planter3.jpg","core/product_files/planter4.jpg","core/product_files/planter5.jpg",
"core/product_files/shed1.jpg","core/product_files/shed2.jpg","core/product_files/shed3.jpg","core/product_files/shed4.jpg","core/product_files/shed5.jpg",
"core/product_files/table1.jpg","core/product_files/table2.jpg","core/product_files/table3.jpg","core/product_files/table4.jpg","core/product_files/table5.jpg");
var product_text = "<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature"
product_text += "from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked"
product_text += "up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage</p>";
var array_length = image_names.length;//get the length of the array
var x = 0;
var y = array_length;
preload(image_names, x, y);
window.onload = images();
I have 2 console.logs() in the preload() function this is the output:
I remembered I created an array already but it was within a template html file, so I've moved it to an external file and added it to my index.php file.
Works a charm, I just don't know very much about caching so was wondering if this is complete now?
var image_names = new Array("core/product_files/bench1.jpg","core/product_files/bench2.jpg","core/product_files/bench3.jpg","core/product_files/bench4.jpg","core/product_files/bench5.jpg",
"core/product_files/binstore1.jpg","core/product_files/binstore2.jpg","core/product_files/binstore3.jpg","core/product_files/binstore4.jpg","core/product_files/binstore5.jpg",
"core/product_files/birdhouse1.jpg","core/product_files/birdhouse2.jpg","core/product_files/birdhouse3.jpg","core/product_files/birdhouse4.jpg","core/product_files/birdhouse5.jpg",
"core/product_files/gate1.jpg","core/product_files/gate2.jpg","core/product_files/gate3.jpg","core/product_files/gate4.jpg","core/product_files/gate5.jpg",
"core/product_files/pethousing1.jpg","core/product_files/pethousing2.jpg","core/product_files/pethousing3.jpg","core/product_files/pethousing4.jpg","core/product_files/pethousing5.jpg",
"core/product_files/planter1.jpg","core/product_files/planter2.jpg","core/product_files/planter3.jpg","core/product_files/planter4.jpg","core/product_files/planter5.jpg",
"core/product_files/shed1.jpg","core/product_files/shed2.jpg","core/product_files/shed3.jpg","core/product_files/shed4.jpg","core/product_files/shed5.jpg",
"core/product_files/table1.jpg","core/product_files/table2.jpg","core/product_files/table3.jpg","core/product_files/table4.jpg","core/product_files/table5.jpg");
I wouldn't have included the same directory in these repeatedly, could have fed it to the preload function as an additional argument and concatenated it to the image names:
This would make it easier to change later if you move the images to a different folder . But never mind.
Quote:
Works a charm, I just don't know very much about caching so was wondering if this is complete now?
If it works a charm then what else is there to know..? But I don't believe there is much more to this topic (apart from some server-side nonsense/trickery to persuade IE to use the cached image-store). Google is your pal if you want to study more.
I suggested loading some images at the beginning and the rest after the page has loaded. I suppose an optimal solution could be to load images at various stages during page-load, but I wouldn't worry about this.
Also.. some clever sites start loading some images that will be required on the next page, so that the transition between pages is smoother. But again, I wouldn't worry..
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
var image = "<img src='"+image_names[index]+"' alt='"+image_names[index]+"' title='"+image_names[index]+"' id='random_image' />";
It is preferable and more efficient to just change the src, title and id of an existing element, rather than repeatedly replacing it with a new element.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
When I said it works a charm I meant I was getting the values I wanted from doing the console.log() but I just didn't know if the process was complete or not. I will change the directory part.