Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html5 #11

Open
uniquejava opened this issue Dec 27, 2015 · 1 comment
Open

html5 #11

uniquejava opened this issue Dec 27, 2015 · 1 comment

Comments

@uniquejava
Copy link
Owner

File API之读图

见html5 the missing manual 2nd edition page332.

把input file隐藏(太丑),给虚边框区域加onclick ,让他去调用input file的click方法,这样就会打开文件选择对话框,需要事先监听input file的change事件,利用HTML5的File API来处理获取的文件。

<div data-role="content">
    <div class="avatar">
        <div id="avatarImage"></div>
    </div>
    <input type="file" id="inputfile" class="inputfile"/>
</div>

read image

$('#inputfile').on('change', function () {
    var files = this.files;
    var file = files[0];

    var fileReader = new FileReader();
    fileReader.onload = function (e) {
        $('#avatarImage').css('background-image', 'url("' + e.target.result + '")');
    };
    fileReader.readAsDataURL(file);
});
$('#avatarImage').on('click', function () {
    $('#inputfile').click();

})

css

#avatarImage {
    width: 100px;
    height: 100px;
    border: dashed #eeeeee 2px;
    border-radius: 50%;
    background-size: 100%;
    background-repeat: no-repeat;
    color: gray;
    text-align: center;
}

#inputfile {
    display: none;
}
@uniquejava
Copy link
Owner Author

uniquejava commented May 1, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant