Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对具有固定位置的背景图像应用边距_Javascript_Css - Fatal编程技术网

Javascript 对具有固定位置的背景图像应用边距

Javascript 对具有固定位置的背景图像应用边距,javascript,css,Javascript,Css,我有一个背景图像,我想覆盖我的整个屏幕减去标题。我也不希望我的背景图像滚动。当用户滚动内容时,它不应移动 因此,我使用以下内容放置我的背景: <div className={style.app} style={{ backgroundImage: `url(${HomePhoto})`, backgroundRepeat: "no-repeat", backgroundAttachment:

我有一个背景图像,我想覆盖我的整个屏幕减去标题。我也不希望我的背景图像滚动。当用户滚动内容时,它不应移动

因此,我使用以下内容放置我的背景:

    <div
      className={style.app}
      style={{
        backgroundImage: `url(${HomePhoto})`,
        backgroundRepeat: "no-repeat",
        backgroundAttachment: "fixed",
      }}
    ></div>

但是,除非我删除
背景附件:“固定”
,否则不会应用
页边距顶部。是否有一种方法可以实现这两种样式?

您可以使用
backgroundPosition
属性,该属性允许您在其容器中移动背景图像:


您可以使用
backgroundPosition
属性,该属性允许您在其容器中移动背景图像:



谢谢这似乎行得通谢谢这似乎行得通
    height: 100vh;
    width: 100vw;
    margin-top: 4rem;
    background-size: 100% 100%;
<div
  className={style.app}
  style={{
    backgroundImage: `url(${HomePhoto})`,
    backgroundRepeat: "no-repeat",
    backgroundAttachment: "fixed",
    backgroundPosition: "100px 0", // or vh
  }}
></div>