簡體   English   中英

使用JS旋轉圖像時如何添加邊距頂部

[英]How to add a margin top when image is rotating with JS

當圖像旋轉 90 度時,我想在圖像頂部添加邊距。 這就是我旋轉圖像的方式:

const img = document.querySelector("#mocci-logo")

document.addEventListener("scroll", (e) => {
  if(!window.scrollY) {
    img.style.transform = "rotate(0deg)"
  } else {
    img.style.transform = "rotate(-90deg)"
  }
})

我知道這可能很簡單,但我是初學者。 所以希望有人能告訴我如何做到這一點。 謝謝!

嘗試這個:

const img = document.querySelector("#mocci-logo")

document.addEventListener("scroll", (e) => {
  if(!window.scrollY) {
    img.style.transform = "rotate(0deg)"
    img.style.marginTop = "0px"; // This can also be 0 or "0".
  } else {
    img.style.transform = "rotate(-90deg)"
    img.style.marginTop = "60px";
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM