简体   繁体   English

滚动不适用于页面重定向

[英]Scroll not working on page redirection

I'm trying to create an image uploading website with a comment system. 我正在尝试使用评论系统创建一个图片上传网站。 The comments section is displayed below the image in the 'imagedisplay.php' file. 注释部分显示在“ imagedisplay.php”文件中的图像下方。 Now, if the user has not logged in, then the user is redirected to the sign-up page and then after he signs-up, back to the page where the comments section is present. 现在,如果用户尚未登录,则该用户将被重定向到注册页面,然后在他注册之后,返回到存在注释部分的页面。

What I wish to do is that after the user has signed in and is redirected back to the imagedisplay page, I want the page to scroll down to the comment-box. 我想做的是,在用户登录并重定向回imagedisplay页面后,我希望页面向下滚动到注释框。

Now to do this, I pass a url in a url, along with a hash that contains the id of the comment-box, in the following way: 现在,通过以下方式,我在URL中传递一个URL,以及一个包含注释框ID的哈希值:

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

Here, commentformid is the id of the comment-box and lastpage is the url of the page where someone tried to post a comment and where that user will be redirected to after signing up. 在这里, commentformid是评论框的ID, lastpage是页面的URL,有人尝试在其中发布评论,并且该用户在注册后将重定向到该URL。

This is the code that I'm using to scroll down to the comment-box: 这是我用来向下滚动到注释框的代码:

$(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);
}

})

The user is correctly directed to the signup page and then back to the imagedisplay page where the comments section is present but the problem is that the page doesnot scroll to the comment-box..rather, the when the page is loaded, it starts with the comment-box itself (that is, the page looks the way it should after scrolling, right when it loads). 将用户正确地定向到注册页面,然后返回到显示有评论部分的图像显示页面,但问题是该页面没有滚动到评论框。相反,当页面加载时,它以注释框本身(即,页面在滚动后看起来正确,加载时恰好)。 In short, there is no scroll effect. 简而言之,没有滚动效果。

[EDIT]: This is the reference I was using to achieve this [编辑]: 是我用来实现目的的参考

Hope my question is clear. 希望我的问题清楚。 I'm really new to this so any help is appreciated. 我真的是新来的,所以可以得到任何帮助。 Thanks in advance! 提前致谢!

If you want to counter browser's native behavior of scrolling automatically, you can use this snippet: 如果要阻止浏览器的自动滚动本机行为,可以使用以下代码段:

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

Just so you know, it may not be bulletproof for all browsers. 请注意,并非所有浏览器都可以防弹。

By having the hash, the web browser immediately goes to the page location (before the document is ready). 通过使用哈希,Web浏览器可以立即转到页面位置(在文档准备好之前)。

What you should do is pass the hash separately, and then use jQuery to go to that location. 您应该做的是分别传递哈希,然后使用jQuery转到该位置。

Try passing the URL 尝试传递网址

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

Then with your jQuery: 然后用你的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];
        }
    }
}​

The function is taken from this page 该功能取自此页

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

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