使用 jQuery 在 JavaScript 中设置延迟悬停时循环代码

发布于 2024-09-27 10:41:53 字数 264 浏览 0 评论 0原文

我希望通过 jQuery 悬停事件来启动和停止具有设定延迟的循环。我一直试图用“鼠标悬停”和“鼠标移出”来做到这一点,但没有成功。

示例(奇怪的伪代码):

Mouseover
    Loop
        Change text colour
        Wait 100ms
Mouseout
    Stop loop

我确信这非常简单,我只是不太知道如何用 JavaScript 构建它。

提前致谢。

I'm looking to start and stop a loop with a set delay with a jQuery hover event. I've been trying to do it with "mouseover" and "mouseout" with no luck.

Example (odd psudocode):

Mouseover
    Loop
        Change text colour
        Wait 100ms
Mouseout
    Stop loop

I'm sure this is super easy, I just don't quite know how to structure it with JavaScript.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

许你一世情深 2024-10-04 10:41:53

这可能有效:

$(function(){
    $('#test').hover(function(){
      var self = $(this),
         rnd  = null,
         col  = null;

      this.iid = setInterval(function(){          
        col = ['#'];
        rnd = ~~(Math.random()*255);
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));     
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));

        self.css({backgroundColor: col.join('')});
      }, 100);
   }, function(){
       if(this.iid){
           clearInterval(this.iid);
           delete this.iid;
       }
  });
});​

请参阅实际操作: http://www.jsfiddle.net/YjC6y/19/< /a>

This might work:

$(function(){
    $('#test').hover(function(){
      var self = $(this),
         rnd  = null,
         col  = null;

      this.iid = setInterval(function(){          
        col = ['#'];
        rnd = ~~(Math.random()*255);
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));     
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));
          col.push(rnd.toString(16).length < 2 ? '0' + rnd.toString(16) : rnd.toString(16));

        self.css({backgroundColor: col.join('')});
      }, 100);
   }, function(){
       if(this.iid){
           clearInterval(this.iid);
           delete this.iid;
       }
  });
});​

See this in action: http://www.jsfiddle.net/YjC6y/19/

酸甜透明夹心 2024-10-04 10:41:53
function rgb() {
    var color = 'rgb(';
    for (var i = 0; i < 3; i++) {
        color += Math.floor(Math.random() * 255) + ',';
    }
    return color.replace(/\,$/, ')')
}

var loop = null;
$(function () {
    $('#someid').hover(function () {
        var $this = $(this);
        loop = setInterval(function () {
            $this.css({backgroundColor: rgb() });
        }, 100);
    }, function () {
        clearInterval(loop);
    });
});

尝试一个例子:http://jsbin.com/uraxe4

function rgb() {
    var color = 'rgb(';
    for (var i = 0; i < 3; i++) {
        color += Math.floor(Math.random() * 255) + ',';
    }
    return color.replace(/\,$/, ')')
}

var loop = null;
$(function () {
    $('#someid').hover(function () {
        var $this = $(this);
        loop = setInterval(function () {
            $this.css({backgroundColor: rgb() });
        }, 100);
    }, function () {
        clearInterval(loop);
    });
});

try an example : http://jsbin.com/uraxe4

沐歌 2024-10-04 10:41:53
$("#yourElem").hover(
  function () {  /* mousenter */
    $this = $(this);

    // take note that the mouse is currently hovering
    $this.data("isHovering", true);

    // create an interval and store its ID in jQuery data
    $this.data("loopId", setInterval(function () {
      // only do something if we are still hovering
      if ($this.data("isHovering")) {
        $this.css("color", getRandomColorValue());
      }
    }, 100);
  },
  function () {  /* mouseleave */
    $this = $(this);

    // take note that the mouse is no longer hovering
    $this.data("isHovering", false);

    // clear the interval that was set and delete the ID
    if ($this.data("loopId")) {
      clearInterval($this.data("loopId"));
      $this.data("loopId", false);
    }
  }
)
$("#yourElem").hover(
  function () {  /* mousenter */
    $this = $(this);

    // take note that the mouse is currently hovering
    $this.data("isHovering", true);

    // create an interval and store its ID in jQuery data
    $this.data("loopId", setInterval(function () {
      // only do something if we are still hovering
      if ($this.data("isHovering")) {
        $this.css("color", getRandomColorValue());
      }
    }, 100);
  },
  function () {  /* mouseleave */
    $this = $(this);

    // take note that the mouse is no longer hovering
    $this.data("isHovering", false);

    // clear the interval that was set and delete the ID
    if ($this.data("loopId")) {
      clearInterval($this.data("loopId"));
      $this.data("loopId", false);
    }
  }
)
横笛休吹塞上声 2024-10-04 10:41:53
changeColorTimerId = -1;


$('.box').hover(function(){
  //mouseOver code here
   changeColorTimerId = setInterval ( changeColor, 1000 );  
},function(){
  //mouseOut  code here  
  if ( changeColorTimerId ){
      clearInterval ( changeColorTimerId  )
  }

});

function changeColor(){
  $(".box").css ( 'backgroundColor',  '' + getRandomColor() );
}

function getRandomColor(){  
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}

此处的工作示例:

http://jsbin.com/etogi3/2

changeColorTimerId = -1;


$('.box').hover(function(){
  //mouseOver code here
   changeColorTimerId = setInterval ( changeColor, 1000 );  
},function(){
  //mouseOut  code here  
  if ( changeColorTimerId ){
      clearInterval ( changeColorTimerId  )
  }

});

function changeColor(){
  $(".box").css ( 'backgroundColor',  '' + getRandomColor() );
}

function getRandomColor(){  
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}

working example here:

http://jsbin.com/etogi3/2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文