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 Unanswered: 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,336
    Answers
    3885
    Vote Rating
    1297
      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!

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

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  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
  •