gpt4 book ai didi

javascript - 使用 location.reload() 后滚动到特定的 div

转载 作者:行者123 更新时间:2023-11-30 11:56:15 24 4
gpt4 key购买 nike

我有一个 JQuery 函数,我想重新加载我的网页,然后将它滚动到特定的 div。

这是我做的

$(document).ready(function() {
$('#mybutton').click(function(e) {
location.reload();
$('html, body').animate({
scrollTop: $("#myID").offset().top
}, 1000);
})
})

当我运行我的脚本时,我的页面可以很好地重新加载,但是滚动脚本不会像我想要的那样运行。

请帮忙

最佳答案

我想我们可以使用 cookie 使其按我们希望的方式工作。我正在添加一个小示例代码。请查看。如果您需要更多帮助,请发表评论。如果这解决了您的问题,请投票这作为答案将不胜感激..谢谢

$(document).ready(function() {


$('#mybutton').click(function(e) {

//$.cookie("reload", 1); // you can use this type of cookie if u have a jquery cookie plugin how ever i have added both type ..

document.cookie=1; // I know this is the absurd way to use cookies. just thought of solving your problem. if you need precise result then either use a jquery cookie plugin or use setCookie function.
reload=true;
location.reload();


})
console.log(document.cookie);
if(document.cookie==1) //if($.cookie("reload")==1)
{
document.cookie="0";
console.log(document.cookie);
$('html, body').animate({
scrollTop: $("#myID").offset().top
}, 1000);
//$.cookie("reload", 0);
}
})

关于javascript - 使用 location.reload() 后滚动到特定的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881620/

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