【问题标题】:Trouble using outerHeight() with backbone在骨干中使用 outerHeight() 时遇到问题
【发布时间】:2012-12-19 12:51:48
【问题描述】:

我正在尝试创建类似 pinterest 的效果,并通过wedding.reposition() 设置 div 的位置。但是,我认为 outerHeight() 函数不起作用,因为它返回 0px。我认为这是由于图像尚未加载,但我仅在相关 div 已加载并附加到 DOM 后才调用wedding.reposition()(附加是同步的?)。

有趣的是,如果我在几秒钟后通过 setTimeout 函数运行 Wedding.reposition,它就可以工作。只是想知道是否有人对此有解决方案?

//repositions all the elements on the page
wedding.reposition = function() {
  var $post = $(".post");
    //sets col position classes
    for(var i = 0, len = $post.length; i < len; i++) {
      //starts from base 1 rather than base 0
      var colClass = "col" + (i % 4 + 1);
      $post.eq(i).addClass(colClass);

      //implies it is on the second or greater row
      if(i >= 4) {
        var $col = $("." + colClass);
        //gets row of the element in the current column
        var row = Math.floor(i / 4);
        var $prev = $col.eq(row - 1);  

        //determines the height of the current object
        console.log($prev.outerHeight());
        var currentObjectHeight = $prev.position().top + $prev.outerHeight() + 15;
        $col.eq(row).css("top", currentObjectHeight);
      }
    };
};



//PostsView corresponds to the overall view holding individual PostView 
wedding.views.PostsView = Backbone.View.extend({
  el: "#column-container",

  initialize: function(){
    _.bindAll(this);
    this.collection.on("reset", this.render);

    this.collection.fetch();
  },

  render: function(){

    this.collection.each(function(model) {
      this.initializePostView(model);
    }, this);
    wedding.reposition();

  },

  initializePostView: function(model) {
    var html = new wedding.views.PostView({model: model});
    this.$el.append(html.render().el);

  }

});


wedding.views.PostView = Backbone.View.extend({

  className: "post",
  template: "#post-template",

  initialize: function(options) {
    _.bindAll(this);
    this.template = _.template($(this.template).html());
  },

  render: function() {
    this.preload();
    return this;
  },

  preload: function() {
    var image = new Image(); 
    var that = this;
    image.src = this.model.get("src");

    image.onload = function() {
      var html = that.template( {model: that.model.toJSON() });
      that.$el.append(html);
    };
  }
});

【问题讨论】:

    标签: jquery backbone.js outerheight


    【解决方案1】:

    虽然您可能会附加到 DOM,但这并不意味着图像会被下载。

    对图像使用load 事件,或waitForImages 等插件。

    【讨论】:

    • 我似乎无法让加载功能工作。例如'$("img").load(function(){ console.log("test") });'不运行?
    • @DanTang 需要更多上下文来理解原因。
    • 嗨亚历克斯,这就是我正在运行的 -> 我试图在所有图像加载后调用重新定位函数,但它似乎不起作用$(document).ready(function() { //fetches data from server, initializes views wedding.start(); $("img").load(function(){ console.log("test") wedding.reposition(); }); });
    • @DanTang load 事件需要在图像开始加载之前注册。
    • 很抱歉问了一个愚蠢的问题,但我该怎么做呢?我尝试先声明加载函数,然后调用wedding.start(),它也不起作用?
    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多