Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108749
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 423
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-15 11:55
文章分类

全部博文(40)

文章存档

2016年(36)

2015年(2)

2013年(2)

我的朋友

分类: jQuery

2016-07-08 16:43:55

上传图片是做网站过程中经常会出现的功能,总结如下
1.uploadify插件,可以参考网上的例子,很多,但是不足是不支持移动端
2.webuploader,这个可以很好的支持移动端,而且网上有单张照片和多张照片上传的demo,下边介绍的是样式问题,现在手机流行的是微信类似的,点击加号添加照片,然后加号后移,继续点击加号添加照片,仿照的是weui里边的样式,这里做一个总结
html代码

点击(此处)折叠或打开

  1. <div id="uploader-demo">
  2.     <div id="fileList" class="uploader-list fl" style="position:relative">
  3.       <div id="filePicker" class="weui_uploader_input_wrp mt5"></div>
  4.     </div>
  5.   </div>
js代码

点击(此处)折叠或打开

  1. // 初始化Web Uploader
  2. var uploader = WebUploader.create({
  3.     // 选完文件后,是否自动上传。
  4.     auto: true,
  5.     fileNumLimit:9,
  6.     formData: {
  7.         'style': 'project_new',
  8.         'path' :
  9.         'city' :
  10.     },

  11.     // swf文件路径
  12.     //swf: BASE_URL + '/js/Uploader.swf',

  13.     // 文件接收服务端。
  14.     server: '',

  15.     // 选择文件的按钮。可选。
  16.     // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  17.     pick: '#filePicker',

  18.     // 只允许选择图片文件。
  19.     accept: {
  20.         title: 'Images',
  21.         extensions: 'gif,jpg,jpeg,bmp,png',
  22.         mimeTypes: 'image/*'
  23.     }
  24. });
  25. // 当有文件添加进来的时候
  26. uploader.on( 'fileQueued', function( file ) {
  27.     var $li = $(
  28.             '
    + file.id + '" class="file-item thumbnail">' +
  29.                 '' +
  30.                 '
    ' + file.name + '
    '
    +
  31.             '
'
  •             ),

  •         $img = $li.find('img');
  •     // $list为容器jQuery实例
  •     $("#filePicker").before( $li );
  •    // console.log($("#img"));
  •     
  •     // 创建缩略图
  •     // 如果为非图片文件,可以不用调用此方法。
  •     // thumbnailWidth x thumbnailHeight 为 100 x 100
  •     uploader.makeThumb( file, function( error, src ) {
  •         if ( error ) {
  •             $img.replaceWith('不能预览');
  •             return;
  •         }
  •         $img.attr( 'src', src );
  •     }, 100, 100 );
  • });
  • // 文件上传过程中创建进度条实时显示。
  • uploader.on( 'uploadProgress', function( file, percentage ) {
  •     var $li = $( "#fileList" ),
  •         $percent = $li.find('.progress span');

  •     // 避免重复创建
  •     if ( !$percent.length ) {
  •         $percent = $('

    '
    )
  •                 .appendTo( $li )
  •                 .find('span');
  •     }

  •    $percent.css( 'width', percentage * 100 + '%' );
  •    //$("#fileList")
  • });

  • // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  • uploader.on( 'uploadSuccess', function( file,data ) {
  •     $( "#fileList" ).addClass('upload-state-done');
  •     $('#fileList').appendTo("+data._raw+" name='project_new[url_list][]'>")
  • });

  • // 文件上传失败,显示上传出错。
  • uploader.on( 'uploadError', function( file ) {
  •     var $li = $( "#fileList" ),
  •         $error = $li.find('i.weui_icon_warn');

  •     // 避免重复创建
  •     if ( !$error.length ) {
  •         $error = $('').appendTo( $li );
  •     }

  •     $error.text('上传失败');
  • });

  • // 完成上传完了,成功或者失败,先删除进度条。
  • uploader.on( 'uploadComplete', function( file ) {
  •     $( "#fileList" ).find('.progress').remove();
  • });
  • })
  • css代码

    点击(此处)折叠或打开

    1. #filePicker,#fileList {
    2.     float: left;
    3. }
    4. .weui_uploader_input_wrp {
    5.     float: left;
    6.     position: relative;
    7.     margin-right: 9px;
    8.     margin-bottom: 9px;
    9.     width: 100px;
    10.     height: 100px;
    11.     border: 1px solid #D9D9D9;
    12. }
    13. .weui_uploader_input_wrp:before, .weui_uploader_input_wrp:after {
    14.     content: " ";
    15.     position: absolute;
    16.     top: 50%;
    17.     left: 50%;
    18.     -webkit-transform: translate(-50%, -50%);
    19.     transform: translate(-50%, -50%);
    20.     background-color: #D9D9D9;
    21. }
    22. .weui_uploader_input_wrp:before {
    23.     width: 2px;
    24.     height: 39.5px;
    25. }
    26. .weui_uploader_input_wrp:after {
    27.     width: 39.5px;
    28.     height: 2px;
    29. }
    30. .webuploader-pick {
    31.     width: 70px;
    32.     height: 80px;
    33.     background: none!important;
    34. }

    35. .file-item{
    36.   float:left;
    37.   margin-right:5px;
    38.   margin-top:5px;
    39. }
    当然之后还有一些细节需要修理,例如提示功能等,但是框架大概就这样了,在此基础上做稍许修改即可




    阅读(1285) | 评论(0) | 转发(0) |
    0

    上一篇:jquey,zepto点击定位到相应div

    下一篇:没有了

    给主人留下些什么吧!~~