ATTENTION!! ... We just launched this forum recently, so not much is on it yet. JOIN TODAY!

Simple Random Image JS Code

Everything that has to to do with programming using Javascript.
Post Reply
User avatar
PixelinkMedia
Site Admin
Posts: 50
Joined: Mon Apr 08, 2019 2:20 pm
Contact:

Simple Random Image JS Code

Post by PixelinkMedia » Sat May 04, 2019 10:15 pm

Here is simple Javascript code to randomly choose an image each time the script/page is loaded.

JS CODE:
RandomImage.js (filename)

Code: Select all



## ENTER TEXT HERE
$img[0] = '<img src="img01.jpg" alt="Image 1" class="imgSize"></img>';
$img[1] = '<img src="img02.jpg" alt="Image 1" class="imgSize"></img>';
$img[2] = '<img src="img03.jpg" alt="Image 1" class="imgSize"></img>';

## ENTER WEIGHTS HERE
$Weight[0]=99;
$Weight[1]=99;
$Weight[2]=99;

## CHOOSE RANDOM TEXT
$sum =0;
for($i=0;$i<count($Weight);$i++)
$sum+=$Weight[$i];
$ShowImg = rand(0, $sum - 1);
//$ShowImg = rand(0, $sum);

for($i=0;$i<count($Weight);$i++)
{
    if($ShowImg<=$Weight[$i])
    {
        $ShowImg=$i;
    	break;
    }
    	else
    	$ShowImg-=$Weight[$i];
    }
    echo $img[$ShowImg];

My Site: Pixelink Media • Windows 7 • 10G RAM • NIV-GForce GT 640 Graphics Card
Affinity (Designer/Photo/ • CorelDraw • Blender • HitFilm • Saola (web animation/interactive) • MagicaVoxel
Lazarus (Free Pascal) • .NET Framework (discontinued for now) • PHP • Html/CSS • Javascript • mySQL/SQLite
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest