gpt4 book ai didi

html - 如何用 CSS 制作这种照明效果

转载 作者:行者123 更新时间:2023-12-03 07:26:30 26 4
gpt4 key购买 nike

我想模拟一个“扫描”灯,它将显示框中的单词,这是我现在的代码:

const e = document.getElementsByClassName('scan')[0];
document.onmousemove = function(event){
e.style.left = `${event.clientX}px`;
};
*{
margin: 0;
padding: 0;
}

html, body{
width: 100%;
min-height: 100vh;
overflow-x: hidden;

display: flex;
}

.banner{
width: 100vw;
height: 100vh;

display: flex;
flex-grow: 1;
flex-direction: row;
align-items: center;
background-color: #031321;
}

.banner .scan{
width: 7px;
height: 80%;

position: absolute;
left: 30px;
z-index: 3;

transition: left 50ms ease-out 0s;

border-radius: 15px;
background-color: #fff;
box-shadow:
0 0 15px 5px #fff, /* inner white */
0 0 35px 15px #008cff, /* inner blue */
0 0 350px 20px #0ff; /* outer cyan */
}

.banner .description{
width: 100%;
color: white;
font-size: 3em;
text-align: center;

-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
<div class="banner">
<div class="scan"></div>
<div class="description">
Just trying something
</div>
</div>

这个想法是在 .description中显示的话div 根据扫描灯的位置。如果可能的话,我只想使用 CSS 来实现这种效果,并且只使用 JavaScript 来移动扫描(这将进一步成为 CSS 动画)。我尝试使用一些伪元素,但效果不佳。 Here's这个动画应该如何工作的一个例子。

最佳答案

您可以使用带有渐变背景的透明文本。我用过 background-attachment: fixed 和一个 CSS 变量来控制背景位置。
您可以增加背景大小(在本例中为 500 像素)以增加过渡平滑度。

const e = document.getElementsByClassName('scan')[0];
const hidden = document.getElementsByClassName('hidden')[0];

document.onmousemove = function(event) {
e.style.left = `${event.clientX}px`; // ↓ background width (500px) / 2
hidden.style.setProperty("--pos", `${event.clientX - 250}px`);
};
* {
margin: 0;
padding: 0;
}

html,
body {
width: 100%;
min-height: 100vh;
overflow-x: hidden;
display: flex;
}

.banner {
width: 100vw;
height: 100vh;
display: flex;
flex-grow: 1;
flex-direction: row;
align-items: center;
background-color: #031321;
}

.banner .scan {
width: 7px;
height: 80%;
position: absolute;
left: 30px;
z-index: 3;
transition: left 50ms ease-out 0s;
border-radius: 15px;
background-color: #fff;
box-shadow: 0 0 15px 5px #fff, /* inner white */
0 0 35px 15px #008cff, /* inner blue */
0 0 350px 20px #0ff;
/* outer cyan */
}

.banner .description {
width: 100%;
color: white;
font-size: 3em;
text-align: center;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}

.hidden {
background: radial-gradient(dodgerblue 10%, #031321 50%) var(--pos) 50% / 500px 500px no-repeat fixed;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<div class="banner">
<div class="scan"></div>
<div class="description">
Just <span class="hidden">hidden</span> something
</div>
</div>

这是另一个带有很长段落和多个隐藏文本的示例。我们在这里控制 X 轴和 Y 轴。

const hiddens = document.querySelectorAll('.hidden');

document.addEventListener("mousemove", e => {
hiddens.forEach(p => {
// ↓ background width (400px) / 2
p.style.setProperty("--posX", `${e.clientX - 200}px`);
p.style.setProperty("--posY", `${e.clientY - 200}px`);
});
});
html,
body {
width: 100%;
overflow-x: hidden;
}

body {
background: #031321;
color: #fff;
font-size: 3rem;
line-height: 1.5;
padding: 20px;
box-sizing: border-box;
}

.hidden {
background: radial-gradient(dodgerblue 10%, #031321 50%) var(--posX) var(--posY) / 400px 400px no-repeat fixed;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Lorem ipsum dolor sit amet, <span class="hidden">consectetur</span> adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <span class="hidden">Excepteur sint</span> occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum
dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea <span class="hidden">commodo</span> consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

关于html - 如何用 CSS 制作这种照明效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64764879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com