-1

I've designed a spotlight effect with css, html and java. This is the link to the page: https://civitonia.com/ The spotlight follows the cursor, but as you can see, when you refresh the page or when you open it, the spotlight is not center on the page but you can see it a the bottom right. I'd like to set a "center point" or something similar that send the spotlight effect at the center of the page every time I refresh it/open it. Is it possible?

CSS:

.spotlight {
    position: fixed;
    width: 100%;
    height: 100%;
    pointer-events: none;
    background-image: radial gradient(circle, transparent 160px, rgba(0, 0, 0, 0)
    200px);
    }

SCRIPT:

<script>
    window.addEventListener("DOMContentLoaded", () => {
        const spotlight = document.querySelector('.spotlight');
        let spotlightSize = 'transparent 160px, rgba(0, 0, 0, 0.85) 200px)';
        window.addEventListener('mousemove', e => updateSpotlight(e));
        function updateSpotlight(e) {
            spotlight.style.backgroundImage = `radial-gradient(circle at ${e.pageX / window.innerWidth * 100}% ${e.pageY / window.innerHeight * 100}%, ${spotlightSize}`;
        }
    });
    </script> 

1 Answers1

1

You can set spotlight to center by setting its radial gradient at half of window's innerWidth and innerHeight on "DOMContentLoaded".

window.addEventListener("DOMContentLoaded", () => {
        const spotlight = document.querySelector('.spotlight');
        let spotlightSize = 'transparent 160px, rgba(0, 0, 0, 0.85) 200px)';
        spotlight.style.backgroundImage = `radial-gradient(circle at ${window.innerWidth/2}px ${window.innerHeight/2}px, ${spotlightSize}`;
        window.addEventListener('mousemove', e => updateSpotlight(e));
        function updateSpotlight(e) {
            spotlight.style.backgroundImage = `radial-gradient(circle at ${e.pageX / window.innerWidth * 100}% ${e.pageY / window.innerHeight * 100}%, ${spotlightSize}`;
        }
    });

Snippet:

window.addEventListener("DOMContentLoaded", () => {
  const spotlight = document.querySelector('.spotlight');
  let spotlightSize = 'transparent 160px, rgba(0, 0, 0, 0.85) 200px)';
  spotlight.style.backgroundImage = `radial-gradient(circle at ${window.innerWidth/2}px ${window.innerHeight/2}px, ${spotlightSize}`;
  window.addEventListener('mousemove', e => updateSpotlight(e));

  function updateSpotlight(e) {
    spotlight.style.backgroundImage = `radial-gradient(circle at ${e.pageX / window.innerWidth * 100}% ${e.pageY / window.innerHeight * 100}%, ${spotlightSize}`;
  }
});
.spotlight {
  position: fixed;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background-image: radial-gradient(circle, transparent 160px, rgba(0, 0, 0, 0) 200px);
}
<div class="spotlight"></div>

To absolutely position the spotlight on mobile screen can use matchMedia method, to check the min width of the screen. Based on that, you can attach the mouse move event listener to it. In the code below, if min width is less than 800px, the spotlight is placed at center of screen else it will add the mousemove event listener to it.

Also, you might want to handle window resize i.e remove the mousemove event listener and reposition the spotlight if min width becomes < 800px and reattach the listener if becomes > 800px again (after becoming < 800px)

window.addEventListener("DOMContentLoaded", ()=>{
  const spotlight = document.querySelector('.spotlight');
  let spotlightSize = 'transparent 160px, rgba(0, 0, 0, 0.85) 200px)';
  //attach mousemove event listener if media query matches. 
  if (matchMedia('only screen and (min-width: 800px)').matches) {
    window.addEventListener('mousemove', updateSpotlight);

    function updateSpotlight(e) {
      spotlight.style.backgroundImage = `radial-gradient(circle at ${e.pageX / window.innerWidth * 100}% ${e.pageY / window.innerHeight * 100}%, ${spotlightSize}`;
    }
    
  } else {
    spotlight.style.backgroundImage = `radial-gradient(circle at ${window.innerWidth/2}px ${window.innerHeight/2}px, ${spotlightSize}`;
  }
});
.spotlight {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background-image: radial-gradient(circle at 50% 50%, transparent 160px, rgba(0, 0, 0, 0.85) 200px);
}

@media only screen and (min-width: 800px) {
  .spotlight {
    position: fixed;
    background-image: radial-gradient(circle at 50% 50%, transparent 160px, rgba(0, 0, 0, 0) 200px);
  }
}
<div class="spotlight"></div>
TechySharnav
  • 4,869
  • 2
  • 11
  • 29
  • Thank you!! Is there a way to make it fixed (position) and not movable just in the mobile version? And to keep it following the cursor in the web version – Emanuele Pesa Jul 16 '22 at 12:30
  • You can use media query in css to absolutely position the spotlight on Mobile screen. Then use JavaScript's [`matchMedia()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) to conditionally add the above event listener to `window`. – TechySharnav Jul 16 '22 at 14:20
  • could you upload the code above if it's possible for you? I'll understand better what you saying! Sorry I'm just learning – Emanuele Pesa Jul 16 '22 at 15:03
  • Should I add @media screen and (max-width:700px) { .spotlight { position: absolute; } and then add matchMedia(.spotlight) to the script section? – Emanuele Pesa Jul 16 '22 at 15:47
  • yeah, but it would be `min-width`. Also, it should be `matchMedia('(min-width:800px)')`. I am including a snippet in the answer. You can refer to [this](https://stackoverflow.com/questions/6370690/media-queries-how-to-target-desktop-tablet-and-mobile) answer, for breakpoints for media queries. – TechySharnav Jul 17 '22 at 01:58
  • – Emanuele Pesa Jul 17 '22 at 16:18
  • If @media screen and (min-width: 800px) { .spotlight { position:absolute; then the spotlight disappear from both versions. If I use position fixed there too it disappear just in the mobile version! – Emanuele Pesa Jul 17 '22 at 17:04
  • Sorry, what I mean is that it isn't working as you are saying! With the second snippet the spotlight goes away in the mobile version have a look if you want www.civitonia.com – Emanuele Pesa Jul 18 '22 at 08:37
  • Please Check updated Answer. – TechySharnav Jul 18 '22 at 10:22
  • It's not working! I've updated the snippet to the website so you can check what happen in the mobile version! – Emanuele Pesa Jul 18 '22 at 11:09
  • 1
    In that case, I would suggest you to open another question on this website regarding the issue. – TechySharnav Jul 18 '22 at 11:49