簡體   English   中英

流星:當頁面刷新或其他頁面位置時,如何保留dom操作?

[英]Meteor: How do you preserve dom manipulations when page refreshes or another page location?

我有一個無限滾動,它會添加新的元素(鏈接),當我導航到該鏈接並在瀏覽器中返回時,我將返回結果的第一頁,而不是先前滾動所添加的擴展元素行動。

你做錯了! 需要以流星的方式去做。

您只需使用Deps.autorun()並更改頁數Session.set('currentPage');Deps.autorun()客戶端上的文檔數Session.set('currentPage');

main.js

//when scrollbar reaches end of page, just change the 'currentPage' session variable to 'grow' the list template
    if ($(window).scrollTop() + $(window).height() == $(document).height()) {
var nowPage = Session.get('pageNumber');
Session.set('pageNumber', parseInt(nowPage) + 1);
e.stopImmediatePropagation();
}

客戶機/ subscription.js

Deps.autorun(function(){
  Meteor.subscribe('huge-list', Session.get('currentPage'); //whenever currentPage changes, so will your subscription if you set up your publish() on the server side;
});

服務器/ publication.js

Meteor.publish('huge-list', function(page){ //when session changes on client, this changes
return Requests.find({}, {limit:page});
});

唯一的方法是保存滾動狀態,然后在Meteor渲染模板后重新應用DOM操作。

例如,如下所示:

Template.my_template.rendered = function() {
    $('#my-scrolling-element').scroll(function(e) {scrollPosition = e.target.scrollTop()})

    if(scrollPosition) {
        $('#my-scrolling-element').scrollTo(scrollPosition);
    }
}

暫無
暫無

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

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