上传图片(基于zepto.js)

效果如下:

1     <div class="otherPic">
2                             <div id="showOtherImage"></div>
3                             <span class="pull-left position_relative" id="openIdCardImg">
4                                 <span class="icon color-blue addPic"></span>
5                                 <input type="file" class="yy_inputFile" id="other_inputFile" name="reasonImg" />
6                             </span>
7                         </div>

html

 1 .basicInfo .item{ padding:.5rem .5rem 0; border-top:.3rem solid #eeeeee;}
 2 .basicInfo li{ overflow:hidden; margin-bottom:.5rem;line-height:2.1rem; border-bottom:1px solid #e3e3e3;}
 3 .basicInfo li:last-child{ border-bottom:none;}
 4 .basicInfo input[type="text"]{ height:2rem; line-height:2rem;}
 5 .basicInfo textarea{ height:8rem; line-height:1.5rem;}
 6 .basicInfo .otherPic{ min-height:3rem;}
 7 .basicInfo .otherPic .addPic{ height:3rem; line-height:3rem; font-size:3rem; margin-bottom:.5rem;}
 8 .basicInfo .otherPic img{ margin:0 .5rem .5rem 0; width:3rem; height:3rem; vertical-align:top; border:1px solid #ddd;}
 9 .basicInfo .yy_inputFile{ position:absolute; top:0; left:0; width:3rem; height:3rem; opacity:0;}
10 .basicInfo .aboutPic{ margin-bottom:.5rem; line-height:1.5rem; }

js:

  1     var img_arr = new Array();
  2         //相关图片
  3         $(page).on(‘change‘,‘#other_inputFile‘,function () {
  4             $(this).resizeImage({
  5                 that:this,
  6                 cutWid:‘‘,
  7                 quality:0.6,
  8                 limitWid:710,
  9                 success:function (data) {
 10                     var len = $(‘#showOtherImage‘).find(‘img‘).size();
 11                     img_arr[len] = data.base64;
 12                     var img = ‘<div class="position_relative display-inlineBlock" style="float:left;">‘ +
 13                         ‘<img src="‘ + img_arr[len]  + ‘">‘ +
 14                         ‘<span class="icon deletedImages" sid="‘ +len+ ‘" id="other_img_‘+len+‘"></span>‘+
 15                         ‘</div>‘;
 16                     $(‘#showOtherImage‘).append(img);
 17                     if(img_arr.length == 9){
 18                         $(‘#openIdCardImg‘).hide();
 19                         return false;
 20                     }
 21                 }
 22             });
 23             this.value = ‘‘;
 24         });
 25
 26         //删除相关图片
 27         $(page).on(‘click‘,‘.deletedImages‘,function () {
 28             var sid = $(this).attr(‘sid‘);
 29
 30             img_arr.splice(sid,1);
 31             $(this).parent().remove();
 32
 33             $(‘#showOtherImage‘).html(‘‘);
 34             for( var i=0; i < img_arr.length; i++) {
 35                 var img = ‘<div class="position_relative display-inlineBlock" style="float:left;">‘ +
 36                     ‘<img src="‘ + img_arr[i] + ‘">‘ +
 37                     ‘<span class="icon deletedImages" sid="‘ +i+ ‘" id="other_img_‘ +i+ ‘"></span>‘+
 38                     ‘</div>‘;
 39                 $(‘#showOtherImage‘).append(img);
 40             }
 41
 42             if(img_arr.length < 9){
 43                 $(‘#openIdCardImg‘).show();
 44             }else{
 45                 $(‘#openIdCardImg‘).hide();
 46             }
 47         });
 48
 49 /*
 50     * 裁剪图片
 51     * $(id).resizeImage({
 52      *  that:this,    //当前图片对象
 53      *  cutWid:‘‘,    //裁剪尺寸
 54      *  quality:0.6,    //图片质量0~1
 55      *  limitWid:400,   //最小宽度
 56      *  success:function (data) {
 57      *      do something...
 58      *  }
 59     * })
 60     *
 61     * */
 62     $.fn.resizeImage = function (obj) {
 63         var file = obj.that.files[0];
 64         var URL = window.URL || window.webkitURL;
 65         var blob = URL.createObjectURL(file);
 66         var base64;
 67
 68         var img = new Image();
 69         img.src = blob;
 70
 71         if(!/image\/\w+/.test(obj.that.files[0].type)){
 72             $.toast("请上传图片!",1000);
 73             return false;
 74         }
 75
 76         img.onload = function() {
 77             if(img.width < obj.limitWid){
 78                 $.toast(‘图片宽度不得小于‘+ obj.limitWid +‘px‘,1000);
 79                 return false;
 80             }
 81             var that = this;
 82
 83             //生成比例
 84             var w,scale,h = that.height;
 85             if(obj.cutWid == ‘‘){
 86                 w =  that.width;
 87             }else{
 88                 w = obj.cutWid;
 89             }
 90             scale = w / h;
 91             h = w / scale;
 92
 93             //生成canvas
 94             var canvas = document.createElement(‘canvas‘);
 95             var ctx = canvas.getContext(‘2d‘);
 96             $(canvas).attr({
 97                 width: w,
 98                 height: h
 99             });
100             ctx.drawImage(that, 0, 0, w, h);
101
102             // 生成base64
103             base64 = canvas.toDataURL(‘image/jpeg‘, obj.quality || 0.8);
104             var result = {
105                 base64:base64
106             };
107
108             //成功后的回调
109             obj.success(result);
110         };
111     };
时间: 2024-08-02 01:43:39

上传图片(基于zepto.js)的相关文章

基于zepto.js的模仿手机QQ空间的大图查看组件ImageView.js

调用方式 :ImageView(index,imgData)  --index参数 为图片默认显示的索引值,类型 为Number  --imaData参数 为图片url数组 ,类型为Array 使用之前要先引入 zepto.js 文件 ImageView.js具体代码如下: /* * ImageView v1.0.0 * --口袋蓝房网 基于zepto.js的大图查看 * --调用方法 ImageView(index,imgDada) * --index 图片默认值显示索引,Number --i

省市选择(基于zepto.js)

效果如下: 1 <div class="clList overflow-h mt75"> 2 <select class="pull-left cl-35 select01" id="cityParent"> 3 <option>请选择省</option> 4 </select> 5 <select class="pull-left cl-35 select02"

相册弹窗(基于zepto.js)

1 //放大图片 2 $(page).on('click','.popupImage img',function () { 3 var that = $(this); 4 that.popupImage({ 5 this:that, 6 width:"200px" 7 }) 8 }); 9 10 //相册弹窗 11 $.fn.popupImage = function (obj) { 12 var $this = obj.this; 13 var sj_w = $this[0].nat

zepto.js swipe实现触屏tab菜单

今天我们来说下zepto.js,有兴趣的朋友可以先进这个网站“http://zeptojs.com/” ,这个可以说是手机里的jquery,但是它取消了hover,加上了swipe及tap这两个触屏功能.前几天,有朋友告诉我百度基于zepto.js做了一个webapp的UI,大家也可以看下:“http://gmu.baidu.com/”,有点像jquery ui的东西,大家有兴趣可以学习啊... 我们今天,用zepto.js的swipe来实现新浪手机网的tab菜单,大家可以先看下新浪的手机版“h

zepto.js 处理Touch事件

处理Touch事件能让你了解到用户的每一根手指的位置,在touch事件触发的时候产生,可以通过touch event handler的event对象取到,如果基于zepto.js开发,一般是通过event.touches[0]来获取属性.重要属性如 下:clientX,clientY:触摸点相对于浏览器窗口viewport的位置;pageX,pageY: 触摸点相对于页面的位置;screenX,screenY:触摸点相对于屏幕的位置 ;identifier:touch对象的unique ID 你

Zepto.js touch,tap增加 touch模块深入分析

1. touch库实现了什么和引入背景 click事件在移动端上会有 300ms 的延迟,同时因为需要 长按 , 双触击 等富交互,所以我们通常都会引入类似 zepto 这样的库.zepto 中touch库实现了 'swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap' 这样一些功能. 2.touch库实现'swipe', 'swipeLeft',

zepto.js的touch模块

 touch库实现了什么和引入背景 touch模块是基于zepto.js的. click事件在移动端上会有 300ms 的延迟,同时因为需要 长按 , 双触击 等富交互,所以我们通常都会引入类似 zepto 这样的库.zepto 中touch库实现了 'swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap' 这样一些功能. 注意:事件名在此模块是驼峰

基于zepto的插件之移动端无缝向上滚动并上下触摸滑动

该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 公司的移动端项目是基于zepto的,有一个页面要求文字能够无缝地不停向上滚动,但查了网上的资料,大多都是基于jquery的,虽然稍作修改就可以用于移动端,但不能实现触摸上下翻滚.所以就去了zepto的官网查看其API,却发现如果要使用zepto的swipe()方法,需要引用其已经封装好的touch.js文件,我就赶紧引用了这个js文件,可在实际测试中,官网给出的touch.js文件

基于Zepto的Alert提示框开源框架Tiny-Alert

项目主页:http://shootyou.github.io/Tiny-Alert/ 什么是Tiny-Alert? 这是一个基于Zepto的提示框插件,在移动端使用会有更好的效果.它被设计成是移动端原生alert和confirm提示框的更美观替代品.同时它还实现了一个loading效果.它的核心代码参考了rDailog,在它基础上适配了zepto,去掉了移动端用不到的特性,另外让它看上去更美观了. 它有如下特性: 轻量级,代码量不超过300行,压缩后仅2k. 基于zepto更适合移动端. 支持回