简体   繁体   中英

Phonegap css height % not working in android 4.2 - 4.3

I always use an android 5.0 phone and an android 4.4 phone to test my apps. they always work. Yesterday, i found an android 4.3 phone and test my app with it. they are not working properly - Height (%). Then i did the same thing in an android 4.2 phone to find out if it can works. but it's the same. what can i do to fix it?

I had that problem too, the size of a page and its content weren't known until the page was actually showed on the screen.

So what I did is to use the trigger custom event (with jquery): http://api.jquery.com/trigger/ .

I bind rules with custom event in JS like (using the standard size of the screen):

$('body').on('custom_event:elements_loaded',{'fsContainer':fsContainer,'elementToCenter':elementToCenter,'zoneToCenterElementIn':zoneToCenterElementIn},function(event){

            fsContainerHeight = ViewManagementTools.getValueFromPixelSizeString($('#'+event.data.fsContainer).css('height'));
            elementToCenterHeight = ViewManagementTools.getValueFromPixelSizeString($('#'+event.data.elementToCenter).css('line-height'));

            ztceiChild = $('body').find('[id='+event.data.zoneToCenterElementIn +'] div');
            zoneToCenterElementInHeight = ViewManagementTools.getValueFromPixelSizeString($(ztceiChild).css('height'));

            marginToApply = (zoneToCenterElementInHeight - elementToCenterHeight)/2;
            $('#'+event.data.zoneToCenterElementIn).parent().css('height',zoneToCenterElementInHeight+'px');
            $('#'+event.data.elementToCenter).css('margin-top',marginToApply+'px');
            $('#'+event.data.elementToCenter).css('height',elementToCenterHeight);
            if(fsContainerHeight<=elementToCenterHeight){
                $('#'+event.data.fsContainer).css('height',elementToCenterHeight + 2*marginToApply);
            } else {
                marginToApply = (fsContainerHeight - elementToCenterHeight)/2;
                $('#'+event.data.elementToCenter).css('margin-top',marginToApply+'px');

            }
        });

And then when the page show, or even on deviceready if you use standard size of your screen to layout things, you can trigger the event:

        $('body').trigger('custom_event:elements_loaded');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM