Javascript Meteor:当页面刷新或其他页面位置时,如何保留dom操作?
我有一个添加新元素(链接)的无限滚动条,当我导航到链接并返回浏览器时,我返回到结果的第一页,而不是由于我以前的滚动操作而添加的扩展元素。唯一的方法是保存滚动条的状态,然后在Meteor渲染模板后重新应用DOM操作 例如,类似这样的内容:Javascript Meteor:当页面刷新或其他页面位置时,如何保留dom操作?,javascript,meteor,Javascript,Meteor,我有一个添加新元素(链接)的无限滚动条,当我导航到链接并返回浏览器时,我返回到结果的第一页,而不是由于我以前的滚动操作而添加的扩展元素。唯一的方法是保存滚动条的状态,然后在Meteor渲染模板后重新应用DOM操作 例如,类似这样的内容: Template.my_template.rendered = function() { $('#my-scrolling-element').scroll(function(e) {scrollPosition = e.target.scrollTop
Template.my_template.rendered = function() {
$('#my-scrolling-element').scroll(function(e) {scrollPosition = e.target.scrollTop()})
if(scrollPosition) {
$('#my-scrolling-element').scrollTo(scrollPosition);
}
}
唯一的方法是保存滚动条的状态,然后在Meteor渲染模板后重新应用DOM操作 例如,类似这样的内容:
Template.my_template.rendered = function() {
$('#my-scrolling-element').scroll(function(e) {scrollPosition = e.target.scrollTop()})
if(scrollPosition) {
$('#my-scrolling-element').scrollTo(scrollPosition);
}
}
你做错了!需要用流星的方式 只需使用
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();
}
client/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;
});
server/publication.js
Meteor.publish('huge-list', function(page){ //when session changes on client, this changes
return Requests.find({}, {limit:page});
});
你做错了!需要用流星的方式
只需使用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();
}
client/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;
});
server/publication.js
Meteor.publish('huge-list', function(page){ //when session changes on client, this changes
return Requests.find({}, {limit:page});
});
使用meteor的好处之一是无需进行页面刷新或导航。如果需要,您可以尝试使用session/querystring变量来帮助执行此操作。使用meteor的好处之一是无需进行页面刷新或导航。如果需要,您可以尝试使用session/querystring变量来帮助执行此操作。我只是从订阅中“增加”集合中可用文档的数量。因此,当他们返回此页面时,它将显示以前可用的文档数量,然后将用户从会话值向下滚动到他们所在的位置。我也想过这样做,但现在我正在考虑执行以下操作。我只是从订阅中“增加”集合中可用文档的数量。因此,当他们返回此页面时,它将显示以前可用的文档数量,然后从会话值向下滚动用户到他们所在的位置