January 10, 20233 yr 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:
January 10, 20233 yr 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.
January 10, 20233 yr Author yes on phone it is a completely different setup. fiddle: really? for me it works fine (Latest version of brave)
January 12, 20233 yr 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