CSS 使用 Javascript mousemove 和 getBoundingClientRect() 在几个元素上的 Clip-Path
CSS Clip-Path on several elements with Javascript mousemove and getBoundingClientRect()
我正在尝试在跟随光标的元素内创建一个剪辑路径。 Clip-Path 应该只出现在元素内部,因此我使用 getBoundingClientRect()。它适用于第一个元素,但我想在多个元素上使用 运行(使用 class .banner)。
我把所有的代码都做了一个代码笔,希望有人能弄明白...:[=14=]
https://codepen.io/gerrit-dolde/pen/oNpXLov
<div class="container">
<section class="underground"></section>
<section class="banner"></section>
</div>
<div class="container">
<section class="underground"></section>
<section class="banner"></section>
</div>
:root {
--x: 0px;
--y: 0px;
}
*{
margin:0;
padding:0;
}
body {
min-height: 100vh;
background:#000;
overflow-x: hidden;
}
.container {
max-width: 800px;
margin: 150px auto;
box-sizing: border-box;
position: relative;
height: 400px;
padding: 0;
}
.underground {
position: absolute;
width: 800px;
height: 400px;
background: url('https://upload.wikimedia.org/wikipedia/commons/e/e7/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006.jpg');
background-size: cover;
filter: grayscale(100%) sepia(0) brightness(70%);
}
.banner {
position: absolute;
width: 800px;
height: 400px;
background: url('https://upload.wikimedia.org/wikipedia/commons/e/e7/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
clip-path: circle(0px at var(--x) var(--y)) !important;
transition: 0.15s;
z-index: 200;
filter: grayscale(0%);
}
.container:hover .banner {
clip-path: circle(200px at var(--x) var(--y)) !important;
transition: 0.15s;
}
const container = document.querySelector(".container");
const banner = document.querySelector(".banner");
container.addEventListener("mousemove", function(e) {
var xPosition = e.clientX - container.getBoundingClientRect().left;
var yPosition = e.clientY - container.getBoundingClientRect().top;
banner.style.setProperty('--x', xPosition + "px");
banner.style.setProperty('--y', yPosition + "px");
}
);
您应该分别在每个容器上设置事件
为您修复:
const container = document.querySelectorAll(".container");
container.forEach(elm=>elm.addEventListener("mousemove", function(e) {
const cur= e.currentTarget;
const banner = cur.querySelector(".banner");
var xPosition = e.clientX - cur.getBoundingClientRect().left;
var yPosition = e.clientY - cur.getBoundingClientRect().top;
banner.style.setProperty('--x', xPosition + "px");
banner.style.setProperty('--y', yPosition + "px");
}
));
我正在尝试在跟随光标的元素内创建一个剪辑路径。 Clip-Path 应该只出现在元素内部,因此我使用 getBoundingClientRect()。它适用于第一个元素,但我想在多个元素上使用 运行(使用 class .banner)。
我把所有的代码都做了一个代码笔,希望有人能弄明白...:[=14=]
https://codepen.io/gerrit-dolde/pen/oNpXLov
<div class="container">
<section class="underground"></section>
<section class="banner"></section>
</div>
<div class="container">
<section class="underground"></section>
<section class="banner"></section>
</div>
:root {
--x: 0px;
--y: 0px;
}
*{
margin:0;
padding:0;
}
body {
min-height: 100vh;
background:#000;
overflow-x: hidden;
}
.container {
max-width: 800px;
margin: 150px auto;
box-sizing: border-box;
position: relative;
height: 400px;
padding: 0;
}
.underground {
position: absolute;
width: 800px;
height: 400px;
background: url('https://upload.wikimedia.org/wikipedia/commons/e/e7/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006.jpg');
background-size: cover;
filter: grayscale(100%) sepia(0) brightness(70%);
}
.banner {
position: absolute;
width: 800px;
height: 400px;
background: url('https://upload.wikimedia.org/wikipedia/commons/e/e7/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
clip-path: circle(0px at var(--x) var(--y)) !important;
transition: 0.15s;
z-index: 200;
filter: grayscale(0%);
}
.container:hover .banner {
clip-path: circle(200px at var(--x) var(--y)) !important;
transition: 0.15s;
}
const container = document.querySelector(".container");
const banner = document.querySelector(".banner");
container.addEventListener("mousemove", function(e) {
var xPosition = e.clientX - container.getBoundingClientRect().left;
var yPosition = e.clientY - container.getBoundingClientRect().top;
banner.style.setProperty('--x', xPosition + "px");
banner.style.setProperty('--y', yPosition + "px");
}
);
您应该分别在每个容器上设置事件 为您修复:
const container = document.querySelectorAll(".container");
container.forEach(elm=>elm.addEventListener("mousemove", function(e) {
const cur= e.currentTarget;
const banner = cur.querySelector(".banner");
var xPosition = e.clientX - cur.getBoundingClientRect().left;
var yPosition = e.clientY - cur.getBoundingClientRect().top;
banner.style.setProperty('--x', xPosition + "px");
banner.style.setProperty('--y', yPosition + "px");
}
));