使用自定义的按钮替换默认的<input type='file'>
2014-02-13 12:47
218 查看
可以通过让默认的input type = 'file'按钮透明度变为0,并且让它刚好覆盖在自定义的按钮上,来实现此效果:
将它写成一个jQuery插件:
注意: 使用input.offset($(this).offset),会产生bug,常常出现两倍的左侧距离,还是使用.css方法
然后在页面中自定义一个上传按钮:
然后对该按钮调用扩展的browseElement方法:
完整代码在:
http://pan.baidu.com/s/1mgwQpew
将它写成一个jQuery插件:
(function($){ $.fn.browseElement = function(){ var input = $("<input type='file' multiple>"); input.css({ "position": "absolute", "z-index": 2, "cursor": "pointer", "-moz-opacity": "0", "filter": "alpha(opacity: 0)", "opacity": "0" }); input.mouseout(function(){ input.detach(); }); $(this).mouseover(function(){ input.css($(this).offset()); input.width($(this).outerWidth()); input.height($(this).outerHeight()); $("body").append(input); }); return input; }; })(jQuery);
注意: 使用input.offset($(this).offset),会产生bug,常常出现两倍的左侧距离,还是使用.css方法
然后在页面中自定义一个上传按钮:
#attach { width:100px; height:30px; border:1px solid #00B7FF; background:#cae2fd; border-radius:4px; color:#00B7FF; font-size:12px; line-height:30px; text-align:center; margin:auto; padding:0 } <div id="attach">选择文件</div>
然后对该按钮调用扩展的browseElement方法:
var hiddenInput = $('#attach').browseElement(); hiddenInput.change(function(){ //上传文件时相关处理代码 });
完整代码在:
http://pan.baidu.com/s/1mgwQpew
相关文章推荐
- js 在<input type='file'/>的使用中,控制文件的后缀jpg gif等
- FloatingActionButton(悬浮按钮)使用学习<一>
- IE7下按钮<input>不显示自定义背景图片
- 自定义UITabBar替换系统默认的(添加“+”号按钮)
- Android利用SparseArray替换使用HashMap<Integer,E>
- IE7下按钮<input>不显示自定义背景图片
- 可替换string的使用 & <xliff:g>标签介绍
- error C2648: “MyDoublyLinkedlist<int>::length”: 将成员作为默认参数使用要求静态成员
- JSP自定义标签中不能使用<%=request.getSession.getAttribute("attributeName")%>
- FloatingActionButton(悬浮按钮)使用学习<一>
- 泛型系列<4>使用相应的泛型版本替换Stack和Queue
- <转>ubuntu 12.04设置开机启动图形应用程序,替换默认图形桌面
- 仿新浪微博IOS客户端(v5.2.8)——自定义UITabBar替换系统默认的(添加“+”号按钮)
- 在 Android 中使用自定义函数接口替换 Frameworks 中的默认函数接口
- Android利用SparseArray替换使用HashMap<Integer,E>
- IE7下按钮<input>不显示自定义背景图片
- 使用MvcHandler设计自定义系统权限<下>
- IE7下按钮<input>不显示自定义背景图片
- Html中<Hr>标签、样式的使用和自定义设置
- 从零开始学android<RadioButton单选按钮的使用.七.>