简体   繁体   English

jQuery Mobile返回按钮滚动到顶部

[英]Jquery Mobile go back button scrolls to top

In my Jquery Mobile website I am using href for back button like; 在我的Jquery Mobile网站中,我将href用作后退按钮,例如;

 <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. 第一页不在同一位置。

Is there any solution for this? 有什么解决办法吗?

Solution

I had this issue i fixed it using iSroll 我有这个问题,我使用iSroll修复了

While going from PageA to PageB save the scroll position of PageA in a variable. 从PageA转到PageB时,将PageA的滚动位置保存在变量中。

to do this modify the iscroll.js and add getScrollY method under scrollTo like this 为此,请修改iscroll.js并在scrollTo下添加getScrollY方法,如下所示

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

And set the scroll position while going back to that PageA, i am doing this on pagehide event of PageB 并在返回该PageA的同时设置滚动位置,我正在PageB的pagehide事件中执行此操作

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

This way i solved my issue, hope this helps. 这样我解决了我的问题,希望对您有所帮助。

More info 更多信息

If you want to find out more about this topic take a look at this article , you will also find working examples. 如果您想了解有关此主题的更多信息,请阅读本文 ,您还将找到一些可用的示例。

为什么不直接在锚标记上添加data-rel =“ back”并设置href =“#”呢?

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

Before I describe your available solutions you need to understand, this is not an error nor is there a perfect solution. 在描述您需要了解的可用解决方案之前,这不是错误,也不是完美的解决方案。 The issue is that to animate the transition to another page the viewport has to be at the top of the page so that the current page and the page transitioning in are vertically lined-up. 问题在于,要为过渡到另一个页面设置动画,视口必须位于页面的顶部,以便当前页面和在其中过渡的页面垂直排列。

If you were half-way down a long list on one page (say 1000px) and the page you are transferring to is only a few hundred pixels tall then the current page would animate off the screen properly but the new page would not be visible as it would be above the viewport. 如果您在一页上的一长串列表(例如1000像素)的中途,而您要转移到的页面只有几百个像素,则当前页面将在屏幕上适当地进行动画处理,但是新页面将不可见它会在视口上方。

There are 2 viable solutions: 有2种可行的解决方案:

iScroll and its jQuery Mobile derivate iScrollview iScroll及其jQuery Mobile派生的iScrollview

iScroll homepage: http://cubiq.org/iscroll-4 iScroll主页: http ://cubiq.org/iscroll-4

iScrollview homepage: https://github.com/watusi/jquery-mobile-iscrollview iScrollview主页: https : //github.com/watusi/jquery-mobile-iscrollview

iScroll is a javascript that can scroll content in a window within a web browser with very similar behaviour to native scrolling on mobile devices such as iPhone and Android. iScroll是一种JavaScript,可以在Web浏览器中的窗口中滚动内容,其行为与在iPhone和Android等移动设备上进行本机滚动非常相似。 This means you can scroll a window within the browser using native-like scrollbars and physics. 这意味着您可以使用类似本机的滚动条和物理原理在浏览器中滚动窗口。

That is also a solution for our current problem. 这也是我们当前问题的解决方案。 Because of iScroll implementation pages will occupy 100% of page height, no matter how far listview is scrolled. 由于iScroll实现,无论列表视图滚动到多远,页面都将占据页面高度的100%。 This is also a reason why on return listview will still stay at a same position. 这也是为什么返回列表视图仍将停留在相同位置的原因。

Of course in case you want to implement this solution you should pick iScrollview implementation. 当然,如果要实施此解决方案,则应选择iScrollview实施。 You would still be able to implement iScroll, but it would take you much more time. 您仍然可以实现iScroll,但这将花费更多时间。

Silent scroll 静音卷轴

Official documentation: http://jquerymobile.com/demos/1.1.0-rc.1/docs/api/methods.html 官方文档: http : //jquerymobile.com/demos/1.1.0-rc.1/docs/api/methods.html

This jQuery Mobile functionality is also the same reason why we have this problem at the first place. 此jQuery Mobile功能也是我们首先遇到此问题的原因。 Before a page transition is triggered original page is silently scrolled to the top. 在触发页面转换之前,原始页面会静默滚动到顶部。

In our case, when listview is selected, its position must be remembered (here you will find solutions of data/parameteres storing during the page transition, just search for the chapter: Data/Parameters manipulation between page transitions) before page is changes. 在我们的例子中,当选择listview时,必须在更改页面之前记住其位置(在这里您将找到在页面转换期间存储的数据/参数的解决方案,只需搜索章节:页面转换之间的数据/参数操作)。 In that case, when we return to the previous page we could use pagebefpreshow event to silently scroll to the bottom before page is shown. 在这种情况下,当我们返回上一页时,可以使用pagebefpreshow事件在显示页面之前静默滚动到底部。

//scroll to Y 100px
$.mobile.silentScroll(100);

And here's a working example of silent scroll: http://jsfiddle.net/Gajotres/2xfrM/ 这是一个无声滚动的工作示例: http : //jsfiddle.net/Gajotres/2xfrM/

And here's a real life jsFiddle example using large listview and several pages: http://jsfiddle.net/Gajotres/5zZzz/ 这是一个使用大型listview和几个页面的jsFiddle现实示例: http : //jsfiddle.net/Gajotres/5zZzz/

// Detect click on a li element and store its coordinate, change page to another
$(document).on('pagebeforeshow', '#index', function(){  
    $('#test-list li').on('click', function(){   
        storePosition.topCoordinate =  $(this).offset().top;
        $.mobile.changePage('#second');
    });    
});

// If there's a stored position use silentscroll function and scroll to correct location
$(document).on('pageshow', '#index', function(){      
    if(storePosition.topCoordinate !== null) {
        $.mobile.silentScroll(storePosition.topCoordinate);
    }
});

// Store position location
var storePosition = {
    topCoordinate : null
}

Unfortunately like in your example, this solution works only on pageshow. 不幸的是,像您的示例一样,此解决方案仅适用于页面展示。 Because of jQM architecture it is only possible to do this during the pageshow event. 由于jQM体系结构,因此只能在pageshow事件期间执行此操作。

Final notes 最后的笔记

If you want to find out more about iScroll + iScrollView, how they work with working examples then take a look at this article . 如果您想了解有关iScroll + iScrollView的更多信息,它们如何与工作示例一起工作,那么请看一下本文

I found a solution here: https://forum.jquery.com/topic/jquery-mobile-scroll-to-top-of-page-on-page-load#14737000005271291 我在这里找到了解决方案: https : //forum.jquery.com/topic/jquery-mobile-scroll-to-top-of-page-on-page-load#14737000005271291

(function($){
      $( document ).on( "mobileinit", function() {
            var silentScroll = $.mobile.silentScroll;
              $.mobile.silentScroll = function( ypos ) {
            if ( $.type( ypos ) !== "number" ) {
               // FIX : prevent auto scroll to top after page load
               return;
            } else {
               silentScroll.apply(this, arguments);
            }
        }
      })
}(jQuery));

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

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