Results 1 to 3 of 3

Thread: changing margins depending on the device

  1. #1
    Sencha Premium Member
    Join Date
    Aug 2011
    Posts
    168
    Answers
    1
    Vote Rating
    2
      0  

    Default changing margins depending on the device

    Is something like this possible?

    I'd like the margin-top of an image be half the image height... The image height can change according to the device though...

    I'm not a programmer... so my code isn't working obviously. Anyone got a solution?

    Code:
    var imgImg=document.getElementById('i');
    var imgHeight=imgImg.style.height;
    var halfHeight=imgHeight/2;
    
    
    templateTeam = new Ext.XTemplate(
      '<div class="storeDetailView"><img id="i" src="{img}" alt="" class="largepic" style="margin-top:-'+halfHeight+'px" />',
      '</div>'
    );
    Thanks

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,551
    Answers
    3931
    Vote Rating
    1271
      0  

    Default

    So you have to wait for the image to be rendered. Then you need to get the height of the image and then set the margin to what you need...

    Code:
    var el = Ext.get('imgId'),
        height = el.getHeight(),
        topMargin = height / 2;
    
    el.setStyle('margin-top', topMargin);
    Something like that may work.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha Premium Member
    Join Date
    Aug 2011
    Posts
    168
    Answers
    1
    Vote Rating
    2
      0  

    Default

    Thx but i'm missing something:

    Cannot call method 'getHeight' of null...
    So it's not finding my elements with id="i".

    I'm a bit confused, where exactly should i put this code?

    Code:
    var el = Ext.get('i');
        height = el.getHeight();
        topMargin = height / 2;
    el.setStyle('margin-top', topMargin);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •