gpt4 book ai didi

javascript - PHP 保持滚动位置

转载 作者:行者123 更新时间:2023-12-02 14:41:03 24 4
gpt4 key购买 nike

当我的页面通过点击超链接刷新时如何保持滚动位置?我尝试创建一个超链接,将数量添加到项目并刷新页面。但每次我点击链接我都会回到页面顶部。

 <a href="selectquantity.php?remove='.$id.'" >[-]</a> 

最佳答案

如果想刷新页面,可以使用javascript或jquery来恢复滚动位置。

对于javascript,您可以按照Restoring page scroll position with jQuery的指南进行操作

代码很简单:

var scroll = $(window).scrollTop();
$("html").scrollTop(scroll);

对于jquery,您可以按照how to remember scroll position of page的指南进行操作

代码是:

// When document is ready...
$(document).ready(function() {

// If cookie is set, scroll to the position saved in the cookie.
if ( $.cookie("scroll") !== null ) {
$(document).scrollTop( $.cookie("scroll") );
}

// When a button is clicked...
$('#submit').on("click", function() {

// Set a cookie that holds the scroll position.
$.cookie("scroll", $(document).scrollTop() );

});//end of submit

});//end of document.ready

关于javascript - PHP 保持滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013459/

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