【鬥醫】【15】Web應用開發50天 之 易用性問題

來源:互聯網
上載者:User

標籤:高度   textarea   自適應   

在介紹HTML富文本使用之前,先解決幾個易用性的問題650) this.width=650;" src="http://img.baidu.com/hi/face/i_f03.gif" alt="i_f03.gif" />

1、在chrome瀏覽器中textarea高度自適應存在問題:當輸入任何一個字元時textarea高度都會增加

解決辦法:

(1)在challenge.js中定義autoAdaptHeight()方法,內容如下:

/**

 * textarea高度自適應

 */

function autoAdaptHeight(component){

    var paddingTop = parseInt($(component).css("padding-top"));

    var paddingBtm = parseInt($(component).css("padding-bottom"));

    var scrollHeight = component.scrollHeight;

    var height = $(component).height();


    // 判斷是否為chrome瀏覽器

    if(window.navigator.userAgent.indexOf("Chrome") > 0){

        if(scrollHeight - paddingTop - paddingBtm > height){

            $(component).css("height", scrollHeight);   

        }

        return;              

    }

    $(component).css("height", scrollHeight);

}


(2)修改initInputComponent()方法,使其調用autoAdaptHeight()方法

// 設定textArea高度自適應

dynamicItem.bind("keyup", function(event){

    autoAdaptHeight(this);

});


【備忘】:

1、在網上搜尋相關解決方案時,很多都存在此問題,對相容性做的稍差一些。網上也有一些組件,感興趣的可以看一下源碼

2、效果不好示範,這裡就不給出了



2、當戰書標題長度超長時沒有給出相應的提示

解決辦法:

(1)在challenge.js中定義setLengthHit()方法,內容如下:

/**

 * textarea長度超出時提示

 */

function setLengthHint(component){

    if(component.id == "challenge_title_id"){

        if(!component.value){

            return;

        }


        var titleId = $("#challenge_title_hint_id");

        if(component.value && component.value.length > 96){

            titleId.parent().show();

        } else {

            titleId.parent().hide();

        }


        titleId.text(component.value.length - 96);

    }

}


(2)修改initInputComponent()方法,使其調用setLengthHint()方法

dynamicItem.bind("keyup", function(event){

    // 設定textArea高度自適應

    autoAdaptHeight(this);

    // 長度超長時給出提示資訊

    setLengthHint(this);

});


效果如所示:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/2D/16/wKiom1OTQWji0RQIAAF5QKSjeas998.jpg" title="超長提示.png" alt="wKiom1OTQWji0RQIAAF5QKSjeas998.jpg" />


3、進入下戰書頁面,標題textarea沒有自動擷取滑鼠

解決辦法:

修改initInputComponent()方法,添加如下內容:

/**

 * 初始化文字框

 */

function initInputComponent(){        

    var textareaArray = new Array("challenge_title_id", "challenge_prescript_id", "challenge_challenger_id");

    // 進入頁面"標題textarea"擷取焦點

    $("#" + textareaArray[0]).focus();

    

    $.each(textareaArray, function(i, item){

        var dynamicItem = $("#" + item);

        // 綁定PlaceHolder

        bindPlaceHolder(dynamicItem);

        

        dynamicItem.bind("keyup", function(event){

            // 設定textArea高度自適應

            autoAdaptHeight(this);

            // 長度超長時給出提示資訊

            setLengthHint(this);

        });

    });

}


效果如所示:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/2D/17/wKioL1OTQNagMhM3AAEla2C4mvU735.jpg" title="自動擷取焦點.png" alt="wKioL1OTQNagMhM3AAEla2C4mvU735.jpg" />


【題外話】:

一個系統的好壞,真的不在於它使用了哪些牛的技術,對於使用者來講就是易用性,拿iphone來講它的app並不是全都好用,只是把其中的某個點做到了極致;facebook的成功亦是如此,它開始時就把其中的幾個點做的很體貼。


本文出自 “青客” 部落格,請務必保留此出處http://qingkechina.blog.51cto.com/5552198/1423592

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.