简体   繁体   English

带有 css、html、javascript 的聚光灯效果

[英]Spotlight effect with css, html, javascript

I've designed a spotlight effect with css, html and java.我用 css、html 和 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.这是页面的链接: https ://civitonia.com/ 聚光灯跟随光标,但正如您所见,当您刷新页面或打开它时,聚光灯不在页面中心,但您可以看到它的右下角。 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: 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> 

You can set spotlight to center by setting its radial gradient at half of window's innerWidth and innerHeight on "DOMContentLoaded" .您可以通过在"DOMContentLoaded"上将其径向渐变设置为窗口的innerWidthinnerHeight的一半来将聚光灯设置为中心。

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.要将聚光灯绝对定位在手机屏幕上,可以使用matchMedia方法,检查屏幕的最小宽度。 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.在下面的代码中,如果最小宽度小于 800 像素,则聚光灯放置在屏幕中心,否则它将向其添加mousemove事件侦听器。

Also, you might want to handle window resize ie 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)此外,您可能想要处理窗口调整大小,即删除mousemove事件侦听器并在最小宽度变为 < 800px 时重新定位聚光灯,如果再次变为 > 800px 则重新附加侦听器(在变为 < 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM