簡體   English   中英

滾動不適用於頁面重定向

[英]Scroll not working on page redirection

我正在嘗試使用評論系統創建一個圖片上傳網站。 注釋部分顯示在“ imagedisplay.php”文件中的圖像下方。 現在,如果用戶尚未登錄,則該用戶將被重定向到注冊頁面,然后在他注冊之后,返回到存在注釋部分的頁面。

我想做的是,在用戶登錄並重定向回imagedisplay頁面后,我希望頁面向下滾動到注釋框。

現在,通過以下方式,我在URL中傳遞一個URL,以及一個包含注釋框ID的哈希值:

var commentformid="commentform"+imageId;
window.location.href="signup.php?lastpage="+encodeURIComponent("imagedisplay.php?id="+imageId+"#"+commentformid);//redirect to signup page

在這里, commentformid是評論框的ID, lastpage是頁面的URL,有人嘗試在其中發布評論,並且該用戶在注冊后將重定向到該URL。

這是我用來向下滾動到注釋框的代碼:

$(document).ready(function(){

//check for hash and if the hash exists, then scroll to the comment-box
var hash=window.location.hash;
if($(hash).length){
    $('body').animate({ scrollTop: $(hash).offset().top }, 1500);
}

})

將用戶正確地定向到注冊頁面,然后返回到顯示有評論部分的圖像顯示頁面,但問題是該頁面沒有滾動到評論框。相反,當頁面加載時,它以注釋框本身(即,頁面在滾動后看起來正確,加載時恰好)。 簡而言之,沒有滾動效果。

[編輯]: 是我用來實現目的的參考

希望我的問題清楚。 我真的是新來的,所以可以得到任何幫助。 提前致謝!

如果要阻止瀏覽器的自動滾動本機行為,可以使用以下代碼段:

$(window).on("beforeunload", function() {
    $(window).scrollTop(0);
});

請注意,並非所有瀏覽器都可以防彈。

通過使用哈希,Web瀏覽器可以立即轉到頁面位置(在文檔准備好之前)。

您應該做的是分別傳遞哈希,然后使用jQuery轉到該位置。

嘗試傳遞網址

imagedisplay.php?id="+imageId+"hash="+commentformid;

然后用你的jQuery:

var hash=GetQueryStringParams('hash');
if($(hash).length){
    $('body').animate({ scrollTop: $(window.location+hash).offset().top }, 1500);
}

function GetQueryStringParams(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​

該功能取自此頁

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM