Javascript“在徘徊时”循环 [英] Javascript "while hovered" loop
问题描述
我很感激任何解决方案,无论是在jQuery或纯JavaScript中 - 这里是(在jquery中):
var scrollingposition = 0;
$ b $('#button')。hover(function(){
++ scrollingposition;
$('#object')。css(right,scrollingposition) ;
});
现在我怎么能把这个放到某种while循环中,这样#object正在移动px px作为#button被徘徊,而不仅仅是当鼠标进入它时?
好的...另一个刺答案:
<$ p ()函数(){
var hovered = false;
var loop = window.setInterval(function(){
($ {code> $('myselector' if(hovered){
// ...
}
},250);
$(this).hover(
function() {
hovered = true;
},
function(){
hovered = false;
}
);
});
250
表示任务每季重复一秒钟。您可以减少此数字以使其更快,或增加数字以使其变慢。
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.
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);
});
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;
}
);
});
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“在徘徊时”循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!