使用javascript更改滚动页边距顶部



当我滚动时,我的图像必须用javascript向下移动10px。我试了很多,比如:

function myFunction() {
document.getElementById("test").style.marginTop = "10px";
}

css:

#test {
position: absolute;
margin-left: 614px;
margin-top: 366px;
width: 32%;
}

但它不起作用。

您可以在javascript代码上添加以下代码。

document.getElementById("test").style.display = "inline-block";

有显示的说明。

https://www.w3schools.com/jsref/prop_style_display.asp

好吧,解决它的最简单方法是:

  • 将html代码放入div(带有id容器(
  • 接下来,您需要(通过javascript(执行onscroll事件->基本上,当你滚动时,图像会向下移动

所以代码可以看起来像:

document.getElementById("conatiner").onscroll = function(){
myScrollDownFunction()
}
function myScrollDownFunction() {
document.getElementById("test").style.marginTop = "376px";
}

最新更新