Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Random thumbnail positions on hover of randomly positioned text

Featured Replies

I would like that when a user hovers different words, that a random arrangement of thumbnails fades-in.

Some caveats:

  • thumbs should stay fairly close to the corresponding text.
  • thumbs can overlap, but not fully.
  • both the text and the thumb positions should be random on every page refresh
  • text and thumbs shouldn't fall outside the screen.
  • should be scalable (i.e.. I will have max. 10 texts and max. 10 thumbs for every text. the thumbs could spread concentrically if need be)
 
And here's a fiddle of what I have so far: https://jsfiddle.net/2jddfsjad/ewk4dzLp/9/

See the image for an example of what I mean:

C9NwSq54gt.png

Is this for desktop? What happens on a phone where hovering isn’t practical. And there isn’t room. And the device most people now use.

That fiddle isn’t working for me, nothing happens when I hover or click.

  • Author

So I came across Jason Knight's random thumb position snippet over at https://forums.htmlhelp.com/ - a non-jquery effort!

Now, I am trying to make multiples of these grey divs, that also appear in random positions when a page is refreshed. Ideally it would be scalable (i.e.. I will have max 10 grey divs).


HTML

<div class="buttonThumbs">
  <button type="button">text</button>
  <img src="https://placeimg.com/100/100/animals" alt="describe this, alt is not optional">
  <img src="https://placeimg.com/100/100/people" alt="describe this, alt is not optional">
  <img src="https://placeimg.com/100/100/tech" alt="describe this, alt is not optional">
  <img src="https://placeimg.com/100/100/nature" alt="describe this, alt is not optional">
<!-- .buttonThumbs --></div>


CSS

html, body {
  height:100%;
}

body {
  display:flex;
  align-items:center;
  justify-content:center;
}

.buttonThumbs {
  position:relative;
  width:30em;
  height:20em;
  max-width:100%;
  max-height:100%;
  margin:auto; /* force scrollbars if too small */
  background:#EEE;
}

.buttonThumbs > * {
  position:absolute;
}

.buttonThumbs button {
  top:50%;
  left:50%;
  border:0;
  background:transparent;
  transform:translate(-50%, -50%);
}

.buttonThumbs img {
  width:100px;
  height:100px;
  opacity:0;
  transition:opacity 1s;
}

.buttonThumbs button:focus ~ img,
.buttonThumbs button:hover ~ img {
  opacity:1;
}

.buttonThumbs img:nth-of-type(1) {
  top:25%;
  left:25%;
}

.buttonThumbs img:nth-of-type(2) {
  top:25%;
  left:75%;
}

.buttonThumbs img:nth-of-type(3) {
  top:75%;
  left:25%;
}

.buttonThumbs img:nth-of-type(4) {
  top:75%;
  left:75%;
}

 

JS

{ // scope isolating block

    const
        origins = [
            { x : 0, y : 0 },
            { x : 0, y : 1 },
            { x : 1, y : 1 },
            { x : 1, y : 0 },
        ],
        buttonImgEvent = (e) => {
            let img = e.currentTarget.nextElementSibling;
            if (img) {
                let
                    originIndex = Math.floor(origins.length * Math.random());
                const
                    wrapper = e.currentTarget.closest(".buttonThumbs"),
                    halfWidth = wrapper.clientWidth / 2,
                    halfHeight = wrapper.clientHeight / 2;
                do {
                    img.style.left = (
                        Math.random() * (halfWidth - img.clientWidth) + 
                        origins[originIndex].x * halfWidth
                    ) + "px";
                    img.style.top = (
                        Math.random() * (halfHeight - img.clientHeight) +
                        origins[originIndex].y * halfHeight
                    ) + "px";
                    originIndex = (originIndex + 1) % origins.length;
                } while (img = img.nextElementSibling);
            }
        };

    for (const button of document.querySelectorAll(".buttonThumbs button")) {
        button.addEventListener("mouseover", buttonImgEvent);
        button.addEventListener("focus", buttonImgEvent);
    }


} // end scope isolation

Here is his codepen: https://codepen.io/jason-knight/pen/ZEjKvVd

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.