【sgDragSize】自定义拖拽修改DIV尺寸组件,适用于窗体大小调整

这篇具有很好参考价值的文章主要介绍了【sgDragSize】自定义拖拽修改DIV尺寸组件,适用于窗体大小调整。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

【sgDragSize】自定义拖拽修改DIV尺寸组件,适用于窗体大小调整,Vue.js,JavaScript&TypeScript,前端,javascript,html

核心原理就是在四条边、四个顶点加上透明的div,给不同方向提供按下移动鼠标监听 ,对应计算宽度高度、坐标变化 【sgDragSize】自定义拖拽修改DIV尺寸组件,适用于窗体大小调整,Vue.js,JavaScript&TypeScript,前端,javascript,html

特性:文章来源地址https://www.toymoban.com/news/detail-655972.html

  1. 支持设置拖拽的最小宽度、最小高度、最大宽度、最大高度
  2. 可以双击某一条边,最大化对应方向的尺寸;再一次双击,则会恢复到原始大小

sgDragSize源码

<template>
    <div :class="$options.name" :disabled="disabled" draggable="false">
        <div :class="`resize-handle resize-${a}`" draggable="false" @mousedown.stop="clickResizeHandle(a)"
            @dblclick="dblclickResizeHandle(a, $event)" v-for="(a, i) in sizeIndexs" :key="i"></div>
    </div>
</template>
<script>
export default {
    name: 'sgDragSize',
    data() {
        return {
            tbHeight: 0,
            dragSizeIndex: '',
            originRect: {},
            dblclickOriginRect: {},
            sizeIndexs: [
                'top',
                'right',
                'bottom',
                'left',
                'top-left',
                'top-right',
                'bottom-left',
                'bottom-right',
            ],
        }
    },
    props: [
        "disabled",//屏蔽
        "taskbarHeight",//任务栏高度
        "minWidth",//拖拽的最小宽度
        "minHeight",//拖拽的最小高度
        "maxWidth",//拖拽的最大宽度
        "maxHeight",//拖拽的最大高度
    ],
    watch: {
        disabled: {
            handler(newValue, oldValue) {
                newValue && this.__removeWindowEvents();
            }, deep: true, immediate: true,
        },
        taskbarHeight: {
            handler(d) {
                this.tbHeight = d || 0;
            }, deep: true, immediate: true,
        },
    },
    destroyed() {
        this.__removeWindowEvents();
    },
    methods: {
        view_innerHeight() {
            return innerHeight - this.tbHeight;
        },
        clickResizeHandle(d) {
            this.dragSizeIndex = d;
            this.mousedown(d);
        },
        dblclickResizeHandle(d, $event) {
            let rect = this.$el.getBoundingClientRect();
            rect.width < innerWidth && rect.height < this.view_innerHeight() && (this.dblclickOriginRect = rect);
            this.dblResize(d, rect, $event);
        },
        __addWindowEvents() {
            this.__removeWindowEvents();
            addEventListener('mousemove', this.mousemove_window);
            addEventListener('mouseup', this.mouseup_window);
        },
        __removeWindowEvents() {
            removeEventListener('mousemove', this.mousemove_window);
            removeEventListener('mouseup', this.mouseup_window);
        },
        mousedown(e) {
            this.originRect = this.$el.getBoundingClientRect();
            this.originRect.bottomRightX = this.originRect.x + this.originRect.width;//右下角坐标.x
            this.originRect.bottomRightY = this.originRect.y + this.originRect.height;//右下角坐标.y
            this.$emit('dragStart', e);
            this.__addWindowEvents();
        },
        mousemove_window(e) {
            let { x, y } = e;
            let minWidth = this.minWidth || 50, minHeight = this.minHeight || 50, maxWidth = this.maxWidth || innerWidth, maxHeight = this.maxHeight || innerHeight;
            x < 0 && (x = 0), y < 0 && (y = 0), x > innerWidth && (x = innerWidth), y > this.view_innerHeight() && (y = this.view_innerHeight());
            let style = {};
            switch (this.dragSizeIndex) {
                case 'top-left':
                    style.left = x;
                    style.top = y;
                    style.width = this.originRect.bottomRightX - x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);
                    style.height = this.originRect.bottomRightY - y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);
                    break;
                case 'top':
                    style.left = this.originRect.x;
                    style.top = y;
                    style.width = this.originRect.width;
                    style.height = this.originRect.bottomRightY - y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);
                    break;
                case 'top-right':
                    style.left = this.originRect.x;
                    style.top = y;
                    style.width = x - this.originRect.x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);
                    style.height = this.originRect.bottomRightY - y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);
                    break;
                case 'left':
                    style.left = x;
                    style.top = this.originRect.y;
                    style.width = this.originRect.bottomRightX - x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);
                    style.height = this.originRect.height;
                    break;
                case 'right':
                    style.left = this.originRect.x;
                    style.top = this.originRect.y;
                    style.width = x - this.originRect.x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);
                    style.height = this.originRect.height;
                    break;
                case 'bottom-left':
                    style.left = x;
                    style.top = this.originRect.y;
                    style.width = this.originRect.bottomRightX - x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);
                    style.height = y - this.originRect.y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);
                    break;
                case 'bottom':
                    style.left = this.originRect.x;
                    style.top = this.originRect.y;
                    style.width = this.originRect.width;
                    style.height = y - this.originRect.y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);
                    break;
                case 'bottom-right':
                    style.left = this.originRect.x;
                    style.top = this.originRect.y;
                    style.width = x - this.originRect.x;
                    style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);
                    style.height = y - this.originRect.y;
                    style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);
                    break;
                default:
            }
            style.width > maxWidth && (style.width = maxWidth);
            style.height > maxHeight && (style.height = maxHeight);
            Object.keys(style).forEach(k => style[k] = `${style[k]}px`);
            style['transition-property'] = 'width,height';
            style['transition-duration'] = '0,0';
            this.$emit('dragging', { e, style });
        },
        dblResize(d, rect, e) {
            let style = {};
            switch (d) {
                case 'top-left':
                    break;
                case 'top':
                case 'bottom':
                    style.left = this.originRect.x;
                    style.top = rect.height >= this.view_innerHeight() ? this.dblclickOriginRect.y : 0;
                    style.width = this.originRect.width;
                    style.height = rect.height >= this.view_innerHeight() ? this.dblclickOriginRect.height : this.view_innerHeight();
                    break;
                case 'top-right':
                    break;
                case 'left':
                case 'right':
                    style.left = rect.width >= innerWidth ? this.dblclickOriginRect.x : 0;
                    style.top = this.originRect.y;
                    style.width = rect.width >= innerWidth ? this.dblclickOriginRect.width : innerWidth;
                    style.height = this.originRect.height;
                    break;
                case 'bottom-left':
                    break;
                case 'bottom-right':
                    break;
                default:
            }
            Object.keys(style).forEach(k => style[k] = `${style[k]}px`);
            style['transition-property'] = 'width,height';
            style['transition-duration'] = '0.1s,0.1s';
            this.$emit('dragging', { e, style });
        },
        mouseup_window(e) {
            this.$emit('dragEnd', e);
            this.__removeWindowEvents();
        },
    }
};
</script> 
<style lang="scss">
.sgDragSize {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    pointer-events: none;

    .resize-handle {
        position: absolute;
        z-index: 100;
        display: block;
        pointer-events: auto;
    }

    &[disabled] {
        .resize-handle {
            pointer-events: none;
        }
    }

    .resize-top {
        cursor: n-resize;
        top: -3px;
        left: 0px;
        height: 7px;
        width: 100%;
    }

    .resize-right {
        cursor: e-resize;
        right: -3px;
        top: 0px;
        width: 7px;
        height: 100%;
    }

    .resize-bottom {
        cursor: s-resize;
        bottom: -3px;
        left: 0px;
        height: 7px;
        width: 100%;
    }

    .resize-left {
        cursor: w-resize;
        left: -3px;
        top: 0px;
        width: 7px;
        height: 100%;
    }

    .resize-top-right {
        cursor: ne-resize;
        width: 16px;
        height: 16px;
        right: -8px;
        top: -8px;
    }

    .resize-bottom-right {
        cursor: se-resize;
        width: 20px;
        height: 20px;
        right: -8px;
        bottom: -8px;
        background: url('/static/img/desktop/Windows7/sgDragSize/resize_corner.png') no-repeat;
    }

    .resize-bottom-left {
        cursor: sw-resize;
        width: 16px;
        height: 16px;
        left: -8px;
        bottom: -8px;
    }

    .resize-top-left {
        cursor: nw-resize;
        width: 16px;
        height: 16px;
        left: -8px;
        top: -8px;
    }
}
</style>

应用

<template>
    <div>
        <div class="box" :style="style">
            <label>最小尺寸:宽度400px,高度200px</label>
            <sgDragSize @dragging="dragging" :minWidth="400" :minHeight="200" />
        </div>
    </div>
</template>
<script>
import sgDragSize from "@/vue/components/admin/sgDragSize";
export default {
    components: {
        sgDragSize,
    },
    data() {
        return {
            style: {
                height: '500px',
                width: '800px',
                left: '100px',
                top: '100px',
            },
        }
    },
    methods: {
        dragging({ style }) {
            this.style = style;
        },
    },
};
</script>
<style lang="scss" scoped>
.box {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #409EFF55;
    box-sizing: border-box;
    border: 1px solid #409EFF;

    label {
        user-select: none;
        color: #409EFF;
    }
}
</style>

到了这里,关于【sgDragSize】自定义拖拽修改DIV尺寸组件,适用于窗体大小调整的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue自定义h5video视频播放器进度条组件,可拖拽、跳转、倍速、全屏

    一个进度条组件控制多个视频的播放、进度调整。视频可点击全屏观看,唯一的进度条是某个指定视频的视频信息。 全屏 点击进度条跳转 拖动滑块 在菜鸟教程上有以下几个参数的详细解说,这张图忘记哪里看的了,如有认领可评论我贴链接 倍速 // 倍速 handleChangeSpeed(item)

    2024年02月12日
    浏览(65)
  • Naive UI 获取树tree完整选中树结构(通用方法,也适用于其他自定义组件)

    截止文章记录前,Naive UI 并未提供直接获取,与选中叶子节点相关的完整树结构数据方法,记录一下前端实现方法。 数据准备: 数据准备:树结构初始数据,选中相关的数据   实现步骤,一共四步,如下:  实现函数方法如下: 

    2024年04月13日
    浏览(29)
  • 【sgRectSelect】自定义组件:Vue实现拖拽鼠标圈选、划区域、框选组件:矩形区域选中checkbox,并回调相关选中、取消选中的操作。

    边框线虚线动画效果请参阅 边框虚线滚动动画特效_虚线滚动效果_你挚爱的强哥的博客-CSDN博客 【代码】边框虚线滚动动画特效。_虚线滚动效果 https://blog.csdn.net/qq_37860634/article/details/130507289   碰撞检测原理请前往  原生JS完成“一对一、一对多”矩形DIV碰撞检测、碰撞检查,

    2024年02月09日
    浏览(61)
  • VS+Qt设置窗口尺寸(二):窗体控件自适应窗口布局,自动调整大小

    VS版本:VS2019 QT版本:Qt5.12.3(msvc2017_64) 为了适配不同尺寸的显示屏,软件窗口需要调整大小,窗口内的控件尺寸也要适配窗口的大小。 本例重点讲述如何设置可调整尺寸的窗口及控件,实现窗口最大化和尺寸调节。 本例使用相对简单的按键和文本框来做示例,其他控件均可

    2023年04月23日
    浏览(65)
  • JS创建DIV,实现鼠标拖拽效果

    题目:用原生js动态创建一个div,大小随意,挂在body上,实现鼠标拖拽效果 需要用到的鼠标事件: 1.鼠标按下(onmousedown), 2.鼠标移动(onmousemove) 3.鼠标抬起(onmouseup) 第一步:创建容器(每个页面,最大的容器是body对象,所有dom对象创建后默认都会放到body) 第二步,

    2024年02月13日
    浏览(35)
  • vue实现在页面拖拽放大缩小div并显示鼠标在div的坐标

    实现在一个指定区域拖拽div,并可以放大缩小,同时显示鼠标在该div里的坐标,如图可示 缩小并拖动 js代码 css

    2024年02月05日
    浏览(39)
  • vue实现鼠标拖拽div左右移动的功能

    直接代码: 这部分区域可以鼠标拖拽左右滚动

    2024年02月03日
    浏览(37)
  • Qt程序设计-无边框可移动可拖拽调整大小窗体

    本文讲解Qt-无边框可移动可拖拽调整大小窗体。 通过鼠标的按下移动进行窗体的移动,拖拽调整窗体大小。 实现过程如下: 创建QWidget窗体,添加一个按钮控制窗体的关闭。

    2024年02月19日
    浏览(31)
  • Vue3中div自由拖拽宽度和高度。

    Vue3中我们会遇到自由拖拽宽度和高度的页面需求,查看很多方法都无法满足当前需求。下面是我们Vue3版本的代码,非常简单主要构想说粗发拖拽方法,把所需要的div的高宽进行拖拽位置进行监听来加减自身div的px值。直接复制粘贴就可以实现效果。根据自己需求更改即可投入

    2024年02月09日
    浏览(31)
  • vue2和vue3拖拽移动div

    直接上代码,代码可以直接运行, vue2拖拽移动div: vue3拖拽移动div: 设置div居中后,发现一开始拖拽时,div会跑到最左边,vue3优化代码如下:

    2024年02月07日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包