Skip to content

Commit

Permalink
Improved the random images code
Browse files Browse the repository at this point in the history
  • Loading branch information
urigrupel committed May 27, 2024
1 parent eae3e94 commit 7e18816
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 35 deletions.
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ <h2>למה הגבת ככה?</h2>
<div class="action-container">
<button onclick="getReason()">קבל סיבה</button>
</div>
<img class="hidden reason-img" id="reason-img" alt="">
<img class="none reason-img" src="images/bear.webp" alt="Dancing Bear" width="450" height="500">
<img class="none reason-img" src="images/bling.webp" alt="Bling Bling" width="500" height="500">
<img class="none reason-img" src="images/stevie.webp" alt="Stevie Wonder" width="500" height="333">
<img class="none reason-img" src="images/frankenstein.webp" alt="The Frankenstein" width="401" height="500">
</section>
</main>

Expand Down
2 changes: 1 addition & 1 deletion script-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 11 additions & 32 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,15 @@ function findTargetLang(text) {
}

function getReason() {
const options = [
{
alt: 'Dancing Bear',
src: 'images/bear.webp',
width: 450,
height: 500,
},
{
alt: 'Bling Bling',
src: 'images/bling.webp',
width: 500,
height: 500,
},
{
alt: 'Stevie Wonder',
src: 'images/stevie.webp',
width: 500,
height: 333,
},
{
alt: 'The Frankenstein',
src: 'images/frankenstein.webp',
width: 401,
height: 500,
},
];
const img = document.getElementById('reason-img');
img.classList.remove('hidden');
index = randInt(0, options.length);
Object.entries(options[index]).forEach(([key, value]) => {
img.setAttribute(key, value);
});
const imgs = document.getElementsByClassName('reason-img');
const index = randInt(0, imgs.length);
let i = 0;
for (const img of imgs) {
if (i === index) {
img.classList.remove('none');
} else {
img.classList.add('none');
}
i++;
}
}
2 changes: 1 addition & 1 deletion styles-min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ h1 {
visibility: hidden;
}

.none {
display: none;
}

.tooltip {
opacity: 0;
padding: 0.5em 1em;
Expand Down

0 comments on commit 7e18816

Please sign in to comment.