zoukankan      html  css  js  c++  java
  • input type=file

    (1)首先来说一下,如何让 <input type='file' >成为你想要的模样。

    最简单的方法就是在让<input type='file' >的透明度为0(完全透明),然后在input的下面设置自己的样式<div>,这样点击<div>时,由于input位于其上,因此就相当于是点了input。

    如:

    <div class='user_photo'>

      <p>上传头像</p>
      <input type='file' name ='photo' />

    </div>我们可以这样来设置样式

    .user_photo{
      width :100px;
      height: 100px;
      border-radius :5px;
      background-color :#eee;
      position :relative;

    }

    input[type='file']{
      opacity: 0;
      position :absolute;
      top :80px;
      width :100px;

    }
    p{
      position :absolute:
      top :78px:
      border-radius: 0px 0px 5px 5px;
      width :100px:
      height: 22px;
      background-color: rgba(0,0,0,0.5);
      color :#ff;f
      display: none;

    }
    .user_photo:hover p{

      display: block; 

    }

    当hover时的效果图:

    (2)如何获取input上传的file的路径,并展示图片

      需要用到html5的FileReader接口:

      

      

    以下面代码为例:

    <div class='user_photo'>
      <div class='user_img'></div>
      <p>上传头像</p>
      <input type='file' name ='photo' />
    </div>

      

     

    监听input[type='file']的change事件,

    获取file对象:var file=this.file[0];console.log(file);结果如下,

     由于无论读取成功或失败,方法并不会返回读取结果,这一结果存储在result属性中。

     当读取成功后(reader.onload)所以我们从this.result中拿到读取结果,插到需要的位置。

  • 相关阅读:
    win32: 静态控件(Static)
    malloc() 和 calloc()有啥区别
    win32: WM_PAINT 实现双缓冲缓图
    char 与 unsigned char的本质区别
    iconv: iconv_open(pToCharset, pFromCharset); 的附加参数//IGNORE
    c语言: 生成随机数
    深圳市住房公积金管理中心
    利用latex制作个人简历
    分布式系统概念与设计中文版(第三版)
    数据结构-红黑树
  • 原文地址:https://www.cnblogs.com/telnetzhang/p/5761945.html
Copyright © 2011-2022 走看看