DOM Element Width before Appended to DOM

后端 未结 6 1719
渐次进展
渐次进展 2020-12-09 02:27

I\'m sure the answer is no, but is it possible to determine the width of an element before it is appended to the DOM?

Once it\'s appended, I know I

6条回答
  •  遥遥无期
    2020-12-09 03:20

    The Mootools Element.Measure functionality that Oscar mentioned is awesome. For those that use jQuery, here's a quick plugin that accomplishes the same thing:

    $.fn.measure = (fn)->
      el = $(this).clone(false)
      el.css
        visibility: 'hidden'
        position:   'absolute'
      el.appendTo('body')
      result = fn.apply(el)
      el.remove()
      return result
    

    You can call it like this, making sure to return the value (thanks Sam Fen for pointing that out!):

    width = $('.my-class-name').measure( function(){ return this.width() } )
    

提交回复
热议问题