gpt4 book ai didi

javascript - 将图像从屏幕中间移动到滚动的 Angular 落

转载 作者:行者123 更新时间:2023-11-29 10:26:11 31 4
gpt4 key购买 nike

嘿,我是新手,如果有任何明显的错误,请原谅我,谢谢 :)

因此,每当您开始滚动时,我都需要一个图像从屏幕中间移动到屏幕的一 Angular 。我能够做到这一点。唯一的问题是,当我滚动回顶部时,图像不会回到原来的位置。

提前感谢您的帮助:)

 window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
document.getElementById("logo").style.width = "70px";
document.getElementById("logo").style.margin = "0px";
document.getElementById("logo").style.position = "fixed";
} else {
document.getElementById("logo").style.width = "30%";
document.getElementById("logo").style.margin = "0 auto";
document.getElementById("logo").style.position = "relative";
}
}
#logo {
width: 30%;
transition: 0.5s;
margin: 0 auto;
margin-top: 15%;
}


.scroll {
height: 1200px;
}
<!Doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.typekit.net/siu5pjx.css">
</head>
<body>

<!--LOGO-->
<div id="logo">
<a href="<?php echo get_page_link( get_page_by_title( home )->ID ); ?>">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" width="100%">
</a>
</div>

<div class="scroll"></div>
</body>
</html>

最佳答案

所以问题是,当您向上滚动时,您没有添加 margin

这可以通过使用 javascript 添加重新添加边距来简单地解决。

window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
document.getElementById("logo").style.width = "70px";
document.getElementById("logo").style.margin = "0px";
document.getElementById("logo").style.position = "fixed";
} else {
document.getElementById("logo").style.width = "30%";
document.getElementById("logo").style.margin = "0 auto";
document.getElementById("logo").style.marginTop = "15%";
document.getElementById("logo").style.position = "relative";
}
}
#logo {
width: 30%;
transition: 0.5s;
margin: 0 auto;
margin-top: 15%;
}


.scroll {
height: 1200px;
}
<!Doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.typekit.net/siu5pjx.css">
</head>
<body>

<!--LOGO-->
<div id="logo">
<a href="<?php echo get_page_link( get_page_by_title( home )->ID ); ?>">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" width="100%">
</a>
</div>

<div class="scroll"></div>
</body>
</html>

关于javascript - 将图像从屏幕中间移动到滚动的 Angular 落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58827532/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com