gpt4 book ai didi

javascript - 如何使用 Tailwind 在 NextJS 中添加动态背景?

转载 作者:行者123 更新时间:2023-12-05 00:35:50 25 4
gpt4 key购买 nike

我有一张海报图片,我将用它作为背景图片。我从 MovieDb 获取它。我把它放在里面 类名 像这样类名={ bg-[url('${path}')] h-screen bg-cover bg-center text-white border-b-8 border-b-solid border-b-slate-400 } .但它抛出错误

./styles/globals.css:5:0
Module not found: Can't resolve './${path}'

Import trace for requested module:
./styles/globals.css
./pages/_app.js

https://nextjs.org/docs/messages/module-not-found

我想添加我的 浏览.tsx 文件也
import { GetStaticProps } from "next";
import React from "react";
import { getData } from "./api/randomMovie";
import { getServerSideProps } from "./gr-en";

export default function Browse({ movies }) {
console.log(movies);
const { poster_path } = movies;
const path =
"https://www.themoviedb.org/t/p/w1280_and_h720_multi_faces/rYFAvSPlQUCebayLcxyK79yvtvV.jpg";
console.log(path);
return (
<div
className={`bg-[url('${path}')] h-screen bg-cover bg-center text-white border-b-8 border-b-solid border-b-slate-400`}
>
<nav className="grid grid-cols-2 py-2 text-white bg-black/60">
<div className="flex col-span-full ml-10">
<div className="text-white mt-2 ">
<svg
xmlns="http://www.w3.org/2000/svg"
className=""
fill="none"
viewBox="0 0 300 81.387"
width="10vw"
height="8vh"
>
<g fill="#e50914">
<path d="M256.09 76.212c4.178.405 8.354.84 12.52 1.29l9.198-22.712 8.743 24.807c4.486.562 8.97 1.152 13.44 1.768l-15.328-43.501L299.996 0H287.01l-.135.186-8.283 20.455L271.32.003h-12.822l13.237 37.565-15.644 38.644zM246.393 75.322V0h-12.817v74.265c4.275.33 8.552.684 12.817 1.056M150.113 71.11c3.46 0 6.916.026 10.366.054V43.492h15.397V31.708H160.48v-19.91h17.733V0h-30.6v71.12c.831 0 1.666-.013 2.5-.01M110.319 71.83c4.27-.152 8.544-.28 12.824-.384V11.8h11.98V.003H98.339V11.8h11.982v60.03h-.002zM12.295 79.772V34.897L27.471 77.96c4.667-.524 9.341-1.017 14.028-1.483V.001H29.201v46.483L12.825.001H0v81.384h.077c4.063-.562 8.14-1.096 12.218-1.613M85.98 11.797V.001H55.377V75.202a1100.584 1100.584 0 0 1 30.578-2.211V61.184c-5.916.344-11.82.74-17.71 1.181V43.497h15.397V31.706H68.245V11.797H85.98zM203.614 60.62V-.003h-12.873v71.876c10.24.376 20.44.9 30.606 1.56V61.619c-5.9-.381-11.81-.712-17.733-1" />
</g>
</svg>
</div>
<ul className="flex ml-10 mt-4">
<li className="mx-2">Home</li>
<li className="mx-2">TV Shows</li>
<li className="mx-2">Movies</li>
<li className="mx-2">New & Popular</li>
<li className="mx-2">My List</li>
</ul>
</div>
<ul className="flex ml-10 pt-3 col-end-6 mr-10">
<li className="mx-2">Search</li>
<li className="mx-2">Kids</li>
<li className="mx-2">Bell</li>
<li className="mx-2">User</li>
<li className="mx-2">My List</li>
</ul>
</nav>
</div>
);
}

export const getStaticProps: GetStaticProps = async (context) => {
const movies = await getData();

console.log(movies);
return {
props: { movies },
};
};


那么在 className 中使用带有模板文字的动态路径的方法是什么?

最佳答案

您不能在 tailwind 中使用字符串连接来创建 CSS 类名 - see this answer .
为了克服顺风团队的这种有意设计,并且由于您的风格在每部电影中都是动态的 - 您应该将您的 background-image内联样式。

 <div styles={{backgroundImage:`url(${path})`}} classNames="....">

关于javascript - 如何使用 Tailwind 在 NextJS 中添加动态背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70583924/

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