2015年12月15日 星期二

讓 Meteor 在換頁時頁面定位到最上方

最近在用 meteor 搭配 iron-router 做一個 web 後台的雛形出來,但因為 meteor 的設計還蠻特別的,所以有些行為跟一般做換頁的網頁效果不太一樣,主要是比較像是 Single Page Application,你在換頁的時候感受不到瀏覽器有在重新 loading 的感覺,所以換頁時,你的 viewport 可能會停留在上一頁的看到的地方,為了不讓 user 感到困擾(<- 視頁面情況..),在 github issue 上面找到一個解法,要在 iron-router 的 filter 加上一個方法,這個方法用來讓頁面捲到最上方,然後把這個方法掛在 Router 的 onAfterAction。


(* 我的 meteor 版本是 Meteor 1.2.1)

那一串 github issue 有很多討論,但是我是用這個方法
/* Source from https://github.com/tmeasday/meteor-router/issues/71#issuecomment-51592305 START */
Router._filters = {
resetScroll: function () {
var scrollTo = window.currentScroll || 0;
$('body').scrollTop(scrollTo);
$('body').css("min-height", 0);
}
};
var filters = Router._filters;
if(Meteor.isClient) {
Router.onAfterAction(filters.resetScroll); // for all pages
}
/* Source from https://github.com/tmeasday/meteor-router/issues/71#issuecomment-51592305 END */
// if you got 404...
Router.configure({
layoutTemplate: 'loginLayout',
notFoundTemplate: '404'
});
// your customer router~
Router.route('/', function () {
this.layout('loginLayout');
this.render("login");
});
Router.route('/login', function () {
this.layout('loginLayout');
this.render("login");
});
Router.route('/dashboard', function () {
this.layout('mainLayout');
this.render("dashBoard");
});
view raw router.js hosted with ❤ by GitHub
有成功拉,好險好險 :)

沒有留言:

張貼留言

若你看的文章,時間太久遠的問題就別問了,因為我應該也忘了... XD

Vue multiselect set autofocus and tinymce set autofocus

要在畫面一進來 focus multiselect 的方式: 參考: https://jsfiddle.net/shentao/mnphdt2g/ 主要就是在 multiselect 的 tag 加上 ref (例如: my_multiselect), 另外在 mounted...