React+SCSS实现Netflix背景模糊失败,求代码排查修复
修复Netflix风格背景模糊效果问题
问题描述
尝试复刻Netflix首页的Featured区域背景效果,但当前实现的背景仅显示原图,没有模糊效果和渐变层次感,和目标效果差距明显。
当前代码
Featured.js
import React from 'react' import './featured.scss' import background from '../../assets/background1.png' import info from '../../assets/info1.png' import { InfoOutlined, PlayArrow } from "@material-ui/icons"; const Featured = () => { return ( <div className='featured'> <img className='background' src={background} alt="" /> {/* <div className="shadow"></div> */} <div className="info"> <img src={info} alt="" /> {/* <span className="desc"> Based on historical events, this series dramatizes the story of Queen Elizabeth II and the political and personal events that have shaped her reign. </span> */} <div className="buttons"> <button className="play"> <PlayArrow className='arrow' /> <span>Play</span> </button> <button className="more"> <InfoOutlined /> <span>More Info</span> </button> </div> </div> </div> ) } export default Featured
featured.scss
.featured{ height: 110vh; background: linear-gradient(to bottom 0%, rgb(0,0,0,0.3) 50%); box-shadow: 0px 0px 20px 0px linear-gradient(rgb(0,0,0,0.3)); position: relative; img.background{ height: 100%; width: 100%; object-fit: cover; background: linear-gradient(to top,transparent 0%, rgb(0,0,0,0.3) 50%); z-index: -999; } .info { width: 35%; position: absolute; left: 50px; bottom: 200px; color: white; display: flex; flex-direction: column; margin-top: -40px; img { width: 300px; position: relative; // z-index: 99; } .desc { margin: 20px 0px; z-index: 99; } .buttons { display: flex; z-index: 99; margin-top: 20px; button { padding: 5px 20px; border: none; border-radius: 5px; display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 500; margin-right: 10px; cursor: pointer; &.play { background-color: white; color: var(--main-color); // font-size: 30px; .arrow{ font-size: 30px; font-weight: bold; } } &.more { background-color: gray; color: white; } span { margin-left: 5px; } } } } }
解决方案及修改说明
原代码的核心问题是直接使用img标签作为背景,无法正确添加模糊滤镜,且渐变叠加方式错误。以下是修改后的完整代码:
修改后的Featured.js
import React from 'react' import './featured.scss' import background from '../../assets/background1.png' import info from '../../assets/info1.png' import { InfoOutlined, PlayArrow } from "@material-ui/icons"; const Featured = () => { return ( <div className='featured'> {/* 替换img为div,用背景图方式实现模糊 */} <div className='background' style={{ backgroundImage: `url(${background})` }}></div> {/* 新增渐变遮罩层 */} <div className="overlay"></div> <div className="info"> <img src={info} alt="" /> {/* <span className="desc"> Based on historical events, this series dramatizes the story of Queen Elizabeth II and the political and personal events that have shaped her reign. </span> */} <div className="buttons"> <button className="play"> <PlayArrow className='arrow' /> <span>Play</span> </button> <button className="more"> <InfoOutlined /> <span>More Info</span> </button> </div> </div> </div> ) } export default Featured
修改后的featured.scss
.featured{ height: 110vh; position: relative; overflow: hidden; /* 裁剪模糊后超出容器的部分 */ .background{ position: absolute; top: 0; left: 0; height: 100%; width: 100%; background-size: cover; background-position: center; filter: blur(8px); /* 添加模糊效果 */ transform: scale(1.1); /* 缩放解决模糊后边缘空白问题 */ z-index: -2; } .overlay{ position: absolute; top: 0; left: 0; width: 100%; height: 100%; /* 模拟Netflix的渐变遮罩,从透明到深黑 */ background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.8) 100%); z-index: -1; } .info { width: 35%; position: absolute; left: 50px; bottom: 200px; color: white; display: flex; flex-direction: column; img { width: 300px; } .desc { margin: 20px 0px; } .buttons { display: flex; margin-top: 20px; button { padding: 5px 20px; border: none; border-radius: 5px; display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 500; margin-right: 10px; cursor: pointer; &.play { background-color: white; color: var(--main-color); .arrow{ font-size: 30px; font-weight: bold; } } &.more { /* 优化按钮背景色,更贴近Netflix */ background-color: rgba(109, 109, 110, 0.7); color: white; } span { margin-left: 5px; } } } } }
关键修改点
- 将
img标签替换为div,通过backgroundImage设置背景,方便添加模糊滤镜 - 给背景容器添加
filter: blur(8px)实现模糊效果,同时用transform: scale(1.1)解决模糊后边缘出现空白的问题 - 新增
overlay渐变遮罩层,模拟Netflix的渐变效果,增强画面层次感 - 调整各层级的
z-index,确保内容区域在最上层 - 优化"More Info"按钮的背景色,更贴近Netflix的样式
内容的提问来源于stack exchange,提问作者Samuel Paschalson
相关产品推荐
相关产品推荐

