【问题标题】:jQueryMobile shows wrong height() valuejQueryMobile 显示错误的 height() 值
【发布时间】:2013-10-22 20:05:24
【问题描述】:

我有一个非常简单的 jQM 应用程序的主页/索引屏幕/页面标记:

<div id="home" class="container" data-role="page">
    <div class="grid-quarter">
        <a class="ui-link">
            <span class="grid-icon-container">
                <span class="grid-icon icon-lock"></span>
                <span class="grid-icon-text">LABEL TEXT</span>
            </span>
        </a>
    </div>
    <!-- 3 x the same -->
</div>

问题

现在这在 jsFiddle 上运行良好,但是一旦我在我的 jQM 应用程序中抛出完全相同的代码,js .height() 函数(以及其他所有尝试获取元素的高度)都会失败。

我编写了一个函数,它会检查每一个比特来确定高度,并在 jQM 中失败。即使我试图直接从 DOM 对象中获取它,它也会失败。甚至当我看到 DOM 对象具有精确的高度设置时。有趣的是:.width() 输出正确。

我不知道从哪里开始调试这个问题。任何帮助表示赞赏。谢谢。


测试代码

// Trigger on Load and Resize 
$( '#home' ).on( 
     'pageinit'
    ,function( event )
    {
        fitIconFont( '.icon-font' );
    }
);
// Fit on Window size change
$( window ).resize( function()
{
    fitIconFont( '.icon-font' );
} );

function fitIconFont( class_name )
{
    // DEBUG
    jQuery( class_name ).each( function( index, element ) 
    {
            // WORKS:
        $( this ).on( 'click', function () 
        {
            console.log( $( this ).height() );
        } );
        console.log( $( element ).css('width') );
        console.log( $( element ).width() );

            // FAILS with 20px as output
        console.log( $( element ).css('height') );
        console.log( $( element ).height() );
        console.log( element.scrollHeight );
        console.log( element.offsetHeight );
        console.log( element.clientHeight );
        console.log( element );

        console.log( $( this ).height() );
        console.log( $( this ).css('height') );
        console.log( this.scrollHeight );
        console.log( this.offsetHeight );
        console.log( this.clientHeight );
        console.log( this );
    } );
}

Chrome 开发栏截图

Link to jsFiddle example

【问题讨论】:

  • 可能是 CSS 问题,您可以发布任何自定义 CSS 吗?创建小提琴时,是否包含 CSS?
  • @JonathanRowny 是的,我没有包含 CSS,因为它在 jsFiddle 上与本地开发环境中的 exact 相同。

标签: javascript jquery css jquery-mobile height


【解决方案1】:

当您在 JSFiddle 中运行此代码时,您可能没有弄乱左侧关于何时加载 JavaScript 的设置(位于框架选择下拉菜单的正上方)。默认情况下,它设置为onLoad,相当于$(window).on('load', ...)。当window.load 触发时,所有资源都将正确加载和设置样式,以便您获得准确的高度。

它在click 事件处理程序中工作,因为页面上的所有资产都已加载,您可以获得准确的读数。 width 可能会被正确报告,因为它在页面完全加载后不会更改,这是通常发生的情况,因为默认情况下块级元素占用 100% 的宽度。

我尝试在 JSFiddle 中使用 no wrap (body) 设置,以便代码在我通常放置代码的位置(文档底部)同时运行。

【讨论】:

  • +1 出于某种原因,即使no wrap (body) 在小提琴中工作。我会在 Q 的底部设置一个指向小提琴的链接,所以你可以看看。关键是我已经纠正了我的错误:从document ready 移动到'pageinit'。我仍然得到错误的输出。
【解决方案2】:

HTML

<div id="header">
    <h1>Header</h1>
</div>
<div id="home" data-role="page" class="container" style="background-color: #aaa;">
    <div class="grid-half " style="background-color: #009ee0;">
        <a href="#" class="ui-link">
            <span class="grid-icon-container">
                <span class="icon-font icon-a"></span>
                <span class="grid-icon-text">Label A</span>
            </span>
        </a>
    </div>
    <div class="grid-half last" style="background-color: #FF00FF;">
        <a href="#" class="ui-link">
            <span class="grid-icon-container">
                <span class="icon-font icon-b"></span>
                <span class="grid-icon-text">Label B</span>
            </span>
        </a>
    </div>
    <div class="grid-half btop" style="background-color: #FFFF00;">
        <a href="#" class="ui-link">
            <span class="grid-icon-container">
                <span class="icon-font icon-c"></span>
                <span class="grid-icon-text">Label C</span>
            </span>
        </a>
    </div> 
    <div class="grid-half last btop" style="background-color: #000;">
        <a href="#" class="ui-link">
            <span class="grid-icon-container">
                <span class="icon-font icon-d"></span>
                <span class="grid-icon-text">Label D</span>
            </span>
        </a>
    </div>       
</div>

脚本

function fitIconFont( elements ) 
{
    $( elements ).each( function( key, el )
        {
            var 
                container         = $( el ).parent()
                container_height = parseInt( $( container ).outerHeight( true ), 0 )
                new_icon_height  = parseInt( container_height * 0.75, 0 ) + 'px'
                new_label_height = parseInt( container_height * 0.25, 0 ) + 'px'
            ;

            // Set font-sizes for icon & label
            $( el ).css( 'font-size', new_icon_height );
            $( el ).next().css( 'font-size', new_label_height );

            // Debug print
            $( '#header' ).text( 'current heights: Container: ' + container_height + ' / ' );
            $( '#header' ).append( 'Icon: ' + new_icon_height + ' / ' );
            $( '#header' ).append( 'Label: ' + new_label_height );
        }
    );
}

// Trigger on Load and Resize 
// Fit on Window load
$( window ).on( 'load' )
{
    fitIconFont( '.icon-font' ) ;
}
// Fit on Window size change
$( window ).resize( function()
{
    fitIconFont( '.icon-font' );
} );

// Fit on Page Show
jQuery( '#home' ).on( 
     'pageshow'
    ,function( event )
     {
        fitIconFont( '.icon-font' );
     }
).trigger( 'updatelayout' );

// Fit on Orientation Change
jQuery( '#home' ).on( 
     'orientationchange'
    ,function( event )
     {
        fitIconFont( '.icon-font' );
     }
).trigger( 'updatelayout' );

/**
 * Update grid container height/width on change of device orientation
 */
$( '#home' ).on( 
    'orientationchange'
    ,function( e )
     {
         $( '.grid-half' ).each( function( index, el )
         {
             //if ( 'portrait' !== e.orientation )
             $( el ).width( window.innerWidth * .5 );
             $( el ).height( window.innerHeight * ( 'portrait' !== e.orientation ? .6 : .5 ) );
         } );
     }
);

CSS

body, html { 
    height: 100%; 
    margin: 0;
    padding: 0; 
    font-size: 100%;
}
#header {
    height: 10%;
    width: 100%;
    line-height: 1;
    font-weight: bold;
}
#header {
    text-align: center;
}
#home {
    width: 100%;
    height: 90%;
}
    /* Grid */
    .grid-half {
        display: table;
        position: relative;
        float: left;
        width: 49.8%;
        height: 50%;
    }
    .grid-half.last {
        border-left: 1px dashed white;
    }
    .grid-btop {
        border-top: 1px dashed white;
    }
        .grid-half .ui-link {
            display: table-cell;
            position: absolute;
            height: 100%;
            width: 100%;
            vertical-align: middle;
            text-align: center;
            text-decoration: none;
        }
        .grid-half .grid-icon-container {
            display: block;
            position: relative;
            top: 25%;
            margin: 0 auto;
            height: 50%;
            width: 50%;
            border: 1px dotted #ddd;
        }
        .grid-half .icon-font {
            margin: 0 auto;
            position: relative;
            display: block;
            height: 75%;
            width: 100%;
            background-color: #bdbddb;
        }
        .grid-icon-text {
            display: block;
            position: relative;
            height: 25%;
            width: 100%;
            margin: 1% auto 0;
            text-align: center;
            background-color: #fff;
        }

/* Icons */
[class^="icon-"]:before,
[class*=" icon-"]:before {
    font-family: Arial;
    font-weight: normal;
    display: inline-block;
    text-decoration: inherit;
    text-align: center;
}
.icon-a:before     { content: "C"; color: #FFFF00; }
.icon-b:before     { content: "M"; color: #009ee0; }
.icon-c:before     { content: "Y"; color: #000; }
.icon-d:before     { content: "K"; color: #FFFF00; }

/* Style */
#header,
a {
    color: #333;
    font-family: Helvetica, Arial, sans-serif;
    text-decoration: none;
}

示例:http://jsfiddle.net/kaiser/qAcSR/

【讨论】:

    【解决方案3】:

    出于某种原因,整个事情只有在同时添加on( 'pageinit' ) AND ( document ).ready() 时才有效。

    不知道为什么,但这种方式会在桌面设备和移动设备上触发。有趣的是:如果我只使用两者之一:Nada,什么都没有,nix 有效。

    【讨论】:

    • $(document).ready() 的正确替换不是 $(document).on('pageshow') 吗?!
    • @tronc 试一试
    • @tronc 自从我从事这项工作以来已经很久了。在pageshow 上实现that I already had it there。不过,如果你想添加它单独的答案......
    猜你喜欢
    • 2012-05-22
    • 2014-10-31
    • 2012-11-06
    • 2019-11-16
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多