简体   繁体   English

根据文本长度动态更改项目的高度

[英]Dynamically change height of item based on length of text

I am working with cordova and jquery mobile, iam working on an app where I can send notifications. 我正在使用cordova和jquery mobile,IAM正在开发可以发送通知的应用程序。 In the notification panel I show all recieved notifications. 在通知面板中,我显示所有收到的通知。

So the html markup of that notification is like this: 因此,该通知的html标记如下所示:

 for(var count = 0; count < info.data.length; count++){
    var shortmessage = info.data[count][3];
    var category = info.data[count][4];
    var typeOf = info.data[count][5];
    var style = '';

    if(typeOf === "warning"){
        imgPath = 'img/icons/alarm.png';
    } else {
        imgPath = 'img/icons/alert.png';
    }

    console.log(shortmessage, shortmessage.length);

     // if long message, set the style
    if ( shortmessage.length > 40 ){
        style = 'min-height: 70px';
    }else if ( shortmessage.length > 65 ){
        style = 'min-height: 80px !important';
    }else if ( shortmessage.length > 75 ){
        style = 'min-height: 100px !important';
    }

if (category === 'vertrek'){
               departureHtml = `
                ${departureHtml}
                <div class='notification-item' style='${style}'>
                    <div class='ui-grid-a notification-grid'>
                        <div class='ui-block-a'>
                            <img class='notification-image' src='${imgPath}'>
                        </div>
                        <div class='ui-block-b'>
                            <span class='notification-text'>${shortmessage}</span>
                        </div>
                    </div>
                </div>`;

            $('.departure-notification-append').empty().prepend(departureHtml);
        }

        if (category === 'inchecken'){
            incheckHtml = `
                ${incheckHtml}
                <div class='notification-item' style='${style}'>
                    <div class='ui-grid-a notification-grid'>
                        <div class='ui-block-a'>
                            <img class='notification-image' src='${imgPath}'>
                        </div>
                        <div class='ui-block-b'>
                            <span class='notification-text'>${shortmessage}</span>
                        </div>
                    </div>
                </div>`;

            $('.check-in-notification-append').empty().prepend(incheckHtml);
        }

These are two of the 5 categories, but it seems to see that it is greater than 40 than it assigns the height of 70px to all of the notification-items. 这是5个类别中的两个类别,但似乎可以看到它大于40,比将height of 70pxheight of 70px分配给所有notification-items. height of 70px notification-items. But than it doesn't change the height of messages greater than 65 and 75. 但是,它不会改变大于65和75的消息高度。

So my question is how can I dynamically set the height of a notification-item based on the text inside notification-text 所以我的问题是如何根据notification-text的文本动态设置notification-item的高度

Setting height: auto will allow items to dynamically resize to occupy whatever height is necessary to render their contents. 设置height: auto将允许项目动态调整大小以占据呈现其内容所需的任何高度。

I would use padding to achieve additional height manipulations rather than mucking with the height property. 我将使用填充来实现其他高度控制,而不是使用height属性进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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