自定義input文件上傳樣式

前言

  文件上傳是咱們常常會用到的功能,可是原生的input樣式太醜了,能不能自定義一個input文件上傳樣式呢?我這裏寫了兩種方法,form表單提交跟ajax異步提交都沒有問題,自動上傳或者點擊上傳按鈕上傳也都沒問題ajax

 

效果

 

 

 

 

 

 

 

代碼編寫

 

方法1

    <!--
            方法1:
            div : 設置寬高、overflow:hidden;超出的部分被隱藏
            input : 設置層級z-index = 1;設置透明度opacity:0;設置相對定位position:relative;使兩個元素重疊
            i : 設置層級z-index = 0;(要比input小)設置相對定位position:relative;使兩個元素重疊
            利用div框出大小,input在i上面但透明度爲0,當咱們點擊i時實際上是點擊到了input但視覺上咱們只看到了i
        -->
        <h3>方法1:</h3>
        <form action="upload" method="post" enctype="multipart/form-data">
            <!-- 輔助div,框出顯示內容 -->
            <div style="width: 20px;height: 20px;overflow:hidden;">
                <!-- 實際的選擇文件input -->
                <input style="position:relative;z-index :1;opacity:0" onchange="change()" id="file" type="file"
                       name="file"/>
                <!-- 可視圖標 -->
                <i class="glyphicon glyphicon-open" style="position:relative;top:-22px;z-index :0;font-size: 20px;"></i>
            </div>
            <!-- 文件名 -->
            <br/><span id="fileName"></span>
            <!-- 表單提交按鈕 -->
            <br/><input id="but_submit" type="submit" value="上傳"/>
        </form>
        <script>
            function change() {
                //回顯文件名
                $("#fileName").text($("#file").val());
            }
        </script>

 

方法2

        <!--
            方法2:
            input : 設置層級display: none;  直接隱藏
            i : onclick="document.getElementById('file2').click();"  圖標的click觸發input的click
            直接隱藏input,設置圖標的click觸發input的click,從而達到咱們想要的效果
        -->
        <h3>方法2:</h3>
        <form id="uploadForm">
            <!-- 實際的選擇文件input -->
            <input style="display: none;" onchange="change2()" id="file2" type="file" name="file"/>
            <!-- 可視圖標 -->
            <i class="glyphicon glyphicon-open" style="font-size: 20px;"
               onclick="document.getElementById('file2').click();"></i>
            <!-- 文件名 -->
            <br/><span id="fileName2"></span>
        </form>
        <script>
            //自動上傳
            function change2() {
                //回顯文件名
                $("#fileName2").text($("#file2").val());
                //執行上傳
                var form = new FormData(document.getElementById("uploadForm"));
                $.ajax({
                    url: ctx + "/rack/upload",
                    type: "post",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (e) {
                        console.log(e);
                    }
                });
            }
        </script>

 

  controllerapp

    @PostMapping("upload")
    public ResultModel<Boolean> upload(MultipartFile file) {
        //文件名
        System.out.println(file.getOriginalFilename());
        return ResultModel.of(true);
    }

 

總結

  樣式仍是醜了一點,但這些都不重要,關鍵是掌握了方法再找個UI小姐姐來幫忙調試,就能夠作出任何想要的UI效果啦!異步

相關文章
相關標籤/搜索