- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用svg创建一个几何campass工具,其中有3个btns,1) 用于移动位于中心的整个营地2) 增加圆的半径3)用于旋转圆的半径4)根据第4个btn的位置使用路径(svg)绘制圆,该位置始终位于圆周上。
所以我有一个函数,
updateCircle(cx,cy,r, Angular )
在此函数中,它查找所有 4btns 的位置和圆的半径(即 svg 中的线)以及大圆的半径和圆心
对于第一个 btn,我使用当前光标在第一个 btn 上的 mousedown、mousemove 和 mouseup 移动点来更新圆圈位置。
默认 Angular 值为 90 度。
因此,对于第二个btn,我必须根据鼠标向下移动的情况增加半径,并在鼠标向上移动的情况下减小半径,当半径位于第三和第四象限时,当半径处于第三和第四象限时,则相反位于第一和第二象限。
所以问题来了:-
如何检测鼠标向上和向下移动?
或者有什么方法可以通过在第二个 btn 上执行任何 mousedown 和 mouse up 事件来执行增加和减少半径的操作吗?
第二个问题是:-
我如何创建第三个 btn 的功能(因为我确信我必须用 Angular 做一些事情,但有点困惑,比如当半径位于第三、第四象限并且鼠标向左移动时当鼠标向右移动时, Angular 增大;当半径位于第一和第二象限时, Angular 减小;反之亦然)
任何建议都应该受到赞赏和有值(value)。
最佳答案
检测鼠标移动方向
通过保留对前一个鼠标位置的引用,将其与新的位置进行比较,然后使用当前的鼠标位置更新下一次迭代的前一个鼠标位置,您可以轻松确定鼠标是向上还是向下移动。下面是如何在 JavaScript 中实现这一点的示例:
const lastPoint = {x: null, y: null}
window.addEventListener('mousemove', e => {
const leftOrRight = (
e.clientX > lastPoint.x ? 'right'
: e.clientX < lastPoint.x ? 'left'
: 'none'
)
const upOrDown = (
e.clientY > lastPoint.y ? 'down'
: e.clientY < lastPoint.y ? 'up'
: 'none'
)
/*
here you can apply the transformations you need to,
then update the cursor position tracker for the
next iteration
*/
lastPoint.x = e.clientX
lastPoint.y = e.clientY
})
旋转
假设您正在使用 CSS 转换,您可以跟踪 javascript 中的旋转变量并将其动态应用到 DOM 元素的内联 CSS。使用与上述相同的方法,您可以在鼠标向上移动时向一种方向旋转,而在向下移动时向另一种方向旋转。以下是如何实现这一点。
圆.html
<div class="circle" style="transform: rotate(0deg);"><div class="inner"></div></div>
圆.css
.circle {
width: 100px;
height: 100px;
background-color: green;
border-radius: 50%;
position: relative;
}
.circle .inner {
width: 10px;
height: 10px;
background-color: orange;
border-radius: 50%;
margin: auto;
}
旋转.js
const rotatingCircle = document.getElementsByClassName('circle')[0]
const lastPoint = {x: null, y: null}
let rotation = 0
window.addEventListener('mousemove', e => {
rotation += (
e.clientY > lastPoint.y ? -1
: e.clientY < lastPoint.y ? 1
: 0
)
rotatingCircle.style = `transform: rotate(${rotation}deg);`
lastPoint.x = e.clientX
lastPoint.y = e.clientY
})
在此处查看实时旋转示例:https://codepen.io/OneCent/pen/jOEMpEr
如果这有帮助或者您有任何疑问,请告诉我!
关于javascript - js/jq中检测鼠标向上、向左、向右、向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59341675/
我正在使用面部跟踪进行 HCI(人机交互)。我正在尝试使用面部控制 PC。 我有 3x3 二维网格按钮。1 2 34 5 67 8 9 假设,当前焦点在按钮 5 上。如果我按向上箭头,则焦点将在 2
我正在为蛇和梯子制作一 block 板,到目前为止,我已经按降序打印了板。但是,我需要以正确的方式打印电路板。 编辑“螺旋下降”意味着 100...91 81...90 80...71 ...
我有一个可以响应式调整大小的菜单,因此每次调整大小时 div (#menuWFhover) 的内容都会重新排列,因此 div 根据窗口大小具有不同的高度。 当我使用 jQuery slideDown/
我们从服务器获取数据并附加 ListView 。我们的问题是,当向上/向下滚动时,它首先显示白屏,然后显示数据。向下/向上滚动时,之前出现的白屏应删除 for (var i=0; i");
我想要一个汉堡图标动态改变颜色的代码,以适应网站的黑色部分/部分和白色。它最初是 3 段白色,js 代码几乎是好的和正确的,但是当它在白色部分时它一直闪烁,并在红色和白色之间闪烁。 js var to
我对点击事件的页脚位置有疑问。我正在使用 bootstrap css,我有一个可折叠元素(bootstrap 中的 Accordion ),当它折叠时它有一个名为 .accordion-toggle.
在此代码段中,使用关键帧和动画并显示无/ block ,div 动画以在悬停时向下滑动。 h1 { padding: 20px; } div { width: 100%; background
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我正在尝试就地缩小文件。 我正在用另一个文件的内容替换一个文件的内容,完成后我想确保源文件是否小于目标文件,目标文件是否正确收缩。 (为什么:因为dest文件是一个备份,写入media的开销很大,所以
似乎每当我用一个负整数除以一个正整数时,我都需要它向下舍入 (向 -inf),而不是向 0。但 C# 和 C++ 都向 0 舍入。 所以我想我需要一个 DivideDownward() 方法。我可以用
考虑这个简单的代码: document.addEventListener( 'keypress', function() { console.log( 'press' ); } ); document
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
我见过几个recyclerview的无限滚动示例,但它们都是向下滚动的。 我想要的是一次又一次地加载我的列表,但无论用户是从上面还是从下面滚动。 正常列表 向上滚动 向下滚动 这样可以通过无限滚动一遍
我正在使用带有 View 的 drupal 7,并且我正在使用我发现的向上/向下文本幻灯片的 jquery 脚本。它有效,但是当我尝试将它与带有 ajax 的 View 公开过滤器一起使用时,它似乎不
*场景 我希望制作一个类似于本网站上的 jquery 效果,它位于主 Flash 添加的右侧: http://www.commbank.com.au/ *问题 我已经开始了,但是用很多方法遇到了一些障
互联网。如果这与其他人没有什么关系,请原谅我,但我会将其留在这里,以防这是一个有效的问题。 我正在尝试创建一个文本区域字段,其中用户每次按下键(a-z),都会触发背景颜色更改(在数组中列出)。我一直在
我正在创建一个网络前端来控制一个小型机器人。 Ajax 调用将在 keydown 上进行,以启动机器人,并在 keyup 上进行停止。 我的问题是,当按下某个键时,keyup、keydown 和 ke
我在内容容器中有两个 div,一个向左浮动,另一个向右浮动。我正在使用屏幕的整个宽度。左 div 宽度为 1290px,右 div 宽度为 625px。有时,在加载页面时,滚动条会更改可用屏幕宽度的宽
请看这个UI sketch图片,我在某个站点的侧边栏(黑框)中有这个 div,当我向下滚动或向上滚动时,我不希望它隐藏...我希望它在我向下滚动和移动时自行向下移动当我向上滚动时向上滚动,这样它就永远
我是一名优秀的程序员,十分优秀!