js----事件案例----上次传头像

发布于:2022-12-14 ⋅ 阅读:(339) ⋅ 点赞:(0)

 

 

<!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>

        *{

            margin: 0;

            padding: 0;

            list-style: none;

        }

        html{

            font-size: 62.5%;

        }

        body{

            text-align: center;

        }

        .alert{

            padding-top:60px;

            font-size: 2.4rem;

            display: none;

        }

        .container{

            height: 100vh;

            background-color: deepskyblue;

            overflow: hidden;

        }

        @media screen and (min-width:968px){

            /* 非移动端 */

            .alert{

                display: block;

            }

            .container{

                display: none;

            }

        }

        .head_pic{

            width: 100px;

            height: 100px;

            margin: 80px auto;

            background-image: url(https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1663174800&t=fe61fc5006e51309ba1186376f8c02ea);

            background-size: cover ;

            background-position: center center;

            border-radius: 50%;

        }

        input{

            display: block;

            width: 100px;

            height: 100px;

            opacity: 0;

        }

    </style>

</head>

<body>

    <p class="alert">请使用移动端打开</p>

    <div class="container">

        <div class="head_pic">

            <input type="file">

        </div>

    </div>

</body>

<script>

    var ipt=document.querySelector('input')

    var dHeadPic=document.querySelector(".head_pic")

    ipt.οnchange=function(){

        // 选择的图片

        var file=this.files[0];

        // 创建一个文件读取对象

        var reader=new FileReader()

        reader.οnlοad=function(){

            // 读取完成后执行

            // reader.result   //读取文件的信息内容

            dHeadPic.style.backgroundImage=`url(${reader.result})`

        }

        reader.readAsDataURL(file)

    }

</script>

</html>

本文含有隐藏内容,请 开通VIP 后查看