Jquery Mobile 返回按钮滚动到顶部 [英] Jquery Mobile go back button scrolls to top

查看:25
本文介绍了Jquery Mobile 返回按钮滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Jquery Mobile 网站中我使用 href 作为后退按钮;

In my Jquery Mobile website I am using href for back button like;

 <a id='{0}' class='{1}' href='/' data-role=""button"" data-icon=""arrow-l"" data-transition=""slide"" data-direction=""reverse"">

但是如果我在第一页上滚动,后退按钮会再次跳回到顶部.首页不会停留在同一位置.

but if I have scroll on first page, back button jumps back to top again. First page does not stay on same position.

有什么解决办法吗?

推荐答案

解决方案

我遇到了这个问题,我使用 iSroll

当从 PageA 到 PageB 时,将 PageA 的滚动位置保存在一个变量中.

While going from PageA to PageB save the scroll position of PageA in a variable.

为此修改iscroll.js并像这样在scrollTo下添加getScrollY方法

to do this modify the iscroll.js and add getScrollY method under scrollTo like this

        scrollTo : function(x, y, time, relative) {
            var that = this, step = x, i, l;

            that.stop();

            if (!step.length)
                step = [{
                    x : x,
                    y : y,
                    time : time,
                    relative : relative
                }];

            for ( i = 0, l = step.length; i < l; i++) {
                if (step[i].relative) {
                    step[i].x = that.x - step[i].x;
                    step[i].y = that.y - step[i].y;
                }
                that.steps.push({
                    x : step[i].x,
                    y : step[i].y,
                    time : step[i].time || 0
                });
            }

            that._startAni();
        },
        getScrollY : function() {

            var that = this;

            return that.y;

        },

现在像这样在页面更改之前保存当前位置

Now save the current position before page change like this

curScrollPos = myScroll.getScrollY();

并在返回到那个 PageA 时设置滚动位置,我在 PageB 的 pagehide 事件上这样做

And set the scroll position while going back to that PageA, i am doing this on pagehide event of PageB

myScroll.scrollTo(0, curScrollPos, 1);
myScroll.refresh();

这样我就解决了我的问题,希望对您有所帮助.

This way i solved my issue, hope this helps.

如果您想了解有关此主题的更多信息,请查看此 文章,您还将找到工作示例.

If you want to find out more about this topic take a look at this article, you will also find working examples.

这篇关于Jquery Mobile 返回按钮滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆