input file美化 --上传图片

来源:互联网 发布:信息录入系统源码 编辑:程序博客网 时间:2024/04/30 17:24

1.效果图:




2.html部分:

<div class="sp-page-box" style="margin-top:50px;">                         
                        <div class="sp-page-column span6">
                            <h4>图片上传</h4>
                            <br />
                            <div class="sp-page-column span1" style="line-height: 35px;">照片:</div>
                            <div class="sp-page-column span5">
                                <span class="sp-upload">
                                    <img class="sp-upload-photo" data-url="" alt="" style="width: 200px; height: 100px;" />
                                    <input type="file" id="btnfile" class="sp-upload-img" />
                                </span>
                            </div>
                        </div>
                    </div>


3.css部分:

.sp-upload{position:relative; display:inline-block; min-height:33px;overflow:hidden;vertical-align:middle; cursor:pointer;}

.sp-upload-img{position:absolute; right:0; top:0; font-size:100px; opacity:0; filter:alpha(opacity=0);cursor:pointer; width:100%;height:100%;}
.sp-upload-photo{float:left; outline:none; width:82px;height:82px;background:url('./icons/addPhotocenter.png') center center no-repeat #fff;border-radius:3px; cursor:pointer;}


4.js部分:

$(".sp-upload .sp-upload-img").change(function () {
            var file = this.files[0];
            if (file.size > 52428800) {
                alert("图片大小不大于50M");
                file = null;
                return false;
            }
            var fileName = file.name;
            var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            if (postfix != "jpg" && postfix != "png") {
                alert("图片仅支持png、jpg类型的文件");
                fileName = "";
                file = null;
                return false;
            }


            var fileUrl = $(this).val();
            $(this).parent().children(".sp-upload-photo").attr("data-url", fileUrl);
            var getimg = $(this).parent().children(".sp-upload-photo");
            var filereader = new FileReader();
            filereader.readAsDataURL(file);
            $(filereader).load(function () {
                getimg.attr("src", this.result);
            });
        });

0 0
原创粉丝点击