javascript - 模糊除css中选定区域以外的所有图像
问题描述
我的要求是模糊图像,同时在所选区域保持不模糊。我通过绝对定位在图像上放置了一个 div。图像仅在该区域可见,除此区域外,所有其他图像均模糊。
这是我的 DOM 结构
<div className="col-md-12 align-center mgb-30">
//within this area image is unblur while rest of image is blurred
<div className="venue-image-filter flex all-center color-white">
<h3 className="heading">Visible Area</h3>
</div>
<img src="https://i.picsum.photos/id/237/200/300.jpg" className="border-radius-10" alt="Venue" />
</div>
过滤器div的样式
.venue-image-filter
{
position:absolute;
top:50%;
left:50%;
width:300px;
height:200px;
transform:translate(-50%,-50%);
}
解决方案
这是一个小演示如何做到这一点:
document.querySelector('.image').addEventListener('mousemove', ({ offsetX, offsetY, target }) => {
const x = offsetX / target.clientWidth;
const y = offsetY / target.clientHeight;
target.style.setProperty('--x', `${x * 100}%`);
target.style.setProperty('--y', `${y * 100}%`);
});
.image {
width: 640px;
height: 200px;
position: relative;
}
.image::before, .image::after {
content: '';
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-image: var(--image);
}
.image::before {
filter: blur(10px);
}
.image::after {
clip-path: circle(20% at var(--x, 50%) var(--y, 50%));
z-index: 1;
}
<div class="image" style="--image: url(https://picsum.photos/id/862/640/200)"><div>
推荐阅读
- robots.txt - Robots.txt 显示有一个奇怪的链接
- directx - 使用采样器和直接索引访问之间的区别
- apache-spark - Spark SQL 以不同方式读取 parquet 表和 csv 表
- python - Kivy 无法使用 ButtonBehavior 向按钮添加文本
- python - Plotly dash 模板类似于 shinydashboard
- javascript - Phaser 使用 Firefox(隐藏的 Iframe)崩溃到 null window.computedStyle()
- angular - 在 ng-template 中使用 ng-content
- java - 如何用不同的语言使用黄瓜/小黄瓜?
- c# - C#大数据快速录入sql-server的咨询-bulk
- ipad - 检索到的资产是 nil ios cordova-plugin-photo-library ionic 1 ios