简体   繁体   English

CSS:使用变换将元素移动到 DOM 上的特定位置

[英]CSS: Using transform to move an element to a specific place on the DOM

I have an image (svg) that start out in the middle of the page on load.我有一个图像(svg),在加载时从页面中间开始。 As the user scrolls down, I want to have that image drift up and to the left so that it eventually gets pinned in the top left corner.当用户向下滚动时,我想让该图像向上和向左漂移,以便最终固定在左上角。
I tried using translate with the scroll number but I feel like it's a guessing game on where the image will end up and what path it takes.我尝试使用带有滚动编号的翻译,但我觉得这是一个关于图像将在哪里结束以及它采用什么路径的猜谜游戏。 Here is what I had so far.这是我到目前为止所拥有的。

 window.addEventListener('scroll', function(evt) { let image = document.getElementById('image'); let scrollY = window.pageYOffset; // how do I use the scrollY to accomplish this? image.style.transform =??
 <div style="text-align: center; background: blue; height: 900px;"> <img id="image" style=" margin-top: 200px; " src="mySvg.svg" /> </div>

for working document.getElementById('image');用于工作 document.getElementById('image'); need to have is corresponded element.需要有对应的元素。

<img id="image" style=" margin-top: 200px; " src="mySvg.svg" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS按位置或变换移动元素? - CSS move element by position or by transform? 触摸移动变换后,CSS动画元素在错误的位置进行动画处理 - CSS animated element animates on wrong place after touch-move transform 在特定位置将元素插入 DOM 不起作用 - Inserting element into DOM at a specific place is not working 如何使用纯JavaScript在DOM中将元素从一个地方移动到另一个地方? - How to move an element from one place to another in the DOM using plain JavaScript? 使用javascript将元素在dom树中向上或向下移动一个位置 - Move an element one place up or down in the dom tree with javascript 使用 DOM 操作移除 CSS &#39;transform&#39; - Remove CSS 'transform' using DOM Manipulation 如何使用 Javascript 替换 DOM 元素? - How to replace DOM element in place using Javascript? 如何计算应用CSS转换矩阵的DOM元素的边界矩形大小? 不使用Element.getBoundingClientRect() - How to calculate bounding rectangles size for a DOM element on which CSS transform matrix is applied? Without using Element.getBoundingClientRect() 将元素放在AngularJS中的DOM中 - Place element in DOM in AngularJS 在 CSS 中将 transform-origin 移回元素的中心 - Move transform-origin back to the center of the element in CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM