当前位置:网站首页>基于 outline 实现头像剪裁以及预览
基于 outline 实现头像剪裁以及预览
2022-08-02 19:36:00 【huaqi_】
outline 是真正意义上的不占据任何空间的属性。
outline 不会影响任何元素的任何布局,并且 outline 轮廓可穿透。
核心布局代码:
.crop {
overflow: hidden;
}
.crop > .crop-area {
width: 80px;
height: 80px;
outline: 256px solid rgba(0, 0, 0, .5);
cursor: move;
}
效果预览

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>outline 与镂空效果</title>
<style> .crop-box, .preview-box {
display: inline-block; vertical-align: top; } .crop, .preview {
position: relative; overflow: hidden; } .crop-area, .preview {
width: 80px; height: 80px; } .crop-area {
position: absolute; left: 80px; top: 56px; outline: 256px solid rgba(0, 0, 0, 0.5); cursor: move; } .crop img, .preview img {
display: block; width: 256px; height: 192px; } .preview img {
position: absolute; /* 初始位置与 crop-area 位置对应 */ left: -88px; top: -56px; } </style>
</head>
<body>
<div class="crop-box">
<h4>剪裁(仅演示移动)</h4>
<div class="crop">
<div id="cropArea" class="crop-area"></div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTS6ESEyK8IGsYRNb31pBZQFhi21oVIn8k5pQ&usqp=CAU" alt="剪裁预备图片" />
</div>
</div>
<div class="preview-box">
<h4>预览</h4>
<div class="preview">
<img id="previewImg" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTS6ESEyK8IGsYRNb31pBZQFhi21oVIn8k5pQ&usqp=CAU" alt="剪裁图片" />
</div>
</div>
<script> const elCropArea = document.getElementById("cropArea"), elPreviewImg = document.getElementById("previewImg"); let data = {
}; // 记录剪裁框初始位置 elCropArea.addEventListener("mousedown", function (event) {
data = {
moving: true, left: elCropArea.offsetLeft, top: elCropArea.offsetTop, x: event.pageX, y: event.pageY, }; }); // 监听具体鼠标移动事件 document.addEventListener("mousemove", function (event) {
if (data.moving) {
event.preventDefault(); // 移动距离 const moveX = event.pageX - data.x, moveY = event.pageY - data.y; // 目标坐标 let left = data.left + moveX, top = data.top + moveY; // 边界判断 if (left < 0) {
left = 0; } else if (left + 80 > 256) {
left = 176; } if (top < 0) {
top = 0; } else if (top + 80 > 192) {
top = 112; } // 剪裁框和预览框重定位 /* elCropArea.style.left = left + "px"; elCropArea.style.top = top + "px"; */ elCropArea.style.cssText = ` left: ${
left}px; top: ${
top}px; `; // elPreviewImg.style.left = -left + "px"; // elPreviewImg.style.top = -top + "px"; elPreviewImg.style.cssText = ` left: ${
-left}px; top: ${
-top}px; `; } }); document.addEventListener("mouseup", function () {
data.moving = false; }); </script>
</body>
</html>
边栏推荐
- golang刷leetcode 经典(12) 完全二叉树插入器
- Three.js入门
- 使用位运算实现加减乘除(+、-、*、/)及比较器的用法
- 你想要的宏基因组-微生物组知识全在这(2022.8)
- 磁盘分区的知识
- Compose主题切换——让你的APP也能一键换肤
- 软件测试分类
- NC | Structure and function of soil microbiome reveal N2O release from global wetlands
- SQL server有什么认证吗?
- Translate My Wonderful | July Moli Translation Program Winners Announced
猜你喜欢

MySQL安装(详细,适合小白)

光源控制器接口定义说明

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

AI Scientist: Automatically discover hidden state variables of physical systems

Redis cluster configuration

Flutter with internationalized adapter automatically generated

You want the metagenomics - microbiome knowledge in all the (2022.8)

J9数字货币论:识别Web3新的稀缺性:开源开发者

TPAMI2022 | TransCL:基于Transformer的压缩学习,更灵活更强大

Lvm逻辑卷
随机推荐
动态规划常见实例详解
Metaverse 001 | Can't control your emotions?The Metaverse is here to help you
Caldera(二)高级实战
Lvm逻辑卷
2022-07-28
Redis集群配置
如何ES源码中添加一个自己的API 流程梳理
MySQL安装配置教程(超级详细、保姆级)
spack install报错/tmp/ccBDQNaB.s: Assembler message:
es 读流程源码解析
es DELETE index 源码分析
geoserver+mysql+openlayers问题点
VMware虚拟机无法上网
解析Collection接口中的常用的被实现子类重写的方法
溜不溜是个问题
Silver circ: letter with material life insurance products should be by the insurance company is responsible for the management
J9数字货币论:识别Web3新的稀缺性:开源开发者
汇编实例解析--利用tcb,tss,全局tss,ldt管理任务实现任务切换
E. Add Modulo 10(规律)
「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解