動きが鈍いのは、イベント ハンドラーが原因なのか、ブラウザーがページ上の膨大な量の DOM ノードを処理できないためなのか、またはその他の理由によるものなのかを判断するのは困難です。
現在のビューポートにないビューのイベントの委任を解除するための簡単な解決策を次に示します。本番環境に完全に対応しているわけではありませんが、イベント ハンドラーがパフォーマンスの問題の原因であるかどうかをテストするのに役立ちます。
(ここで JSFiddleを操作し、ブラウザ コンソールも確認してください)
var View = Backbone.View.extend({
onScroll: function() {
var wasInView = this.isInView;
var isInView = this.checkIsInView();
if(wasInView === true) {
if(!isInView) {
this.undelegateEvents();
}
}
else if(wasInView === false) {
if(isInView) {
this.delegateEvents();
}
}
else {
//first pass
if(!isInView) {
this.undelegateEvents();
}
}
this.isInView = isInView;
},
checkIsInView: function() {
var $el = this.$el,
top = $el.offset().top,
bottom = top + $el.height(),
windowTop = $(window).scrollTop(),
windowBottom = windowTop + $(window).height();
return ((bottom <= windowBottom) && (top >= windowTop));
},
render: function () {
//rendering code here...
if(!this.lazyScroll) {
//wait for scroll events to stop before triggering scroll handler
this.lazyScroll = _.debounce(_.bind(this.onScroll, this), 50);
$(window).bind('scroll', this.lazyScroll)
.bind('resize', this.lazyScroll);
}
return this;
},
remove: function() {
if(this.lazyScroll) {
$(window).unbind('scroll', this.lazyScroll)
.unbind('resize', this.lazyScroll);
delete this.lazyScroll;
}
Backbone.View.prototype.remove.apply(this, arguments);
}
});