Javascript quot;while hoveredquot; loop(Javascript“悬停时环形)
问题描述
谁能帮我解决这个问题...我有一个按钮,当它悬停时会触发一个动作.但只要按钮悬停,我希望它一直重复.
Can anybody help me on this one...I have a button which when is hovered, triggers an action. But I'd like it to repeat it for as long as the button is hovered.
我很感激任何解决方案,无论是在 jquery 还是纯 javascript 中 - 这是我的代码此时的样子(在 jquery 中):
I'd appreciate any solution, be it in jquery or pure javascript - here is how my code looks at this moment (in jquery):
var scrollingposition = 0;
$('#button').hover(function(){
++scrollingposition;
$('#object').css("right", scrollingposition);
});
现在我怎样才能把它放到某种while循环中,以便#object在#button悬停时逐像素移动,而不仅仅是当鼠标进入它时?
Now how can i put this into some kind of while loop, so that #object is moving px by px for as #button is hovered, not just when the mouse enters it?
推荐答案
好的……又是一个答案:
OK... another stab at the answer:
$('myselector').each(function () {
var hovered = false;
var loop = window.setInterval(function () {
if (hovered) {
// ...
}
}, 250);
$(this).hover(
function () {
hovered = true;
},
function () {
hovered = false;
}
);
});
250
表示任务每四分之一秒重复一次.您可以减少此数字以使其更快,或增加它以使其更慢.
The 250
means the task repeats every quarter of a second. You can decrease this number to make it faster or increase it to make it slower.
这篇关于Javascript“悬停时"环形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript“悬停时"环形
基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01