html - 如何在html中为固定div创建视差效果

标签 html css

我创建了一个带有固定侧边栏的 html 页面,该侧边栏也有背景图片。我试图在背景图像中加入视差效果,但我做不到。我尝试将背景附件更改为固定和滚动,但由于 div 是固定的,视差效果对我来说不起作用。请给我一个解决方案。 这是我的 CSS 代码。

.header{
    background-image: url("../images/8.jpg");
    background-attachment:scroll;
    height: 100%;
    position: fixed;
    background-size: cover;
    width: 40%;
    background-repeat: no-repeat;
    margin: 0;
    padding-left: 20px;
    font-family: 'Asap Condensed';
    font-size: 50px;
    padding-top: 100px;
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
  }

最佳答案

这是来自 W3Schools 的一个很好的例子使用 background-attachment: fixed; 技术:

<style>
.parallax { 
    /* The image used */
    background-image: url("img_parallax.jpg");

    /* Set a specific height */
    height: 500px; 

    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
  }
</style>

<!-- Container element -->
<div class="parallax"></div> 

关于html - 如何在html中为固定div创建视差效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46282824/

相关文章:

PHP 显示无?

javascript - HTML/CSS 跨度弹出显示不响应

来自 html 选择的 Javascript img.src

javascript - Chrome 中的 Plupload 定位问题

html - 如何将 div 对齐到 iframe 的右侧?

javascript - 指令如何等待处理元素的动态内插属性?

HTML 按钮关闭窗口

css - 为什么输入文件不显示文本 "Browse"?

java - 找不到在 Scene Builder 中添加的 CSS StyleSheet

跨多个表的 jQuery 交替行颜色