简体   繁体   English

Backbone.js 检测滚动事件

[英]Backbone.js detecting scroll event

I have a following view我有以下观点

var FullWindow = Backbone.View.extend({
  initialize: function() {
    _.bindAll(this, 'detect_scroll');
  },

  // bind the events
  events : {
    "scroll" : "detect_scroll"
  },

  detect_scroll: function() {
    console.log('detected');
  }
});

and I initialize it via我通过

var full_window = new FullWindow({el:$('body')});

But I don't think it's working.但我不认为它有效。

When I change the events to当我将事件更改为

events : {
  "click" : "detect_scroll"
},

It's fine.没关系。

What's wrong?怎么了?

I don't think that the body element will fire a scroll event unless you explicitly give it a scrollbar by setting set its overflow property to scroll in CSS.我不觉得body因素将触发滚动事件,除非你明确地给它一个滚动条,通过设置来设置它的overflow属性为scroll的CSS。 From the jQuery docs:来自 jQuery 文档:

The scroll event is sent to an element when the user scrolls to a different place in the element.当用户滚动到元素中的不同位置时,滚动事件被发送到元素。 It applies to window objects, but also to scrollable frames and elements with the overflow CSS property set to scroll (or auto when the element's explicit height or width is less than the height or width of its contents).它适用于窗口对象,但也适用于可滚动框架和溢出 CSS 属性设置为滚动的元素(或当元素的显式高度或宽度小于其内容的高度或宽度时自动)。

Assuming that you aren't explicitly giving the body element a scrollbar with overflow:scroll and/or a fixed height, the scroll event you want to listen for is probably being fired by the window object, not the body .假设您没有明确地为body元素提供一个带有overflow:scroll和/或固定高度的scroll您想要监听的scroll事件可能是由window对象触发的,而不是body

I think the best approach here is to drop the Backbone event binding (which is really just a shorthand, and only works on events within the view.el element) and bind directly to the window in initialize() :我认为这里最好的方法是删除 Backbone 事件绑定(这实际上只是一个速记,仅适用于view.el元素中的事件)并直接绑定到initialize()的窗口:

initialize: function() {
    _.bindAll(this, 'detect_scroll');
    // bind to window
    $(window).scroll(this.detect_scroll);
}

I think the problem is that Backbone uses event delegation to capture events, that is it attaches listeners to the this.$el , and that the scroll event does not bubble up by definition.我认为问题在于 Backbone 使用事件委托来捕获事件,即将侦听器附加到this.$el ,并且scroll事件不会根据定义冒泡。 So, if the scroll event occurs for a child (or descendant) of this.$el , then this event can not be observed at this.$el .因此,如果scroll事件发生在this.$el的子节点(或后代)上,则无法在this.$el处观察到此事件。

The reason it works for click , is just because click bubbles up.它适用于click的原因只是因为click冒泡。

The scroll bars for body and window are different and you have to make sure you aren't scrolling on the window object. body 和 window 的滚动条是不同的,你必须确保你没有在 window 对象上滚动。 Here is a jsfiddle that illustrates the issue you are probably encountering.这是一个 jsfiddle,它说明了您可能遇到的问题。

jsfiddle提琴手

I'm not for sure if you can change the 'el' to the document.window object, but I don't think that would be a good solution anyway.我不确定您是否可以将“el”更改为 document.window 对象,但我认为无论如何这都不是一个好的解决方案。 I would say your best bet is to either use CSS like I've done to help you with the body element or to create a div inside the body and reference that verse the body tag.我会说你最好的选择是要么像我一样使用 CSS 来帮助你处理 body 元素,要么在 body 内创建一个 div 并引用该 body 标签。

Good luck.祝你好运。

I had same issue and this is how I implemented it我有同样的问题,这就是我实现它的方式

var MyView = Backbone.View.extend({
el: $('body'),
initialize: function() {
    $(window).scroll(function() {
        if ($(window).scrollTop()!=0 && $(window).scrollTop() == $(document).height() - $(window).height()) {
            if (mpListModel.get('next')) {
                var res = confirm('Next page?');
                if (res==true) {
                    myView.fetch()
                }
            }
        }
    });
},
events: {},
fetch: function() {
    //fetch stuff
}

}); }); var myView = MyView(); var myView = MyView();

afterRender: function() {
    // <div id="page-content" class="page-content"> la class qui contiens le scroll
    var $scrollable = this.$el.find('.page-content'); 
    $scrollable.scroll(function() {
           alert("event scroll ");
    });
},

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

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