在鼠标移动时查看绝对定位的 div。

huangapple go评论75阅读模式
英文:

See through an absolute div on mouse move

问题

我正在尝试创建一种手电筒效果。我有一个 <div> 元素(固定位置,辐射背景,高度和宽度均为100%),遮挡了整个屏幕。在鼠标移动时,我想以手电筒的方式显示 <div> 后面的内容。

我找不到一种方法来使这个 <div> 的一部分变成透明(不透明度:0)。请参见附带的示例图像:

在鼠标移动时查看绝对定位的 div。

在鼠标悬停时,"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:

在鼠标移动时查看绝对定位的 div。

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(&#39;magnifier&#39;)
let size = magnifier.offsetWidth

window.addEventListener(&#39;mousemove&#39;, (e) =&gt; {
  magnifier.style.top = e.clientY - (size / 2)+&#39;px&#39;
  magnifier.style.left = e.clientX - (size / 2)+&#39;px&#39;
  
})

<!-- 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 -->

&lt;h1 class=&quot;text-7xl&quot;&gt;Coming soon&lt;/h1&gt;

&lt;div id=&quot;overlay&quot;&gt;
  &lt;div id=&quot;magnifier&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- 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 &amp;&amp; event.target.ownerDocument) || document;
  doc = eventDoc.documentElement;
  body = eventDoc.body;

  event.pageX = event.clientX +
    (doc &amp;&amp; doc.scrollLeft || body &amp;&amp; body.scrollLeft || 0) -
    (doc &amp;&amp; doc.clientLeft || body &amp;&amp; body.clientLeft || 0);
  event.pageY = event.clientY +
    (doc &amp;&amp; doc.scrollTop  || body &amp;&amp; body.scrollTop  || 0) -
    (doc &amp;&amp; doc.clientTop  || body &amp;&amp; body.clientTop  || 0 );

  const spotlight = document.getElementById(&quot;spotlight&quot;);
  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

要创建一个在鼠标移动时揭示

后面内容的手电筒效果,你可以使用CSS和JavaScript。以下是一个示例:

<!-- 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 -->

在这段代码中,我们有一个固定的

,类名为"flashlight",覆盖整个屏幕。它具有径向渐变背景,从深色(rgba(0, 0, 0, 0.8))开始渐变到透明,从而创建手电筒效果。

我们使用JavaScript监听文档上的mousemove事件。在每次鼠标移动时,我们更新

的背景位置以匹配鼠标坐标(x,y)。这使得手电筒的中心跟随鼠标光标移动。我们还设置背景的大小以创建所需的手电筒效果半径。

英文:

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(&#39;mousemove&#39;,
  function(event) {
    var flashlight =
      document.querySelector(&#39;.flashlight&#39;);
    var x = event.clientX;
    var y = event.clientY;
    var radius = 100;
    flashlight.style.backgroundPosition =
      x + &#39;px &#39; + y + &#39;px&#39;;
    flashlight.style.backgroundSize =
      radius + &#39;px &#39; + radius + &#39;px&#39;;
  });

<!-- 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 -->

&lt;div class=&quot;flashlight&quot;&gt;&lt;/div&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年6月2日 14:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定