I'm not sure I can use jQuery for this, since it waits for the page to be ready before firing, but I could be wrong...
Basically, I'd love to change a class-name to one of a few specified names, randomly, every time the document loads. For instance...
.photoSet1 {background-image:url(path/to/photo1.jpg);}
.photoSet2 {background-image:url(path/to/photo2.jpg);}
.photoSet3 {background-image:url(path/to/photo3.jpg);}
<div class="photoSet1">
.. stuff ..
</div>
Every the page is loaded, I'd like to change the div element's class to either 'photoSet1', 'photoSet2', or 'photoSet3', randomly ... then, naturally, based on the class that's pumped into the <div> element, the background image will change.
Can this be done using jQuery? Either way, what's the slimmest JavaScript needed to accomplish this?
Thanks all!
Changing a document's class on page load
Whether you're a seasoned veteran or a struggling beginner, Web Radiance is the web development and web design forum for you. You'll find answers to all your HTML, CSS, SEO, and Programming needs. Pull up a chair and stay awhile.
Page 1 of 1
Changing a document's class on page load
#2
Posted 08 September 2008 - 11:28 PM
I'm not a jquery guy. I have used it a little, and I can kind of use it, but I am by now means a pro. I can give you a few pointers though. First, you want to generate a random number between 1 and however many potential images you have. This way you can use the random number, and assign the class name using that random number. To generate a random number, you can use this formula:
Where A is the top number in the range, and B is the bottom number in the range. So for example if you want a number between 1 and 3, you would use:
Then, you want to attach that random number to the rest of the class name.
From there, you can use the jquery functions to assign className to the class of the element.
As for waiting until the document loads, I believe that it is possible to target a specific element onload using jquery, though I don't remember exactly how to do it. Maybe someone else can help out a little more with that.
Are there any jquery forums out there?
edit: apparently there is! http://www.jqueryhelp.com/
var rand_no = Math.floor((A-(B-1))*Math.random()) + B;
Where A is the top number in the range, and B is the bottom number in the range. So for example if you want a number between 1 and 3, you would use:
var rand_no = Math.floor((3)*Math.random()); // this comes from var rand_no = Math.floor((3-0)*Math.random()) + 0;
Then, you want to attach that random number to the rest of the class name.
var className = 'photoSet' + rand_no
From there, you can use the jquery functions to assign className to the class of the element.
As for waiting until the document loads, I believe that it is possible to target a specific element onload using jquery, though I don't remember exactly how to do it. Maybe someone else can help out a little more with that.
Are there any jquery forums out there?
edit: apparently there is! http://www.jqueryhelp.com/
<a href="http://www.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
#3
Posted 09 September 2008 - 06:49 AM
Looking around a little bit more, I discovered that there is a way to fire off jQuery before the page finishes loading:
Instead of waiting for the page to load, this simply waits for the DOM to load. Knowing that the DOM finishes loading before all images (and, I think, links to CSS files), I should be OK using jQuery.
I just need to apply what you've suggested to this ... I think. I'm so bad with JavaScript.
$(document).ready(function(){
Your code here
});
Instead of waiting for the page to load, this simply waits for the DOM to load. Knowing that the DOM finishes loading before all images (and, I think, links to CSS files), I should be OK using jQuery.
I just need to apply what you've suggested to this ... I think. I'm so bad with JavaScript.
#4
Posted 09 September 2008 - 07:14 AM
Got it to work, thanks to your assistance haku:
$(document).ready(function(){
var rand_no = Math.floor((4-1)*Math.random()) + 1;
var className = 'photoSet' + rand_no;
$("#homePhotoBoard").addClass(className);
});
#5
Posted 09 September 2008 - 07:57 AM
Nice! Glad to have given you a nudge in the right direction. Good job on figuring it out yourself.
<a href="http://www.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
#6
Posted 17 February 2009 - 04:38 AM
I want to learn java script . where can I learn it tell me please.if you have knowledge about this.
#7
Posted 06 April 2011 - 07:52 AM
try www.w3schools.com..
it's a pretty organized website meant for tutoring web developers like us..
it's a pretty organized website meant for tutoring web developers like us..
Share this topic:
Page 1 of 1


Help
This topic is locked

MultiQuote









