function ResizableBoxWithLockAspect(element, config) {
    //需要调整尺寸的div
    let c = element;

    var aspect = c.offsetWidth / c.offsetHeight;
    var cfg = {
        top: true,
        right: true,
        bottom: true,
        left: true,
        topLeft: true,
        topRight: true,
        bottomRight: true,
        bottomLeft: true,
        minWidth: 100,
        maxWidth: c.parentElement.offsetWidth * 0.9,
    }
    if (!!config) {
        cfg = Object.assign(cfg, config);
    }

    // 是否开启尺寸修改
    let resizeable = false
    // 鼠标按下时的坐标，并在修改尺寸时保存上一个鼠标的位置
    let clientX, clientY
    // div可修改的最小宽高
    let minW = cfg.minWidth, minH = cfg.minHeight || (minW / aspect);
    let maxW = cfg.maxWidth, maxH = cfg.maxHeight || (maxW / aspect);
    // 鼠标按下时的位置，使用n、s、w、e表示
    let direc = ''

    c.addEventListener('mousemove', onMouseMove)
    c.addEventListener('mousedown', onMouseDown)

    // 鼠标松开时结束尺寸修改
    function onMouseUp() {
        document.body.removeEventListener('mousemove', onMouseMove)
        document.body.removeEventListener('mouseup', onMouseUp)
        c.addEventListener('mousemove', onMouseMove)
        resizeable = false
    }

    // 鼠标按下时开启尺寸修改
    function onMouseDown(e) {
        let d = getDirection(e)
        // 当位置为四个边和四个角时才开启尺寸修改
        if (d !== '') {
            resizeable = true
            direc = d
            clientX = e.clientX
            clientY = e.clientY

            // 鼠标移动事件交由body监听
            c.removeEventListener('mousemove', onMouseMove)
            document.body.addEventListener('mousemove', onMouseMove)
            // 鼠标松开事件
            document.body.addEventListener('mouseup', onMouseUp)
        }
    }

    // 鼠标移动事件
    function onMouseMove(e) {
        let d = getDirection(e)
        let cursor
        if (d === '') cursor = 'default';
        else cursor = d + '-resize';
        // 修改鼠标显示效果
        c.style.cursor = cursor;
        // 当开启尺寸修改时，鼠标移动会修改div尺寸
        if (resizeable) {
            // 鼠标按下的位置在右边，修改宽度
            if (direc.indexOf('e') !== -1) {
                resizeWidth(e.clientX - clientX);
                clientX = e.clientX
            }
            // 鼠标按下的位置在上部，修改高度
            if (direc.indexOf('n') !== -1) {
                resizeHeight(e.clientY - clientY);
                clientY = e.clientY
            }
            // 鼠标按下的位置在底部，修改高度
            if (direc.indexOf('s') !== -1) {
                resizeHeight(clientY - e.clientY);
                clientY = e.clientY
            }
            // 鼠标按下的位置在左边，修改宽度
            if (direc.indexOf('w') !== -1) {
                resizeWidth(clientX - e.clientX);
                clientX = e.clientX
            }
            function resizeHeight(increment) {
                if (direc.length > 1) {
                    // 四个角触发了两次
                    increment /= 2;
                }
                var height = Math.max(minH, c.offsetHeight + increment);
                height = Math.min(maxH, height);
                var width = height * aspect;
                updateSize(width, height);
            }
            function resizeWidth(increment) {
                if (direc.length > 1) {
                    // 四个角触发了两次
                    increment /= 2;
                }
                var width = Math.max(minW, c.offsetWidth + increment);
                width = Math.min(maxW, width);
                var height = width / aspect;
                updateSize(width, height);
            }
        }
        return false
    }

    function updateSize(width, height) {
        c.style.width = width + 'px';
        c.style.height = height + 'px';
        cfg.onResize && cfg.onResize(width, height);
    }

    // 获取鼠标所在div的位置
    function getDirection(ev) {
        let xP, yP, offset, dir;
        dir = '';

        xP = ev.offsetX;
        yP = ev.offsetY;
        offset = 16;

        if (yP < offset) dir += 'n';
        else if (yP > c.offsetHeight - offset) dir += 's';
        if (xP < offset) dir += 'w';
        else if (xP > c.offsetWidth - offset) dir += 'e';

        return dir;
    }
}