zoukankan      html  css  js  c++  java
  • 前端模拟 图片上传---->>通过选取的图片获取其路径<<------

    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            div {
                
                position: relative;
                overflow: hidden;
                background: #EEE;
                 100%;
                height: 667px;
            }
    
            #bg, #mask-bg {
                position: absolute;
                 100%;
                height: 667px;
                background-size: cover;
            }
            #mask-bg{
                top:9px;
                mask-image: url(mask.png);
                -webkit-mask-image: url(mask.png);
            }
            input {
                height: 60px;
                margin-top: 20px;
            }
        </style>
    </head>
    <body>
    <div>
        <p id="bg"></p>
        <p id="mask-bg"></p>
    </div>
    <input type="file">
    
    <script>
        var input = document.querySelector('input'),
            bg = document.querySelector('#bg'),
            maskBg = document.querySelector('#mask-bg');
    
        input.onchange = function () {
            var src = getObjectURL(this.files[0]);
            setBg(src);
    
        };
    
        function setBg(src){
            bg.style.backgroundImage = 'url(' + src + ')';
            maskBg.style.backgroundImage = 'url(' + src + ')';
        }
    
        /**
         * 通过选择的文件获取其图片路径(blob)
         * @param file
         * @returns {*}
         */
        function getObjectURL(file) {
            var url = null;
            console.log(window.createObjectURL)
            console.log(window.URL)
            if (window.createObjectURL != undefined) {
                url = window.createObjectURL(file)
            } else if (window.URL != undefined) {
                url = window.URL.createObjectURL(file)
            } else if (window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(file)
            }
            return url
        }
    
    </script>
    </body>
  • 相关阅读:
    Vue ui创建vue-cli 3.0项目
    Vue中封装axios
    微信小程序自定义顶部导航
    linux离线安装docker
    解决websocket不能@Autowired注入问题
    springboot 连接 Access数据库
    Flex 布局教程:语法篇
    通过代码来了解下java策略模式,消除if-else
    docker下安装FastDFS
    centos7下安装docker
  • 原文地址:https://www.cnblogs.com/vali/p/6217236.html
Copyright © 2011-2022 走看看