skip to Main Content

The documentation explains that it allows the ability to dynamically set the top or bottom values. However, the example is confusing, as it is both setting the bottom value as well as returning the value.

Should you return a value? Should you set a value? It goes against every fibre of my being for a function to set and return a value.

Example from docs:

$('#myAffix').affix({
  offset: {
    top: 100,
    bottom: function () {
      return (this.bottom = $('.footer').outerHeight(true))
    }
  }
})

2

Answers


  1. Chosen as BEST ANSWER

    From some research, it seems the return value of the affix callback functions are used to determine when the affix-top or affix-bottom is applied.

    However, the example code given on the bootstrap site is confusing, because they are also setting the this.bottom. Setting this.bottom is basically saying where it should be positioned, after its fixed.

    In my opinion, you should be using the events to listen to the change in state, and setting this.bottom, this.top, etc. And furthermore, setting a value and returning it, all on one line is just asking for future devs to your codebase to hate you for eternity.

    So in summary, the return value is "at what point the affix kicks in" and the this.bottom / this.top is merely a convenient way of setting the position.


  2. Twitter bootstrap’s JS & associated documentation have always been misleading and confusing. I’m not even sure the value of this in the example above is referring to the affix instance.

    From all indications in the source code, it looks to me like the return value is the one that matters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search