input type=file选择图片按钮样式修改与图片预览

1 背景

在这里插入图片描述
通过上图我们可以看到input type=file按钮的默认样式,非常不美观,如果要自定义该按钮的样式,要如何实现呢?

2 方式1样式

input覆盖整个按钮区域,并且设置完全透明

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: relative;
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: #ccc;
            text-align: center;
        }
        
        #file {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
        }
    </style>
</head>
<body>
    <div>
        选取图片
        <input type="file" accept="image/*" id="file">
    </div>
</body>
</html>

3 方式2样式

input隐藏,通过label设置按钮样式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: #ccc;
            text-align: center;
        }

        #file{
            display: none;
        }
        
        label {
            display: block;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div>
        <input type="file" accept="image/*" id="file">
        <!-- 将label与input建立关系,点击label等于点击了input -->
        <label for="file">选取图片</label>
    </div>
</body>
</html>

4 图片预览

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
    $(function() {
        // input选取文件后会触发change事件
        $('#file').on('change', function(event) {
            console.log(event.target.files) //选取的文件对象数组

            // 通过FileReader读取文件对象信息
            var reader = new FileReader()
            reader.onload = function(e) {
                var img = new Image()
                img.src = e.target.result //读取结果会转为base64数据格式
                img.style.width = '200px'
                document.body.appendChild(img)
            }
            reader.readAsDataURL(event.target.files[0])
        })
    })
</script>

5 js动态创建input方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .file {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: #ccc;
            text-align: center;
        }
	</style>
</head>
<body>
    <div class="file">
        选取图片
    </div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
	$(function() {
		$('.file').on('click', function() {
                // 动态创建input元素,type为file,样式为隐藏,并追加到body中
                var input = $('<input/>')
                input.prop('type', 'file')
                input.css('display', 'none')
                $('body').append(input)
                // 监听选取文件触发的change事件
                input.on('change', function(event) {
                    console.log(event.target.files) //选取的文件对象数组
                    // 通过FileReader读取文件对象
                    var reader = new FileReader()
                    reader.onload = function(e) {
                        var img = new Image()
                        img.src = e.target.result //读取结果会转为base64数据格式
                        img.style.width = '200px'
                        document.body.appendChild(img)
                    }
                    reader.readAsDataURL(event.target.files[0])
                    // 完成选取文件后,从dom中自我删除
                    input.remove()
                })
                // 自动触发click事件去选取文件
                input.click()
            })
        })
    </script>
</body>
</html>

6 效果展示

在这里插入图片描述

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
修改input type=file按钮样式,可以使用CSS来实现。但是由于input type=file的设计,只有很少的CSS属性可以应用于它。所以,我们需要使用一些技巧才能改变它的外观。以下是一些实现方法: 方法一:使用label元素代替input元素,并使用CSS样式来美化label元素。 HTML代码: ``` <label for="file-upload" class="custom-file-upload"> <i class="fa fa-cloud-upload"></i>选择文件 </label> <input id="file-upload" type="file"/> ``` CSS代码: ``` .custom-file-upload { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; } .custom-file-upload:hover { background-color: #f5f5f5; } .custom-file-upload i { margin-right: 5px; } ``` 方法二:使用伪元素和背景图片来美化input元素。 HTML代码: ``` <input type="file" class="custom-file-input"/> <label class="custom-file-label" for="custom-file-input">选择文件</label> ``` CSS代码: ``` .custom-file-input { position: absolute; left: -9999px; } .custom-file-label { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; font-weight: normal; background-image: url('file-upload.png'); background-repeat: no-repeat; background-position: center; background-size: 16px; padding-left: 24px; } .custom-file-label:hover { background-color: #f5f5f5; } ``` 其中,file-upload.png是一个上传图标图片。 以上两种方法都可以实现美化input type=file按钮的效果。您可以根据自己的需求选择其中一种方法进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值