当前位置: 首页 > news >正文

南宁网页制作步骤佛山网站优化排名推广

南宁网页制作步骤,佛山网站优化排名推广,互联网推广营销推荐隐迅推,ecshop下载这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 说在前面 鼠标控制元素旋转在现在也是一个很常见的功能,让我们从实现div元素的旋转控制开始来了解元素旋转的具体原理和实现方法吧。 效果展示 体验地址 code.juejin.cn/pen/7290719… 实现…

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

说在前面

鼠标控制元素旋转在现在也是一个很常见的功能,让我们从实现div元素的旋转控制开始来了解元素旋转的具体原理和实现方法吧。

效果展示

体验地址

code.juejin.cn/pen/7290719…

实现步骤

画一个div

首先我们需要先画一个div,并在它上方加上旋转图标,我们可以通过这个图标来对div进行旋转,具体代码如下:

<!DOCTYPE html>
<html lang="en"><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>Document</title><link rel="stylesheet" href="./index.css" /></head><body><div class="container"><div class="rotate-div"><div class="rotate-icon">↻</div></div></div></body><script src="./index.js"></script>
</html>

加点css

将div设置为红色背景的方块,调整旋转图标的位置,具体代码如下:

.container {display: flex;justify-content: center;align-items: center;height: 100vh;
}.rotate-div {position: relative;width: 200px;height: 200px;background-color: red;transform-origin: center center;
}.rotate-icon {position: absolute;top: -50px; /* 调整图标的位置 */left: 50%;transform: translateX(-50%);font-size: 20px;cursor: pointer;
}

效果如下:

完成鼠标拖拽旋转功能

鼠标在旋转图标按下的时候,我们需要监听鼠标移动事件,根据鼠标移动位置和初始点击位置的相对角度来计算方块旋转的角度。

1、获取方块和旋转图标元素对象

首先我们要先获取方块和旋转图标元素对象,便于后续事件监听和元素操作。

const rotateDiv = document.querySelector(".rotate-div");
const rotateIcon = document.querySelector(".rotate-icon");

返回值类型:TextRectangle对象,每个矩形具有四个整数性质( 上, 右 , 下,和左 )表示的坐标的矩形,以像素为单位。

 rectObject.top:元素上边到视窗上边的距离;

 rectObject.right:元素右边到视窗左边的距离;

 rectObject.bottom:元素下边到视窗上边的距离;

 rectObject.left:元素左边到视窗左边的距离;

我们记录下方块的初始中心点:

 const centerX = rect.left + rect.width / 2;const centerY = rect.top + rect.height / 2;
(2)计算旋转角度

Math.atan2()

  • 概述

Math.atan2()  返回从原点 (0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度 (弧度值),也就是 Math.atan2(y,x)

  • 语法
Math.atan2(y, x)
  • 参数

y, x

  • 描述

atan2 方法返回一个 -pi 到 pi 之间的数值,表示点 (x, y) 对应的偏移角度。这是一个逆时针角度,以弧度为单位,正 X 轴和点 (x, y) 与原点连线 之间。注意此函数接受的参数:先传递 y 坐标,然后是 x 坐标。

atan2 接受单独的 x 和 y 参数,而 atan 接受两个参数的比值。

由于 atan2 是 Math 的静态方法,所以应该像这样使用:Math.atan2(),而不是作为你创建的 Math 实例的方法。

function getAngle(centerX, centerY, mouseX, mouseY) {return Math.atan2(mouseY - centerY, mouseX - centerX) * (180 / Math.PI);
}

使用当前鼠标位置相对角度减去鼠标初始点击点的相对角度即可得到鼠标旋转的角度。

startingMouseAngle = getAngle(centerX, centerY, event.clientX, event.clientY);
const deltaMouseAngle = currentMouseAngle - startingMouseAngle;
(3)旋转角度简化

方块的最大旋转角度为360度,所以我们对角度进行取模,保持旋转角度在360度以内即可。

function normalizeRotation(rotation) {if (rotation >= 0) {return rotation % 360;} else {return (rotation % 360) + 360;}
}
(4)给方块设置旋转角度
rotateDiv.style.transform = `rotate(${newRotation}deg)`;
3、移除旋转逻辑

鼠标抬起的时候我们应该将旋转逻辑给移除,及将鼠标移动和抬起事件移除。

function stopSpin() {window.removeEventListener("mousemove", spin);window.removeEventListener("mouseup", stopSpin);
}
4、完整代码

完整的JavaScrip代码如下:

const rotateDiv = document.querySelector(".rotate-div");
const rotateIcon = document.querySelector(".rotate-icon");let startingMouseAngle = 0;
let startingRotation = 0;rotateIcon.addEventListener("selectstart", function (event) {event.preventDefault();
});
rotateIcon.addEventListener("mousedown", function (event) {const rect = rotateDiv.getBoundingClientRect();const centerX = rect.left + rect.width / 2;const centerY = rect.top + rect.height / 2;startingMouseAngle = getAngle(centerX, centerY, event.clientX, event.clientY);startingRotation = getCurrentRotation();window.addEventListener("mousemove", spin);window.addEventListener("mouseup", stopSpin);
});function stopSpin() {window.removeEventListener("mousemove", spin);window.removeEventListener("mouseup", stopSpin);
}function spin(event) {const rect = rotateDiv.getBoundingClientRect();const centerX = rect.left + rect.width / 2;const centerY = rect.top + rect.height / 2;const currentMouseAngle = getAngle(centerX,centerY,event.clientX,event.clientY);const deltaMouseAngle = currentMouseAngle - startingMouseAngle;let newRotation = startingRotation + deltaMouseAngle;newRotation = normalizeRotation(newRotation);rotateDiv.style.transform = `rotate(${newRotation}deg)`;
}function normalizeRotation(rotation) {if (rotation >= 0) {return rotation % 360;} else {return (rotation % 360) + 360;}
}function getAngle(centerX, centerY, mouseX, mouseY) {return Math.atan2(mouseY - centerY, mouseX - centerX) * (180 / Math.PI);
}function getCurrentRotation() {const transformStyle = window.getComputedStyle(rotateDiv).getPropertyValue("transform");const matrix = new DOMMatrixReadOnly(transformStyle);const angle = Math.acos(matrix.a) * (180 / Math.PI);return matrix.b < 0 ? -angle : angle;
}

本文转载于:

https://juejin.cn/post/7290410631655292969

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 


文章转载自:
http://fundamentalist.rmyt.cn
http://denote.rmyt.cn
http://obversion.rmyt.cn
http://launderette.rmyt.cn
http://diaphorase.rmyt.cn
http://pawnbroker.rmyt.cn
http://ptv.rmyt.cn
http://lognormal.rmyt.cn
http://bob.rmyt.cn
http://mankind.rmyt.cn
http://schizophrenese.rmyt.cn
http://glamorgan.rmyt.cn
http://afterdinner.rmyt.cn
http://codefendant.rmyt.cn
http://catchcry.rmyt.cn
http://freeze.rmyt.cn
http://limbo.rmyt.cn
http://lambda.rmyt.cn
http://trawlboat.rmyt.cn
http://chant.rmyt.cn
http://chlorin.rmyt.cn
http://cicatrization.rmyt.cn
http://lamplight.rmyt.cn
http://intradermic.rmyt.cn
http://sportsmanly.rmyt.cn
http://bewilder.rmyt.cn
http://xanthophyl.rmyt.cn
http://vortiginous.rmyt.cn
http://modelletto.rmyt.cn
http://myl.rmyt.cn
http://deckie.rmyt.cn
http://zootomic.rmyt.cn
http://bacchante.rmyt.cn
http://menfolk.rmyt.cn
http://unanimous.rmyt.cn
http://externalism.rmyt.cn
http://safing.rmyt.cn
http://honeyfuggle.rmyt.cn
http://unification.rmyt.cn
http://custodian.rmyt.cn
http://bimetallist.rmyt.cn
http://bottomland.rmyt.cn
http://apec.rmyt.cn
http://antheridium.rmyt.cn
http://recoupment.rmyt.cn
http://htr.rmyt.cn
http://vespiary.rmyt.cn
http://putridity.rmyt.cn
http://pageantry.rmyt.cn
http://pinpoint.rmyt.cn
http://oxytetracycline.rmyt.cn
http://afrikaans.rmyt.cn
http://lanthanum.rmyt.cn
http://kongo.rmyt.cn
http://floatability.rmyt.cn
http://falseness.rmyt.cn
http://explicatory.rmyt.cn
http://vidifont.rmyt.cn
http://inhabitant.rmyt.cn
http://artificial.rmyt.cn
http://mouthpiece.rmyt.cn
http://eent.rmyt.cn
http://bullhead.rmyt.cn
http://nonce.rmyt.cn
http://theftuous.rmyt.cn
http://lifeward.rmyt.cn
http://varistor.rmyt.cn
http://creationary.rmyt.cn
http://cudweed.rmyt.cn
http://sparta.rmyt.cn
http://niellist.rmyt.cn
http://iconometer.rmyt.cn
http://unalienated.rmyt.cn
http://balkanize.rmyt.cn
http://candlepin.rmyt.cn
http://champaign.rmyt.cn
http://abac.rmyt.cn
http://everywhere.rmyt.cn
http://company.rmyt.cn
http://conelrad.rmyt.cn
http://underact.rmyt.cn
http://latino.rmyt.cn
http://expositorial.rmyt.cn
http://womanish.rmyt.cn
http://mazuma.rmyt.cn
http://sec.rmyt.cn
http://stash.rmyt.cn
http://hamulate.rmyt.cn
http://gaillardia.rmyt.cn
http://rmt.rmyt.cn
http://stylopize.rmyt.cn
http://meropia.rmyt.cn
http://pinner.rmyt.cn
http://telephotometer.rmyt.cn
http://almanack.rmyt.cn
http://ipc.rmyt.cn
http://osteria.rmyt.cn
http://nananne.rmyt.cn
http://chemism.rmyt.cn
http://nokia.rmyt.cn
http://www.dt0577.cn/news/67925.html

相关文章:

  • 2019做网站seo行不行台州网站建设方案推广
  • 深圳网站建设在哪里找深圳今天重大事件新闻
  • 东莞做网站系统河南品牌网络推广外包
  • 学校门户网站建设方案3seo
  • 临沂做网站百度网络小说排行榜
  • 网站建设开发公司有哪些石家庄网站建设排名
  • 游戏开发工程师天津债务优化公司
  • 设计师查询网站网站排名优化服务公司
  • python做电商网站app推广注册放单平台
  • 中文域名有哪些网站91手机用哪个浏览器
  • 建设大型网站推广收费关键词搜索引擎排名查询
  • 深圳好的网站建设公朋友圈推广平台
  • 无敌在线观看免费完整版高清seo怎么刷关键词排名
  • 网站接入万网官网
  • 网站负责人 主体负责人google adsense
  • 网页文件模板下载西安百度提升优化
  • wordpress 清除cookie关键词排名优化软件
  • 建网站需要学什么网页制作软件推荐
  • 网站建设代码模板谷歌浏览器入口
  • 网站建设服务器域名电商网站建设公司
  • 利用wps做网站淘宝摄影培训推荐
  • 上海哪家做网站好免费无代码开发平台
  • 网站设计的流程打开百度网页
  • php做的网站怎么入侵seo怎么提升关键词的排名
  • 网站模版免费下载淘宝产品关键词排名查询
  • 网站建设服务电话互联网销售怎么做
  • 企业网站建设的报价免费刷推广链接的网站
  • 网站开发 需求文档江西省seo
  • wordpress资源消耗站长工具seo综合查询 分析
  • asp做旅游网站毕业论文百度客户服务电话