英文:
See through an absolute div on mouse move
问题
我正在尝试创建一种手电筒效果。我有一个 <div>
元素(固定位置,辐射背景,高度和宽度均为100%),遮挡了整个屏幕。在鼠标移动时,我想以手电筒的方式显示 <div>
后面的内容。
我找不到一种方法来使这个 <div>
的一部分变成透明(不透明度:0)。请参见附带的示例图像:
在鼠标悬停时,"Pré-inscription" 按钮会出现,辐射效果会保留在鼠标光标周围(截图上的圆圈太小,我将使它变大)。
编辑:为了更好地理解,我创建了一个 Codepen。如您所见,有一个固定的辐射渐变效果。红色圆圈应该允许看到渐变效果下的内容。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let magnifier = document.getElementById('magnifier')
let size = magnifier.offsetWidth
window.addEventListener('mousemove', (e) => {
magnifier.style.top = e.clientY - (size / 2)+'px'
magnifier.style.left = e.clientX - (size / 2)+'px'
})
<!-- language: lang-css -->
#overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgb(255,215,108);
background: radial-gradient(circle, rgba(251, 92, 3, 1) 0%, rgba(255,215,108,1) 100%);
}
#magnifier {
position: absolute;
background: red;
width: 7rem;
height: 7rem;
border-radius: 100%;
top: -20rem;
left: -20rem;
}
<!-- language: lang-html -->
<h1 class="text-7xl">Coming soon</h1>
<div id="overlay">
<div id="magnifier"></div>
</div>
<!-- end snippet -->
有什么想法吗?
谢谢。
Axel
英文:
I'm trying to create some kind of flashlight effect. I have a div (fixed, radiant background, height and width 100%) masking the entire screen. On mouse move, I'd like to reveal what is behind the div in a flashlight way.
I don't find a way to make a portion of this div transparent (opacity: 0). See example attached:
The "Pré-inscription" button appears on mouse over, the gradiant remains around the mouse cursor (the circle is too small on the screenshot, I will make it bigger)
EDIT: for a better understanding, I created a Codepen. As you can see, the fixed radiant gradiant. The red circle should allow to see through the gradiant.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let magnifier = document.getElementById('magnifier')
let size = magnifier.offsetWidth
window.addEventListener('mousemove', (e) => {
magnifier.style.top = e.clientY - (size / 2)+'px'
magnifier.style.left = e.clientX - (size / 2)+'px'
})
<!-- language: lang-css -->
#overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgb(255,215,108);
background: radial-gradient(circle, rgba(251, 92, 3, 1) 0%, rgba(255,215,108,1) 100%);
}
#magnifier {
position: absolute;
background: red;
width: 7rem;
height: 7rem;
border-radius: 100%;
top: -20rem;
left: -20rem;
}
<!-- language: lang-html -->
<h1 class="text-7xl">Coming soon</h1>
<div id="overlay">
<div id="magnifier"></div>
</div>
<!-- end snippet -->
Any idea?
Thanks
Axel
答案1
得分: 2
你可以尝试使用具有内部阴影的叠加效果,该效果会跟踪鼠标位置。
<div class="bigoverlay" id="bigoverlay">
<h1>Some hidden text</h1>
<div class="spotlight" id="spotlight"></div>
</div>
html, body {
margin: 0;
}
.bigoverlay {
width: 100dvw;
height: 100dvh;
overflow: hidden;
position: relative;
}
.spotlight {
border-radius: 50%;
width: 250vmax;
height: 250vmax;
box-shadow: 0 0 5vmax 110vmax inset black;
position: absolute;
z-index: -1;
left: -75vmax;
top: -75vmax;
}
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var eventDoc, doc, body;
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
const spotlight = document.getElementById("spotlight");
const boundingRect = spotlight.getBoundingClientRect();
spotlight.style.left = `${event.pageX - boundingRect.width/2}px`
spotlight.style.top = `${event.pageY - boundingRect.height/2}px`
}
以上是代码部分的翻译,不包括注释和代码块标记。
英文:
You could try something with an overlay with an inset shadow which tracks the mouse location.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var eventDoc, doc, body;
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
const spotlight = document.getElementById("spotlight");
const boundingRect = spotlight.getBoundingClientRect();
spotlight.style.left = `${event.pageX - boundingRect.width/2}px`
spotlight.style.top = `${event.pageY - boundingRect.height/2}px`
}
<!-- language: lang-css -->
html, body {
margin: 0;
}
.bigoverlay {
width: 100dvw;
height: 100dvh;
overflow: hidden;
position: relative;
}
.spotlight {
border-radius: 50%;
width: 250vmax;
height: 250vmax;
box-shadow: 0 0 5vmax 110vmax inset black;
position: absolute;
z-index: -1;
left: -75vmax;
top: -75vmax;
}
<!-- language: lang-html -->
<div class="bigoverlay" id="bigoverlay">
<h1>Some hidden text</h1>
<div class="spotlight" id="spotlight"></div>
</div>
<!-- end snippet -->
答案2
得分: -2
要创建一个在鼠标移动时揭示
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.addEventListener('mousemove', function(event) {
var flashlight = document.querySelector('.flashlight');
var x = event.clientX;
var y = event.clientY;
var radius = 100;
flashlight.style.backgroundPosition = x + 'px ' + y + 'px';
flashlight.style.backgroundSize = radius + 'px ' + radius + 'px';
});
<!-- language: lang-css -->
.flashlight {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: radial-gradient(circle, rgba(0, 0, 0, 0.8), transparent);
pointer-events: none;
z-index: 9999;
}
<!-- language: lang-html -->
<div class="flashlight"></div>
<!-- end snippet -->
在这段代码中,我们有一个固定的
我们使用JavaScript监听文档上的mousemove事件。在每次鼠标移动时,我们更新
英文:
To create a flashlight effect that reveals content behind a div on mouse move, you can use CSS and JavaScript. Here's an example
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.addEventListener('mousemove',
function(event) {
var flashlight =
document.querySelector('.flashlight');
var x = event.clientX;
var y = event.clientY;
var radius = 100;
flashlight.style.backgroundPosition =
x + 'px ' + y + 'px';
flashlight.style.backgroundSize =
radius + 'px ' + radius + 'px';
});
<!-- language: lang-css -->
.flashlight {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: radial-gradient(circle, rgba(0, 0, 0, 0.8), transparent);
pointer-events: none;
z-index: 9999;
}
<!-- language: lang-html -->
<div class="flashlight"></div>
<!-- end snippet -->
In this code, we have a fixed div with the class "flashlight" that covers the entire screen. It has a radial gradient background that starts with a dark color (rgba(0, 0, 0, 0.8)) and transitions to transparent, creating a flashlight effect.
We use JavaScript to listen for the mousemove event on the document. On each mouse move, we update the background position of the div to match the mouse coordinates (x, y). This makes the center of the flashlight follow the mouse cursor. We also set the size of the background to create the desired radius for the flashlight effect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- css
- html
- javascript
评论