简体   繁体   English

更改window.location.href时,不会将页面滚动到底部

[英]Scroll page to bottom not happening while changing window.location.href

I have issue while trying to scroll page to bottom in ajax success. 我在尝试将页面滚动到Ajax成功底部时遇到问题。 I am setting window.location.href as I need to remove the hash tag in url and then I want to scroll the page to bottom. 我正在设置window.location.href因为我需要删除url中的hash标签,然后将页面滚动到底部。 But the page is still on top after page load. 但是页面加载后页面仍位于顶部。 Below is my code. 下面是我的代码。

I know that scroll to bottom can be written after page reload outside ajax. 我知道在ajax外部重新加载页面后可以滚动到底部。 But this particular requirement to scroll to bottom is not desired all the time and its based on a certain button click inside which ajax has been written. 但是一直不需要一直滚动到底部的这种特殊要求,它是基于已在其中写入ajax的特定按钮单击而实现的。

    $.ajax({
            url: updatedUrl,
            dataType: 'json',
            type: 'post',
            data: 'id=' +  id,
            complete: function() {},
            success: function(data) {
                window.location.href = window.location.href.split('#')[0];
                $('.overlay').hide();
                var $target = $('html,body'); 
                $target.animate({scrollTop: $target.height()}, 1000); 
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
    });

What might be the issue here? 这里可能是什么问题? How can I remove the hash tag as well as scroll page to bottom? 如何删除哈希标签以及滚动到底部?

When you do window.location.href this will reload the page with new URL, the lines next to this line won't get exicuted. 当您执行window.location.href这将使用新的URL重新加载页面,该行旁边的行不会被删除。 If you want to change the url without loading the page ,replace window.location.href = window.location.href.split('#'[0]; with window.history.pushState("string", "Title", window.location.href.split('#')[0]); 如果要在不加载页面的情况下更改网址window.location.href = window.location.href.split('#'[0];window.history.pushState("string", "Title", window.location.href.split('#')[0]);替换window.location.href = window.location.href.split('#'[0]; window.history.pushState("string", "Title", window.location.href.split('#')[0]);

this will change the URLwithout reloading 这将更改URL而无需重新加载

I don't know if that's possible that way; 我不知道那是不可能的。 when you load another page, is another scope, you can't control it from a previous page script. 当您加载另一个页面是另一个作用域时,您无法从上一页脚本中对其进行控制。

But I have some suggestions. 但是我有一些建议。

If you can change the "new page", pass some information like in a query string in the URL and include a script in the new page to scrollDown. 如果可以更改“新页面”,请在URL中传递一些信息,例如在查询字符串中,并在新页面中包含脚本以向下滚动。

Another option: open that page in an IFRAME 100%. 另一个选择:以100%的IFRAME打开该页面。 This way you can control from "outside" (there are some restrictions about executing code from "outside" - is it the same "origin" (same domain). 这样,您可以从“外部”进行控制(从“外部”执行代码存在一些限制-它是否是相同的“源”(相同的域)。

I see you using window.location.href = window.location.href.split('#')[0]; 我看到您使用window.location.href = window.location.href.split('#')[0]; . Perhaps you could, when you want to scroll to the bottom, use window.location.href = window.location.href.split('#')[0] + '#bottom'; 当您想滚动到底部时,也许可以使用window.location.href = window.location.href.split('#')[0] + '#bottom'; and include a <div id="bottom"> at the end of the page. 并在页面末尾添加<div id="bottom">

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM