- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我在我的网站上做了这个小技巧,当你在屏幕上移动光标时,它会改变网站 Logo 的旋转速度。
出于某种原因,我似乎无法使用 js 更改即时旋转元素的动画属性。我还添加了一个文本框来显示我用于 Logo 旋转速度的鼠标 X 位置,并且它正确显示了字符串,所以我猜它可能是一个愚蠢的语法错误。
<style type="text/css">
#logorota {
animation: 15s linear 0s normal none infinite rot_inf;
}
@keyframes rot_inf {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(359deg);
}
}
</style>
<body>
<script>
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = mousePos, logoSpeed;
function mousePos (x) {
var mouseX = x.pageX / 100;
document.mouseMovs.mousePos.value = mouseX + "s linear 0s normal none infinite rot_inf"; //displays the string correctly.
};
function logoSpeed (x)
{
var mouseX = x.pageX / 100;
document.getElementById('logorota').style.animation = mouseX + "s linear 0s normal none infinite rot_inf";
};
</script>
<div>
<img src="logorota.png" id="logorota">
<form name="mouseMovs">
<input type="text" name="mousePos" /> Mouse position <br />
</form>
</div>
</body>
最佳答案
我这里有一个 webkit 的解决方案,可以正常工作。动态更改这些值有点棘手。
附加到“mousemove”,此脚本连续添加/删除动画规则和类并更改一些值以使其足够平滑地旋转。
如果你使用它,你会想改变很多东西。至少确保样式表索引是好的等等。在这种情况下,我使用 1,因为那是 jsFiddle 样式表索引。
编辑:现在也适用于 firefox
//
document.addEventListener('mousemove', function(event) {
Rotator.init(event, document.getElementsByClassName("img")[0]);
}, false);
//
var Rotator = {
init: function(event, element_) {
var element = element_,
compatibility = {
prefixes: ["-webkit-", "-moz-"],
},
browserPrefix,
browserEvent = event.clientX,
stylesheet = 0;
for (var k in compatibility) {
if (k === "prefixes") {
for (var i = 0; i < compatibility[k].length; i += 1) {
if (window.getComputedStyle(element, null).getPropertyValue(compatibility[k][i] + "transform") !== null) {
browserPrefix = compatibility.prefixes[i];
}
}
}
}
this.rotate(element, browserPrefix, browserEvent, stylesheet);
},
rotate: function(element_, browserPrefix_, browserEvent_, stylesheet_) {
var element = element_,
className = element.className,
browserPrefix = browserPrefix_,
browserEvent = browserEvent_,
stylesheet = stylesheet_,
blanks = [
"@" + browserPrefix + "keyframes ",
" {from{" + browserPrefix + "transform:rotate( ",
"deg);}to{" + browserPrefix + "transform:rotate(",
"deg);}}",
".",
"{" + browserPrefix + "animation-name:",
";" + browserPrefix + "animation-timing-function:linear;" + browserPrefix + "animation-iteration-count:infinite;" + browserPrefix + "animation-duration:",
"s}"],
name = "rotating" + browserEvent,
speed = String(browserEvent / 100),
transform = window.getComputedStyle(element, null).getPropertyValue(browserPrefix + "transform"),
values, a, b, angle, toAngle;
if (transform !== "none") {
values = transform.split('(')[1];
values = values.split(')')[0];
values = values.split(',');
a = values[0];
b = values[1];
angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
}
toAngle = 360 + angle;
document.styleSheets[stylesheet].insertRule(blanks[0] + name + blanks[1] + angle + blanks[2] +
(function () {
if (!toAngle) {
return 10;
} else {
return toAngle;
}
}()) + blanks[3], 0);
document.styleSheets[stylesheet].insertRule(blanks[4] + className + blanks[5] + name + blanks[6] + speed + blanks[7], 0);
if (document.styleSheets[stylesheet].cssRules.length > 8) {
for (var rules = document.styleSheets[stylesheet].cssRules.length; rules > 2; rules -= 1) {
document.styleSheets[stylesheet].deleteRule([rules] - 1);
}
}
}
}
关于javascript - 使用 javascript onmousemove 更改动画 css 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231660/
我确定我在这里遗漏了一些简单的东西,但我很难过。 为什么 addEventListener 和 removeEventListener 函数在这些 React 生命周期方法中不起作用,但它们下面的注释
我真的不知道如何在标题中表达这一点,但我正在尝试创建一个包含所有事件函数的类,但我遇到了奇怪的问题。 这是问题的一些模拟代码。 class Mouse{ constructor(){
我正在学习使用 onmousemove 事件,但无法使其工作,其想法是稍微倾斜图像以创建一种效果,就像它跟随鼠标一样,我猜是一种视差。这就是我想要实现的目标: https://tympanus.net
这个问题已经有答案了: How to get mouse position in jQuery without mouse-events? (7 个回答) 已关闭 3 年前。 在 Javascript
这个问题已经有答案了: What is the (function() { } )() construct in JavaScript? (28 个回答) 已关闭 3 年前。 我正在编写一些 Java
我有下一张 table : div class="container">
我正在尝试使用 javascript 创建一个自定义视频播放器,代码应该在 onmousemove 事件上显示叠加层它确实触发了代码但由于某种原因两次,我认为这是因为每个顶部都有双全屏 div其他,但
function aim() { var mousex = ? } 我找不到如何在 javascript 中获取 onmousemove 事件的鼠标坐标。 最佳答案 var guide =
我搜索了本博客和其他博客中提供的答案,但没有找到任何解决方案。非常感谢您的帮助。 我有一个 onmousemove 事件,它正在执行要触发的事件列表中的第一项,但不会执行第二项。 document.g
我正在我网页上的一个 div 中加载一个外部网页(即本例中的 www.duckduckgo.com)。我想在 div 内部和外部获取鼠标的 X 和 Y 位置,但是当我在 div 内部时,网页似乎阻止了
我正在做一个jsp页面,它记录了鼠标在该页面上的时刻。我有记录时刻的代码,但我不知道如何将总时刻的最终值传递给变量? onmousemove: Mouse over and leave me!
我已经对 javascript 中的事件进行了一些研究,但仍然无法弄清楚 onmousemove 中传递给事件的内容是什么。 摘自:W3schools onmousemove="show_coords
我想在单击鼠标并在元素上方更改背景颜色,并可以移动它,这样您就不必单击每个元素,而是能够用鼠标“绘制”。但我不知道怎么办。我现在的代码如下 function change_color(x, y) {
我的应用程序有 350 个编辑字段,所有这些字段都应有一个 OnMouseMove 事件。我已经为所有这些生成了以下代码: ... type ... procedure Edit1MouseMo
var zoomPad = document.getElementsByClassName('zoomPad'); zoomPad[0].onmouseover = function(){ v
这可能已得到解答,但无法在搜索中找到任何正面结果。 有一个脚本可以使用各种额外的计算来移动元素“正常” x,y 定位。 我没有使用 JQuery 或任何其他库,但这里有一个核心示例:>> fiddle
我这里有一个真正的问题。我希望仅当单击对象以及将鼠标移到对象上时才调用函数。这是一个示例(忽略奇怪的语法)以帮助您理解: 我正在想一个办法来解决这个问题。如果我创建一个 onMouseDown 来触
为了给我们的 Web 应用程序添加一些个性,我在某些元素上添加了一些动画,当鼠标靠近时,我们的机器人吉祥物会从后面窥视。为此,我使用 onmousemove 函数来检查光标的距离,并在鼠标靠近时为机器
大家好:)如果我在示例中使用鼠标指针传递 div,函数在控制台中打印两次,这意味着 onmousemove 事件被触发两次。我还打印了鼠标指针的坐标以及您如何在下图,我也没有垂直移动,只是水平移动。考
我正在实现“调整大小 handle ”来更改左侧导航面板的宽度。这是一个div收到onMouseDown()事件,计算必要的宽度并将它们应用于后续调用中的正确元素 onMouseMove() .但我遇
我是一名优秀的程序员,十分优秀!