JS 实现带手柄自由调整页面大小的功能

news/2024/7/7 19:15:10

        当你想实现点击并拖动某个dic元素来调整其大小的时候,我们可以通过如下代码(可直接复制粘贴运行)实现效果:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Resizable Element</title>
  <style>
    .resizable {
      width: 200px;
      height: 200px;
      background-color: lightgray;
      border: 1px solid #ccc;
      resize: both;
      overflow: auto;
    }
  </style>
</head>
<body>
  <div class="resizable">
    <!-- 这里放置需要调整大小的内容 -->
  </div>

  <script>
    document.addEventListener('mousedown', function(event) {
      if (event.target.classList.contains('resizable')) {
        event.preventDefault();
        event.target.classList.add('resizing');
        const initialX = event.clientX;
        const initialY = event.clientY;

        const mouseMoveListener = function(e) {
          const width = event.target.offsetWidth + (e.clientX - initialX);
          const height = event.target.offsetHeight + (e.clientY - initialY);
          event.target.style.width = width + 'px';
          event.target.style.height = height + 'px';
        };

        const mouseUpListener = function() {
          event.target.classList.remove('resizing');
          event.target.style.userSelect = 'auto';
          event.target.style.cursor = 'auto';
          document.body.style.cursor = 'auto';
          document.removeEventListener('mousemove', mouseMoveListener);
          document.removeEventListener('mouseup', mouseUpListener);
        };

        event.target.style.userSelect = 'none';
        event.target.style.cursor = 'nwse-resize';
        document.body.style.cursor = 'nwse-resize';
        document.addEventListener('mousemove', mouseMoveListener);
        document.addEventListener('mouseup', mouseUpListener);
      }
    });
  </script>
</body>
</html>

注意!!!:

        但在实际开发运用中,上面的代码是远远不够的,运行过上面代码的伙伴肯定能感觉到效果是不理想的,只能实现基本的调整页面大小的需求,而在调整页面大小的过程中是不灵活的,所以我们可以通过以下方式得到改善——添加手柄:在下面的优化代码中,我们使用JavaScript 实现了调整大小的功能。当用户点击并拖动位于 .resize-handle 类名的元素时,会触发相应的事件处理函数,从而实现调整大小的效果。这样就能够更灵活地控制调整大小的行为和效果。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Resizable Element</title>
  <style>
    .resizable {
      width: 200px;
      height: 200px;
      background-color: lightgray;
      border: 1px solid #ccc;
      overflow: auto;
      position: relative;
    }
    .resize-handle {
      width: 10px;
      height: 10px;
      background-color: #000;
      position: absolute;
      bottom: 0;
      right: 0;
      cursor: nwse-resize;
    }
  </style>
</head>
<body>
  <div class="resizable">
    <!-- 这里放置需要调整大小的内容 -->
    <div class="resize-handle"></div>
  </div>

  <script>
    const resizableElement = document.querySelector('.resizable');
    const resizeHandle = resizableElement.querySelector('.resize-handle');

    let isResizing = false;
    let initialX;
    let initialY;
    let originalWidth;
    let originalHeight;

    resizeHandle.addEventListener('mousedown', function(event) {
      event.preventDefault();
      isResizing = true;
      initialX = event.clientX;
      initialY = event.clientY;
      originalWidth = parseFloat(getComputedStyle(resizableElement, null).getPropertyValue('width'));
      originalHeight = parseFloat(getComputedStyle(resizableElement, null).getPropertyValue('height'));

      document.addEventListener('mousemove', resize);
      document.addEventListener('mouseup', stopResize);
    });

    function resize(event) {
      if (isResizing) {
        const width = originalWidth + (event.clientX - initialX);
        const height = originalHeight + (event.clientY - initialY);
        resizableElement.style.width = width + 'px';
        resizableElement.style.height = height + 'px';
      }
    }

    function stopResize() {
      isResizing = false;
      document.removeEventListener('mousemove', resize);
      document.removeEventListener('mouseup', stopResize);
    }
  </script>
</body>
</html>

        最后一句:代码都是可直接运行的,所以大家可以先复制代码到自己电脑的编辑器上面运行看效果,是否能达到自己的预期需求哦!


http://lihuaxi.xjx100.cn/news/1905570.html

相关文章

基于Java版本与鸿鹄企业电子招投标系统的二次开发实践-鸿鹄企业电子招投标系统源代码+支持二开+鸿鹄电子招投标系统

随着市场竞争的加剧和企业规模的扩大&#xff0c;招采管理逐渐成为企业核心竞争力的重要组成部分。为了提高招采工作的效率和质量&#xff0c;我们提出了一种基于电子化平台的解决方案。该方案旨在通过电子化招投标&#xff0c;使得招标采购的质量更高、速度更快&#xff0c;同…

SLAM算法与工程实践——RTKLIB编译

SLAM算法与工程实践系列文章 下面是SLAM算法与工程实践系列文章的总链接&#xff0c;本人发表这个系列的文章链接均收录于此 SLAM算法与工程实践系列文章链接 下面是专栏地址&#xff1a; SLAM算法与工程实践系列专栏 文章目录 SLAM算法与工程实践系列文章SLAM算法与工程实践…

状态管理@State

目录 一、简单类型的更新 二、class对象类型的变量 被该装饰器修饰的变量&#xff0c;在数据变化时会触发UI的刷新&#xff0c;也就是ArkTS UI中触发build()函数的调用&#xff0c;重新根据状态构建UI。如下更新是可以观察到的&#xff1a; 1、string number boolean 类型的数…

视觉增强RTK论文(1)—— GNSS-Stereo-Inertial SLAM for Arable Farming

文章目录 摘要方法标记ORB-SLAM3GNSS-Stereo-Inertial融合实验结果代码摘要 农业任务自动化速度的加快要求现场机器人采用高精度和鲁棒的定位系统。同时定位和映射(SLAM)方法不可避免地会在探索性轨迹上积累漂移,并且主要依赖于位置重新访问和循环闭合来保持一个有界的全局…

Flink Table API 与 SQL 编程整理

Flink API总共分为4层这里主要整理Table API的使用 Table API是流处理和批处理通用的关系型API&#xff0c;Table API可以基于流输入或者批输入来运行而不需要进行任何修改。Table API是SQL语言的超集并专门为Apache Flink设计的&#xff0c;Table API是Scala和Java语言集成式…

文件操作(open与fopen,write与fwrite,read与fread)

open与fopen fopen函数是标准I/O库函数的一部分&#xff0c;它提供了更高级别的文件操作功能&#xff0c;例如缓冲、格式化输入输出等。而open函数直接与底层文件系统交互&#xff0c;提供了更底层的文件操作。linux下的fopen是open的封装函数&#xff0c;fopen最终还是要调用…

Docker部署xxl-job以及接入方法

1.Docker一键部署xxl-job 1.原理与介绍&#xff1a;官方文档 2.部署 首先你需要有一个mysql的数据库&#xff0c;关于数据库的创建这里不做介绍&#xff0c;本地通过任何方式创建均可。 创建完成后需要创建对应的库和表&#xff0c;这里的库名定义为xxl_job&#xff08;可以…

YOLOv8改进 | 2023注意力篇 | HAttention(HAT)超分辨率重建助力小目标检测 (全网首发)

一、本文介绍 本文给大家带来的改进机制是HAttention注意力机制&#xff0c;混合注意力变换器&#xff08;HAT&#xff09;的设计理念是通过融合通道注意力和自注意力机制来提升单图像超分辨率重建的性能。通道注意力关注于识别哪些通道更重要&#xff0c;而自注意力则关注于图…