How to change background image randomly every load - html

How can I change the background image randomly from several sub folderseverytime the page is reloaded?
Imagine that the background file name is wallpaper.jpg
And in the folder images, I have subfolders and inside everyone of them I have one picture with the same name 'wallpaper.jpg'.
My goal is everytime we reload the page, the wallpaper changes randomly selecting one wallpaper.jpg from all the subfolders from the. Images folder.
Can you help me please?
Thanks

You will either have to make a naming pattern for the folders. Or put all the images in one folder with a naming pattern. I assume that your folder names are 0 1 2 3 4 5 6
<script type="text/javascript">
let folderCount= 5;
function refresh()
{
let rand= Math.ceil( Math.random() * folderCount);
document.body.background = images/'+num+'/wallpaper.jpg';
document.body.style.backgroundRepeat = "repeat";
}
</script>

You can do this simply with JavaScript like this:
let element = document.querySelector("body");
let arrImages = ["pathForImage1", "pathForImage2", "pathForImage3"];
let randomNumber = Math.floor(Math.random() * arrImages.length);
element.style.background = "url(" + arrImages[randomNumber] + ")";

Related

open random pdf file from folder with coding (HTML main code used)

I have made this website where I collect recipes I can eat.
I would like to add a 'random' feature, where one of the recipes opens and I don't have to chose what to eat.
My knowlege of coding is limit (like a year of highschool making a HTML website limited) but I do now it has to be possible. I also learned very briefly about a random number generator option in PHP and Javascript.
The website is coded with HTML and Notepad++.
The files are all pdf typed and like this, '1.pdf' '2.pdf'
You mean something like
<script>
const max = 15; // the largest number in your PDF filename
const min = 1;
const rnd = Math.floor(Math.random() * (max - min + 1) + min);
const pdf = document.createElement("embed");
pdf.width="800px"; // or what you want
pdf.height="2100px";
pdf.src=rnd+".pdf"
window.addEventListener("load",function() {
document.body.appendChild(pdf);
});
</script>

Using a session.storage variables in a link

I'm trying to make a simple configurator using session.storage where the user first chose the first attribute, which then will be stored in a session.storage variable (ie: onclick="sessionStorage.at1='red'")
then, then second and the third.
That works fine and I can display the different attributes as text - but I would like to show the corresponding image that I have made with the following syntax: at1at2at3.jpg (redlargewood.jpg) but I have no clue in how to actually use the variables like: <img src=sessionStorage.at1 + sessionStorage.at2 + sessionStorage.at3 + ".jpg " >
any help would be greatly appreciated
You can achieve this via javascript.Add id to your image tag and do the following in javascript:
let first = 'red';
let second = 'large';
let third = 'wood';
let pic = `${first}${second}${third}.jpg`
document.getElementById('demo').src = pic;

random default thumbnail for blogger

i'm currently learning html and css. I'm working on a blogger blog template.
My template has a front page of post thumbnails.
I have set a class for my default thumbnail for when my posts do not have an image and I have set the url in the css.
.altthumbnails {
background: url(myimagelocation.jpg);
}
is there any way I can make more than one default thumbnail? I would like to chose maybe three images to show at random when there is no post image.
Thanks for any replies
You can not do it only with pure css. But you can do it either by the server-side code you use or js which the one I suggest. Basicly one of the easiest solution for this is you can make different classes for each different image you wanna use, then you can produce a random number in js and use this number to apply different classes to your elements. Here's an example:
DEMO
In the example there are 5 different css class that supply 5 different background-image and all their names end with numbers 1 >> 5.
.thumb_1 { .. }
.thumb_2 { .. }
.thumb_3 { .. }
.thumb_4 { .. }
.thumb_5 { .. }
In javascript we're gonna loop through the objects, generate a random number 1 >> 5 and use this number to add a new class like this.
var div = document.getElementsByClassName("thumb");
//var rand = Math.floor(Math.random() * 5 ); // for just one image
for (var i = 0; i < div.length; i++){
var rand = Math.floor(Math.random() * 5 ); // for multiple images
div[i].className += " thumb_" + (rand + 1) ;
};

Problems with http://wall.plasm.it/examples/example-the-wall/ - Images

It is probably piece of cake for You, but I'm in the black hole now and I don't know what is the problem with The Wall. I would like to do something as www.wall.plasm.it/examples/example-the-wall/. Please - look at: www.megainstal.pl/ there is not any images. Could You help with fix it? I will be really grateful for any help/information.
Short story:
1) I download the files from github: https://github.com/plasm/the-wall
2) I changed only 3 files:
01-wall.css,
index.html,
01-wall.js
(case: basic)
3) in 01-wall.js I declared images:
var a = new Element("img[src=../../img/middle/"+counterFluid+".jpg]");
4) images that are: 1 to 76.jpg
Could you point me to where I making a mistake? If You going to see source files - I zip that's all in the one file: www.megainstal.pl/files.zip
I contact with author of plasm and he told me that I've mistakes in:
1) width and height size image - I improved
2) window.addEvent("domready", function() … not definite in 01-wall.js - I improved but I have doubt. Please - look at the code:
// Define 01-wall.js
var maxLength = 76; // Max Number images
var counterFluid = 1;
var wallFluid = new Wall("wall", {
"draggable":true,
"inertia":true,
"width":150,
"height":150,
"rangex":[-100,100],
"rangey":[-100,100],
callOnUpdate: function(items){
items.each(function(e, i){
var a = new Element("img[src=public/img/middle/"+counterFluid+".jpg]");
a.inject(e.node).fade("hide").fade("in");
//THERE
// Behaviour on click
a.addEvent("click", function(e){
// Get Movement
if( wall.getMovement() ){
//(move)
}else{
//(no move)
}
})
//THERE
counterFluid++;
// Reset counter
if( counterFluid > maxLength ) counterFluid = 1;
})
}
});
// Init Fluid Wall
wallFluid.initWall();
Please - help me with this troubles. I really need the Wall ;)

How to create a pagination photos?

I'd like to integrate a pagination in photo gallery project.
Ex: << previous 1 2 3 next >>
Let's say I have 13 photos and want to display on each page first 6 photos. So in total, I must have 3 pages of 6 photos each and each page number is clickable to display the maximum of 6 photos...
How would I proceed the right method?
Here's what I though:
var totalPhotos:uint;
var maxNumberThumbPerPage:uint = 6;
var totalPage:uint;
totalPhotos = tabPhoto.length;
totalPage = Math.ceil(totalPhotos/maxNumberThumbPerPage);
create a function that goes something like this
var imagesArray:Array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
function createPage($pageNum:int, $perPage:int = 6):Array{ // though vector is preferred
// imagesArray - the array holdig all the images
var iStart:int = $pageNum * $perPage;
var iEnd:int = ($pageNum + 1) * $perPage;
if (iEnd > imagesArray.length) { iEnd = imagesArray.length}
return imagesArray.slice(iStart, iEnd);
}
trace( createPage(0));
trace( createPage(1));
trace( createPage(2));
this will get you the content of each page, this is one of the trickier parts, but as you can see still pretty simple.
other part would be to create the navigation and create the rendering part