gpt4 book ai didi

javascript - 如何在页面底部但在(未固定的)页脚上方制作固定的 "go to top"按钮?

转载 作者:行者123 更新时间:2023-11-30 11:22:21 25 4
gpt4 key购买 nike

我制作了一个页面(在 Shopify 上),并在左下角制作了一个固定的“转到顶部”箭头。没关系,但是当我滚动到页面底部时,箭头将位于页脚前面,我希望它保持在页脚上方。

这是我使用的代码:

$(document).ready(function() {			
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$('.go-top').fadeIn(200);
} else {
$('.go-top').fadeOut(200);
}
});

$('.go-top').click(function(event) {
event.preventDefault();

$('html, body').animate({scrollTop: 0}, 300);
})
});
.go-top {
position: fixed;
bottom: 2em;
right: 0.5em;
text-decoration: none;
font-size: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="go-top"> &#x2191; </a>

最佳答案

    $(document).ready(function() {		
$(window).scroll(function() {
//--------------------------- Lines added ------------------------//
var footertotop = ($('.footer').position().top);
var scrolltop = $(document).scrollTop() + window.innerHeight;
var difference = scrolltop-footertotop;
if (scrolltop > footertotop) {
$('.go-top').css({'bottom' : difference});
}else{
$('.go-top').css({'bottom' : 10});
};
//--------------------------- end ---------------------------------//
if ($(this).scrollTop() > 200) {
$('.go-top').fadeIn(200);
} else {
$('.go-top').fadeOut(200);
}
});
$('.go-top').click(function(event) {
event.preventDefault();
$('html, body').animate({scrollTop: 0}, 300);
})
});
.container{height:2000px;position:relative}
.footer{height:200px;width:100%;position:absolute;bottom:0px;background:red}

.go-top {
position: fixed;
bottom: 20px;
display:none; // <---- Dont display on page load
right: 0.5em;
text-decoration: none;
font-size: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
<a href="#" class="go-top"> &#x2191; </a>
<div class="footer"></div>
</div>

关于javascript - 如何在页面底部但在(未固定的)页脚上方制作固定的 "go to top"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49329126/

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