【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放

简介: 【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放


特性:

  1. 支持缩放瓦片图,定义瓦片图初始缩放比例,以鼠标所在位置为中心缩放
  2. 支持局部拖拽加载

sgTileImage源码

<template>
    <div :class="$options.name">
        <div class="sg-ctrl">
            <label>缩放百分比</label>
            <el-input-number style="width: 150px;" v-model.trim="scaleValue" :precision="0" :step="10" :min="10" :max="100"
                :controls-position="`left`" @change="changeScaleValue" />
        </div>
        <div class="sg-tile-img" ref="scrollContainer">
            <div ref="dragContainer" :style="{ width: `${tileSize * colCount}px` }">
                <img v-for="(a, i) in tiles" :key="i" :ref="`tile${i}`" :loaded="a.loaded" :width="tileSize"
                    :height="tileSize" draggable="false">
            </div>
        </div>
        <sgDragMoveTile :data="dragMoveTileData" @dragMove="dragMove" />
    </div>
</template>
<script>
import sgDragMoveTile from "@/vue/components/admin/sgDragMoveTile";
export default {
    name: 'sgTileImage',
    components: {
        sgDragMoveTile
    },
    data() {
        return {
            dragMoveTileData: {},
            scaleValue: 100,
            orginTileSize: 0,
            tileSize: 0,
            colCount: 0,
            rowCount: 0,
            tiles: [],//瓦片图数组
            mousePoint_bk: null,
        }
    },
    props: [
        "data",
        /* data格式:{
            orginTileSize:500,//瓦片图原始尺寸
            colCount: 20,//行数
            rowCount: 20,//列数
            scaleValue: 100,//缩放百分比
            tiles: [],//瓦片图数组
        } */
    ],
    watch: {
        data: {
            handler(newValue, oldValue) {
                if (newValue && Object.keys(newValue).length) {
                    newValue.orginTileSize && (this.orginTileSize = newValue.orginTileSize);
                    newValue.colCount && (this.colCount = newValue.colCount);
                    newValue.rowCount && (this.rowCount = newValue.rowCount);
                    newValue.scaleValue && (this.scaleValue = newValue.scaleValue);
                    newValue.tiles && (this.tiles = newValue.tiles);
                    this.$nextTick(() => { this.loadScreenViewTiles(); });
                }
            },
            deep: true,//深度监听
            immediate: true,//立即执行
        },
        scaleValue: {
            handler(newValue, oldValue) {
                this.tileSize = this.orginTileSize * newValue / 100;
                this.$nextTick(() => { this.loadScreenViewTiles(); });
            },
            deep: true,//深度监听
            immediate: true,//立即执行
        },
    },
    destroyed() {
        removeEventListener('mousewheel', this.mousewheel);
    },
    mounted() {
        this.dragMoveTileData = {
            scrollContainer: this.$refs.scrollContainer,
            dragContainer: this.$refs.dragContainer,
        }
        let rect_scrollContainer = this.$refs.scrollContainer.getBoundingClientRect();
        this.mousePoint_bk = {
            x: rect_scrollContainer.width / 2,
            y: rect_scrollContainer.height / 2,
        };
        setTimeout(() => {
            this.loadScreenViewTiles();
            addEventListener('mousewheel', this.mousewheel, { passive: false });
        }, 1000);
    },
    methods: {
        // 校正放大缩小后,瓦片图的坐标(目的是为了让缩放看起来是以鼠标坐标为中心点)
        centerPosition(e, {
            rect_dragContainer_orign,
            scrollLeft_orgin,
            scrollTop_orgin,
            mousePoint }) {
            let scrollContainer = this.$refs.scrollContainer;
            let dragContainer = this.$refs.dragContainer;
            let rect_dragContainer = dragContainer.getBoundingClientRect();
            let scale = rect_dragContainer.width / rect_dragContainer_orign.width;//缩放比例
            let mouse_left_dis_orgin = scrollLeft_orgin + mousePoint.x;//缩放前鼠标距离瓦片图最左侧的距离
            let mouse_top_dis_orgin = scrollTop_orgin + mousePoint.y;//缩放前鼠标距离瓦片图最顶部的距离
            let mouse_left_dis = mouse_left_dis_orgin * scale;//缩放后鼠标距离瓦片图最左侧的距离
            let mouse_top_dis = mouse_top_dis_orgin * scale;//缩放后鼠标距离瓦片图最顶部的距离
            let scrollLeft = mouse_left_dis - mousePoint.x;
            let scrollTop = mouse_top_dis - mousePoint.y;
            scrollContainer.scrollLeft = scrollLeft;
            scrollContainer.scrollTop = scrollTop;
            this.mousePoint_bk = mousePoint;
        },
        mousewheel(e) {
            // 记录缩放前的数据
            let rect_dragContainer_orign = this.$refs.dragContainer.getBoundingClientRect();
            let scrollContainer = this.$refs.scrollContainer;
            let mousePoint = { x: e.x, y: e.y };
            let scrollLeft_orgin = scrollContainer.scrollLeft;
            let scrollTop_orgin = scrollContainer.scrollTop;
            // 开始缩放
            e.deltaY < 0 && (this.scaleValue += 10);
            e.deltaY > 0 && (this.scaleValue -= 10);
            // 开始计算坐标
            this.$nextTick(() => {
                this.centerPosition(e, {
                    rect_dragContainer_orign,
                    scrollLeft_orgin,
                    scrollTop_orgin,
                    mousePoint
                })
            });
            e.preventDefault && e.preventDefault();//阻止默认的滚轮事件
            return false;
        },
        // 获取浏览器可视范围的瓦片图,并加载图片
        loadScreenViewTiles(d) {
            let scrollContainer = this.$refs.scrollContainer;
            if (scrollContainer) {
                let rect_scrollContainer = scrollContainer.getBoundingClientRect();
                this.tiles.forEach((v, i) => {
                    let tile = this.$refs[`tile${i}`];
                    if (tile) {
                        tile = tile[0];
                        let rectTile = tile.getBoundingClientRect();
                        if (
                            rectTile.x + rectTile.width > rect_scrollContainer.x - rectTile.width &&
                            rectTile.y + rectTile.height > rect_scrollContainer.y - rectTile.height &&
                            rectTile.x < rect_scrollContainer.x + rect_scrollContainer.width &&
                            rectTile.y < rect_scrollContainer.y + rect_scrollContainer.height
                        ) {
                            tile.onload = d => { v.loaded = true; };
                            tile.src = v.img;
                        }
                    }
                });
            }
        },
        dragMove(d) {
            this.loadScreenViewTiles();
        },
        changeScaleValue(d) {
            this.mousewheel(this.mousePoint_bk);
        },
    },
};
</script>
<style lang="scss" scoped> .sgTileImage {
     .sg-ctrl {
         position: absolute;
         left: 10px;
         top: 10px;
         z-index: 1;
         box-sizing: border-box;
         padding: 10px 20px;
         background-color: white;
         box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
         border-radius: 4px;
         display: flex;
         align-items: center;
         justify-content: flex-end;
         label {
             margin-right: 10px;
         }
     }
     .sg-tile-img {
         position: absolute;
         left: 0;
         top: 0;
         overflow: auto;
         width: 100%;
         height: 100%;
         div {
             display: flex;
             flex-wrap: wrap;
             img {
                 border: none;
                 opacity: 0;
                 transition: opacity .382s;
                 &[loaded] {
                     opacity: 1;
                 }
             }
         }
     }
 }
</style>

用例

<template>
  <div>
    <sgTileImage :data="tileImageData" />
  </div>
</template>
<script>
import sgTileImage from "@/vue/components/admin/sgTileImage";
export default {
  components: { sgTileImage },
  data() {
    return {
      tileImageData: {
        orginTileSize: 500,//瓦片图原始尺寸
        colCount: 20,//行数
        rowCount: 20,//列数
        tiles: [],//瓦片图数组
      }
    }
  },
  created() {
    this.tileImageData.tiles = [...Array(this.tileImageData.colCount * this.tileImageData.rowCount)].map((v, i) => ({
      loaded: false,
      img: `http://shuzhiqiang.com/tiles/${i}.jpg`,
    }));//瓦片图数组
  },
};
</script>

依赖组件

image.png


相关文章
|
8月前
|
存储 人工智能 弹性计算
阿里云企业新用户定义及最新优惠内容政策完整解析
2026年阿里云企业新用户政策以**资格清晰、福利全面、覆盖场景广**为特点,从低价服务器、算力补贴到AI支持、出海扶持、迁云补助形成完整扶持体系。企业只需完成企业实名认证、确保账号无历史付费记录,即可领取对应优惠,以极低成本完成上云、扩产、出海与AI落地。
786 4
|
11月前
|
Windows
geek卸载软件怎么安装?详细安装步骤
geek.exe是一款轻量级Windows清理优化工具,专用于卸载软件、清除残留文件及注册表垃圾。下载后需核对文件名与大小,安装时建议更改路径至非系统盘,注意取消捆绑软件勾选,完成后可快速启动使用,操作简单高效。(238字)
2122 3
|
4月前
|
Web App开发 JavaScript 前端开发
指纹浏览器VS虚拟机技术对比:多账号环境隔离方案技术解析(2026)
本文通过义乌卖家赵杰账号被平台批量关联的真实案例,深入剖析虚拟机与指纹浏览器的本质差异。指出虚拟机虽隔离系统,却暴露GPU、时钟、MAC等虚拟化特征;而指纹浏览器通过内核级修改,精准伪造Canvas、WebGL等50+浏览器参数,兼顾安全性、资源效率与批量管理能力。
|
6月前
|
人工智能 前端开发 架构师
2026 年度孵化 OPC(超级个体)项目的 AI 编码工具硬核评测
在软件工程全栈门槛被 AI 彻底消融的今天,一个人活成一支队伍的“超级个体(OPC)”商业模式已进入全面爆发期。本文聚焦独立创客与 OPC 项目孵化的核心诉求,深度评测全球主流 AI 自动编程智能体在跨库多任务并行、白盒可控性及全栈商业化交付中的真实表现,为您提供冷启动阶段的最优选型。
402 2
|
7月前
|
JSON JavaScript 调度
流式加载 / 按需加载技术
流式加载 / 按需加载技术
|
10月前
|
Web App开发 前端开发 安全
豫唐工具集正式发布:重新定义前端开发工具体验
2026年2月26日,豫唐团队发布开源免费的“豫唐工具集”——轻量纯前端在线平台,含ICO生成、摸鱼神器、密码生成、项目管理计算器与数独游戏五大工具,本地运行、零数据上传,兼顾安全、效率与极简美学。
665 6
|
监控 Java 调度
SpringBoot中@Scheduled和Quartz的区别是什么?分布式定时任务框架选型实战
本文对比分析了SpringBoot中的`@Scheduled`与Quartz定时任务框架。`@Scheduled`轻量易用,适合单机简单场景,但存在多实例重复执行、无持久化等缺陷;Quartz功能强大,支持分布式调度、任务持久化、动态调整和失败重试,适用于复杂企业级需求。文章通过特性对比、代码示例及常见问题解答,帮助开发者理解两者差异,合理选择方案。记住口诀:单机简单用注解,多节点上Quartz;若是任务要可靠,持久化配置不能少。
1452 4
|
自然语言处理 安全 程序员
程序员推荐的12款实用模板免费的WordPress主题
本文介绍了12款实用的免费WordPress主题,包括Sydney、PopularFX、Zakra等,涵盖企业、博客、美食等多种风格。这些主题功能丰富,如自定义布局、预制模板、AMP兼容等,适合不同需求的用户。每款主题均有精美预览图展示,建议收藏以备后续使用。更多主题可访问ztmao.com。
1066 2
程序员推荐的12款实用模板免费的WordPress主题
|
传感器 人工智能 安全
万物联网
万物互联,智启未来。物联网正重塑生活与产业:从智能家居到智慧工厂,从城市治理到精准农业,通过感知、连接与智能,实现物物协同。融合AI、5G与数字孪生,迈向高效、智能的新纪元。