javascript - 为什么当我尝试在滚动时更改它时我的旋转 css 不起作用?
问题描述
我试图在向下滚动页面时进行 h1 更改。我想要在左侧 Y 页中间的固定位置。为什么不固定?课程一词不在其他地方
const sections = document.querySelectorAll("div")
const clientTag = document.querySelector("h1.client")
document.addEventListener("scroll", function () {
const pixels = window.pageYOffset
sections.forEach(section => {
if (section.offsetTop - 60 <= pixels) {
clientTag.innerHTML = section.getAttribute("data-client")
}
})
})
div {
height:100vh;
}
header {
display:inline-block;
position: fixed;
transform:rotate(-90deg) translate(50%, 50%);
-webkit-transform: translate(50%,50%) rotate(90deg);
transform-origin: 78% 100%;
padding:auto;
margin:auto;
}
<header>
<h1 class="client">work</h1>
</header>
<div data-client="work"></div>
<div data-client="about"></div>
<div data-client="curriculum"></div>
<div data-client="contact"></div>
解决方案
transform-origin
是什么让标题跳了这么多。您可以将其删除,使其采用默认值 ( center
) 并始终相对于其中心旋转,该中心通过设置top
andleft
属性定义。
const sections = document.querySelectorAll("div")
const clientTag = document.querySelector("h1.client")
document.addEventListener("scroll", function () {
const pixels = window.pageYOffset
sections.forEach(section => {
if (section.offsetTop - 60 <= pixels) {
clientTag.innerHTML = section.getAttribute("data-client")
}
})
})
div {
height:100vh;
}
header {
display: block;
position: fixed;
transform: translate(-50%, -50%) rotate(-90deg);
-webkit-transform: translate(-50%, -50%) rotate(-90deg);
top: 50%;
left: 20px;
}
<header>
<h1 class="client">work</h1>
</header>
<div data-client="work"></div>
<div data-client="about"></div>
<div data-client="curriculum"></div>
<div data-client="contact"></div>
推荐阅读
- c# - 在场景之间访问 Firebase 用户/数据库?
- python - 我将如何在此代码中添加选项和附加功能
- javascript - How can I calculate the angle between two lines with the same starting point?
- c# - 如何从 bson 文档中的键获取值以放入 ListBox (C#)
- .net - 如何在不加载该程序集的情况下将 AssemblyName 解析为程序集文件路径?
- regex - 匹配所有正则表达式模式,直到达到分隔模式
- lua - Lua 错误尝试索引全局 nil 值
- ruby-on-rails - 错误:分支“升级”未完全合并
- image - 如何使用淡入淡出效果,如 CSS 过渡到 SVG 中的图像标签?
- tensorflow - 与普通层不同,Keras 自定义层不返回权重