Javascript“在徘徊时”循环 [英] Javascript "while hovered" loop

查看:82
本文介绍了Javascript“在徘徊时”循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我解决这个问题...我有一个按钮,当它被徘徊时触发一个动作。但是,只要按钮被徘徊,我希望它重复它。



我很感激任何解决方案,无论是在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆