Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 10 images in the body, and Randomize button. When i click on Randomize button, i need to get random height and width for that all images. How can i do that?

What I have tried:

I have tried like this`

 <style>
		.d1{
			float: left;
			margin: 1px;
			width: 130px;
			height: 130px;
			background-size: cover;
		}
		#btn3{
			width: 100px;
			height: 30px;
		}
	</style>

<div class="d1" style="background-image: url(Images/1.jpg)"></div>
	<div class="d1" style="background-image: url(Images/2.jpg)"></div>
	<div class="d1" style="background-image: url(Images/3.jpg)"></div>
	<div class="d1" style="background-image: url(Images/4.jpg)"></div>
	<div class="d1" style="background-image: url(Images/5.jpg)"></div>
	<div class="d1" style="background-image: url(Images/6.jpg)"></div>
	<div class="d1" style="background-image: url(Images/7.jpg)"></div>
	<div class="d1" style="background-image: url(Images/8.jpg)"></div>
	<div class="d1" style="background-image: url(Images/9.jpg)"></div>
	<div class="d1" style="background-image: url(Images/10.jpg)"></div>
	<button onclick="randomize()" id="btn3">Randomize</button>


function randomize(){
	var arr = document.querySelectorAll(".d1")
	var height = parseInt(Math.random()*1000);
	var width = parseInt(Math.random()*1000);
	for(var i = 0; i < arr.length; i++){
		arr[i].style.height = height;
		arr[i].style.width = width;
	}
}
Posted
Updated 11-Jul-18 22:36pm
Comments
Kornfeld Eliyahu Peter 12-Jul-18 4:14am    
Using image as background and displaying image as element are two different things...
F-ES Sitecore 12-Jul-18 4:35am    
What do you mean "get random width and height"? Do you mean get the width and height of a random image?

1 solution

Your question is vague. each image has fixed height and width, since they are not vector image. You can't get random height and width, but if you look for random number then it's a different topic. Take a look at this JavaScript random() Method[^]

And you have used image as background of each DIV. If so, you cannot set height or width to any of the image. You are going to set height and width for div. But, if you want to set your div height and width according to your images height and width, it would be different method that you are seeking for. The following code might help

HTML
<html>
	<script>
	function process(di) {
		var imgurl = di.style.backgroundImage.replace(/^url\(\"|\"\)$/g, "");
		var img = new Image();
		img.onload=function () {
			di.style.height=this.height;
			di.style.width=this.width;
		}
		img.src = imgurl;
	}
	window.onload = function () {
		var x=document.querySelectorAll(".nonsense")
		for(var i=0;i<x.length; i++) {
			process(x[i]);
		}
	};
	</script>
	<body>
		<div class="nonsense" style="background-image:url(orange.png)" >ORANGE COLOR</div>
		<div class="nonsense" style="background-image:url(red.png)" >red COLOR</div>
	</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900