CSS 使 DIV 位置固定在具有 PE​​RSPECTIVE 属性的 DIV 内部

2023-12-09

我有一个获取问题#fixed with position:fixed关系到#container

检查这个小提琴:https://jsfiddle.net/a1zoghs0/2/

我知道,如果我把#fixed在外面#container,

它将有position:fixed。就像这个小提琴:https://jsfiddle.net/xc879rbm/1/

但不幸的是,我有一个问题,这种方法无法工作。是吗 可以把这个放进去#container并且仍然启用position:fixed关系到#container?

我的代码有什么问题吗?

提前致谢...


这是我的解决方案。我不太确定其中的所有数学细节,但它看起来对于调整窗口大小相当稳健。

主要技巧是将标题放置得足够远并使其足够大,以便视口的移动不会对其位置产生太大影响 - 想想天空中的月亮,它又大又远,因此你的头部的移动不影响其位置。

body{
  margin:0; /* without it your container will not cover full body */
}
#container {
    width:100vw; /* probably 100% would be ok here */
    height:100vh; /* 100% would not be ok here, 
                     as we need to center perspective viewport 
                     w.r.t. screen- not w.r.t. whole long page content */
    /* this makes math easier to me */
    perspective:1px; 
    perspective-origin:0 0;
    /* we want container to be a window through which you watch
       the scrolling world */
    overflow-y:scroll;
    /* we don't want scrollbar at the bottom, as it would again force
       us to use calc for perspective-origin-y.
       Frankly, I do not know why the scrollbar appears at all */
    overflow-x:hidden;
    /* This is to allow positioning of layers relatively to container*/
    position:relative;
}

.parallaxBase {
    width:100%; 
    position:absolute;
    top:200px;  
 }

.parallaxBack {
    height:100vh;
    /* The general formula to keep size intact is scale(perspective-z)
       so in our case it is scale(1+1) */
    transform:translateZ(-1px) scale(2);
    transform-origin: 0 0;
}

#background {background:red; height:200px; padding-top:100px; }
#content {background:yellow; }
#fixed {background:green; 
  width:100%; 
  height:40px; 
  /* this is not so much to make it sticky,
     it is rather to not push the parallaxBack div lower,
     and to make sure that header is not occluded by elements
     which have position:absolute like parallaxBase,
     as even z-index:1 won't help you if you have position:static
      */
  position:absolute; 
  top:0px; 
  z-index:1;
  /* Now the main idea:
     we push the navbar 1023px to the back, but at the same time,
     we make it (1023+1) times bigger, which makes it appear in
     original size.*/
  transform: scale(1024) translateZ(-1023px);
  transform-origin: 0 0;
 }
<div id="container">
  <div id="fixed">this is fixed // why not fixed?</div>
  <div class="parallaxBack">
    <div id="background"> this is parallax</div>
  </div>
  <div class="parallaxBase">
    <div id="content">
  this is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is content
  this is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is content
    </div>
  </div>
</div>
You might want to play with it in https://jsfiddle.net/7L8ndtuv/2/

我也推荐阅读https://developers.google.com/web/updates/2017/03/custom-scrollbar这启发了我的回答。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CSS 使 DIV 位置固定在具有 PE​​RSPECTIVE 属性的 DIV 内部 的相关文章

  • 如何将文本环绕在非矩形图像周围?

    是否可以将文本环绕在非矩形图像周围 我希望不同国家的地图周围的文本环绕该国家 地区的形状 即使边界本身不是直的 文本也始终与该国家 地区的边界保持相同的距离 这可能吗 您可以使用这个方法 http torylawson com index
  • 如何使用 Jsoup 解析此 HTML

    我正在尝试提取 了解你的拖拉机 和 壳牌石油公司 1955 请记住 这只是整个代码的一小段 并且有多个 H2 H3 标签 我想获取所有 H2 和 H3 标签的数据 HTML 如下 https i stack imgur com Pif3B
  • 更改 Angular 对话框的背景颜色

    我正在尝试更改对话框背景 无需触摸style css file 正如其他一些答案所说 有很多方法可以设置对话框样式 1 此解决方案适用于宽度和高度 但透明背景被 忽略 this dialog open DialogComponent dis
  • 替换javascript中字符串中的多个字符

    我得到了这段不错的代码 但我不知道为什么不起作用 出于兼容性目的 它应该获取文本输入的值并用 HTML 代码替换每个给定的国家字符 但是 当我单击按钮时 该函数返回字符串而不进行任何更改 任何想法 jsfiddle http jsfiddl
  • 有没有办法将图像异步加载到用户缓存?

    我有一份物品清单 它们每个都是一个带有名称的正方形 当用户将鼠标悬停在正方形上时 将显示一个图像 这是由 jQuery 完成的 代码是这样的 square hover function var link this attr title th
  • 如何为平板电脑和 iPhone 制作样式表

    我认为样式表上的链接目标将使我的 css 文件仅在平板电脑或 iPhone 上加载时加载 但我试图隐藏的元素仍然存在 我目前正在使用 media handheld 有太多的移动设备型号需要为其编写样式表 你最好根据以下内容调整你的CSS屏幕
  • Bootstrap:列表内联项目符号?

    有人知道如何在 Bootstrap 3 中水平列表中的元素之间添加项目符号 分隔符吗 ul class list inline li Author Michal li li Modified 17 08 2014 li li Comment
  • 强制应用媒体查询

    我有这个 html 和 css 代码 div class wrapper div class a div div class b div div media all and max width 400px max height 300px
  • Rails 上的 SASS 无效 CSS 错误

    我正在尝试使用http startbootstrap com stylish portfolio http startbootstrap com stylish portfolio但是 在我的 Rails 应用程序中 我在 vintage
  • 如何选择一个元素但排除其子元素

    我明白 div hover not class 作品 但是 如何实现 LI 元素的 悬停 效果 但排除鼠标悬停在LI内的某个 DIV 上时的悬停效果 E G li Hello div Bye div li 我想获得 li 的悬停效果 li
  • HTML CSS 循环协助

    希望你能帮忙 我想知道您是否可以在循环中创建以下类型 你知道这是否可能吗 body background 000 DEMO SPECIFIC STYLES typewriter h1 color fff font family Bebas
  • 角度材质表-表内边框

    I am using Angular material table and I want to set border inside the table Using CSS I was able to set border Normal ca
  • 如何让 ExtJS ComboBox 与文本一起显示?

    我想让以下内容显示在一行中 我尝试过使用样式浮动和显示 Show this input
  • Ace Editor 自动完成和多种语言

    如何为 Ace 编辑器创建自动完成功能以及如何突出显示 php 中的 html javascript 和 csshttp ace ajax org http ace ajax org
  • 如何将多个画布元素放入一个画布元素中?

    我有多个由 JavaScript 创建的画布元素 我的问题是 如何将多个画布元素放入一个画布元素 干得好 JSfiddle http jsfiddle net Allendar UqxCY 它会抓取每个画布并将它们做成 PNG 的 访问缓冲
  • 标题后的三角形与标题背景渐变相匹配

    我必须制作这样的标题 我用这个渐变制作标题没有问题 我也知道如何定位三角形 after 但是 如何使三角形颜色与标题背景相匹配 问题是梯度不是静态的 例如 如果我在智能手机中打开页面 由于屏幕较短 渐变将比在显示器中打开更 紧凑 所以箭头颜
  • 无法使用python和beautifulsoup抓取网页中的某些href

    我目前正在使用 Python 3 4 和 bs4 爬取网页 以收集塞尔维亚在里约 2016 年的比赛结果 所以网址here http rio2016 fivb com en volleyball women teams srb serbia
  • JQuery 设置动态最大宽度

    我不太擅长 jQuery 因此完整的代码解决方案将是理想的选择 该函数将 获取浏览器屏幕的 70 宽度 将该宽度转换为其相应的 px 值 设置最大宽度 mainContainer使用从转换 计算中获得的值 这是我要设置的容器的 CSS 样式
  • IE10 中的弹性盒

    我试图让弹性框在 IE10 中工作 但它不起作用 Safari Chrome 和 Firefox 工作正常 但 IE10 不工作 有人知道答案吗 代码笔 http codepen io anon pen vcEGH http codepen
  • 使用 ASP.Net 和 Ajax 的登录页面

    我正在尝试使用 html ajax 和 ASP NET 制作登录页面 数据确实传递给 ajax 函数 但是当我调试 asp 页面时 用户名和密码以 NULL 发送 该代码应该获取用户名和密码 然后返回用户 ID html页面 div Use

随机推荐

  • 将 mysql 转换为 pdo

    所以我有一个应该处理所有数据执行操作的函数 sql function loadResult sql this gt connect sth mysql query sql rows array while r mysql fetch obj
  • 使用 fastxml.jackson 将 JSON 解析为 Java 记录

    根据设计 Java 记录不能从另一个对象继承 请参阅为什么Java记录不支持继承 所以我想知道实现以下目标的最佳方法是什么 鉴于我的 JSON 数据包含具有一些通用数据 独特数据的对象 例如 类型 宽度和高度适用于所有形状 但根据类型 它们
  • AngularJS 和 jquery 移动

    我通过 AngularJS 中的控制器将 html partial 加载到 ng view 指令中 html 部分看起来像这样 div ul li a href detailsuser a A a li li a href detailsu
  • 将 Http“Post”请求发送到 C 或 C++ 中的 PHP 代码

    我正在尝试向我的 php 文件发送 post 请求并获取信息 它工作正常 但是 在打印 php 文件中的响应之前 它还会打印一些内容 这就是它打印的内容 first HTTP 1 1 200 OK Date Fri 20 Apr 2012
  • ASP.NET MVC 自定义路由 很长的自定义路由在我脑海中没有出现

    今天我花了几个小时阅读有关在 ASP NET MVC 中进行自定义路由的内容 我可以理解如何执行任何类型的自定义路由 如果它从默认路由扩展或类似于 小于默认路由 但是 我正在尝试找出如何执行类似于以下内容的路线 Language Langu
  • 对自定义类的 IEnumerable 中具有特定 ID 的项目求和

    CustomClass int ID int numberToSum float numToAverage IEnumerable
  • Volley速度慢并导致内存泄漏

    在我的项目中 我使用 volley 下载 JSON 流 我解析该流并将其显示在列表视图中 我使用以下方法加载数据 private void loadEventData int year final int month get volley
  • 在您的 delphi 应用程序中托管 WSH(VBScript、JavaScript)?

    我正在寻找从我的 Delphi 应用程序执行用户提供的脚本 是否可以在我的应用程序中托管 Windows 脚本宿主引擎并为其提供要执行的脚本 或者 有更好的方法来解决这个问题吗 P S 我不是在寻找第三方组件 这是完全可能的 并且有大量代码
  • 替换每行中第一次出现的位置

    假设我有一个格式如下的文档 word1 word2 word3 word4 word5 word6 word7 word8 我该如何使用sed将第一次出现的空格替换为逗号后跟空格 以便文档如下所示 word1 word2 word3 wor
  • 在从命令行调用的 Jython 脚本中使用 UCanAccess

    操作系统 Windows7 Jython2 7 0FR 最终版本 尝试将 Gord Thompson 的解决方案与 Jython 结合使用 在没有 ODBC 的情况下从 Java 操作 Access 数据库 我可以让 Ucanaccess
  • 如何在核心java中实现图?

    我有一个有向未加权图 给出了节点的数量和节点之间的所有链接 我尝试使用向量数组来完成该任务 但 java 不支持它 ArrayList 和 Vectors 支持随机访问迭代器 但无法在 java 中执行此操作 因为我是新手 我不想使用二维矩
  • 排序合并的数据框

    作为一个相当新的 R 程序员 我似乎遇到了一个奇怪的问题 可能是我对 R 缺乏经验 在读取连续文件并将其合并到单个数据帧中后 我发现顺序未按预期对数据进行排序 我在每个文件中有多个引用 但每个文件引用在不同时间获得的测量数据 这是代码 li
  • 正确的 Bash 和 shell 脚本变量大写 [关闭]

    Closed 这个问题是基于意见的 目前不接受答案 我遇到过许多变量全部大写的 shell 脚本 我一直认为这存在严重的误解 我的理解是 按照惯例 也许很久以前就有必要 环境变量全部大写 但在像 Bash 这样的现代脚本环境中 我一直更喜欢
  • Google oauth java 客户端获取访问令牌失败,并显示“400 Bad Request {“error”:“invalid_request”}”

    我正在使用此处显示的 Web 服务器应用程序 示例 在我的回调 servlet 中 我想检索访问令牌 但无法成功执行此操作 以下是我的代码 Override protected String getUserId HttpServletReq
  • UIAnimation的理解

    我正在尝试使用以下代码来执行一些动画 void performSlidingfromX int xx fromY int yy UIImageView Image self getImage UIView beginAnimations n
  • 透明JButton

    是否可以使 JButton 透明 包括边框 但不透明文本 我扩展了 swing 的 JButton 并覆盖了它 Override public void paint Graphics g Graphics2D g2 Graphics2D g
  • 如何使用 DXL 脚本从 IBM Doors 加载特定基线

    我编写了一个从批处理文件运行的 DXL 脚本 它接收一个模块路径 一个基线和一个视图 批处理命令打开 IBM Doors 9 6 后 脚本应该转到具有相关视图的模块路径并加载输入的基线 我的问题是我的脚本加载了current基线而不是我输入
  • 如何将多个实体集绑定到一个 odata 控制器?

    我有很多不同的实体想要为其启用 OData 这些实体根据其类型分为不同的组 目前 默认设置是将 EntitySet 与控制器名称相匹配 但我不希望为我拥有的每个实体类型都有一个控制器 有没有一种方法可以将多个 EntitySet 映射到一个
  • 将动态连接字符串与 Breeze EFContextProvider 结合使用

    目前我有一个应用程序 web silverlight 其中我的 ObjectContext 的连接字符串是动态的 它基于用户登录的方式 因为我的每个客户都有自己的数据库 即 用户名 域 我试图找到一种使用 EFContextProvider
  • CSS 使 DIV 位置固定在具有 PE​​RSPECTIVE 属性的 DIV 内部

    我有一个获取问题 fixed with position fixed关系到 container 检查这个小提琴 https jsfiddle net a1zoghs0 2 我知道 如果我把 fixed在外面 container 它将有pos