当前位置:网站首页>基于 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>
边栏推荐
猜你喜欢

「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解

Leetcode刷题——单调栈问题(739每日温度问题、496下一个更大元素I、503下一个更大元素 II)

A Review of Nature Microbiology: Focusing on the Algae--Ecological Interface of Phytoplankton-Bacteria Interactions

扫码预约 | 观看Apache Linkis数据处理实践以及计算治理能力

4 kmiles join YiSheng group, with more strong ability of digital business, accelerate China's cross-border electricity full domain full growth

什么是现场服务管理系统(FSM)?有什么好处?

动态规划常见实例详解

AI科学家:自动发现物理系统的隐藏状态变量

基于OpenGL的冰川与火鸟(光照计算模型、视景体、粒子系统)

Kali命令ifconfig报错command not found
随机推荐
当TIME_WAIT状态的TCP正常挥手,收到SYN后…
What is a Field Service Management System (FSM)?what is the benefit?
MaxCompute 近期发布上线的版本的 SQL 引擎新功能参数化视图有什么优势?
Metaverse 001 | Can't control your emotions?The Metaverse is here to help you
【心理学 · 人物】第一期
如何ES源码中添加一个自己的API 流程梳理
Leetcode刷题——23. 合并K个升序链表
技术分享 | Apache Linkis 快速集成网页IDE工具 Scriptis
MOSN 反向通道详解
ShapeableImageView 的使用,告别shape、三方库
分享一个 web 应用版本监测 (更新) 的工具库
Silver circ: letter with material life insurance products should be by the insurance company is responsible for the management
竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)
spack install报错/tmp/ccBDQNaB.s: Assembler message:
斯堪尼亚SCANIA OTL标签介绍
[AnXun cup 2019] easy_web
MySQL安装(详细,适合小白)
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
一款好用的FAQ搭建工具
PG 之 SQL执行计划