模拟"点击并按住5秒钟"使用jquery

时间:2016-02-02 14:41:17

标签: jquery

我在一个元素上做了一些CSS3过渡,使用" active"选择。我现在需要的是编程"点击并按住4-5秒"当页面加载时,该元素上的行为,而无需用户实际点击它。

有没有办法模拟"点击并按住"在特定的时间内,使用jQuery还是javascript?

这样的东西
$('div').click(5000);

显然不起作用。

谢谢!

3 个答案:

答案 0 :(得分:2)

如果您坚持使用JQuery进行模拟,那么mousedown事件应该可行。像这样:

$('div').mousedown(function(){
    setTimeout(function(){ $('div').mouseup(); }, 5000);
});
祝你好运。

答案 1 :(得分:0)

您可以使用"延迟" jQuery

$('div').
    mouseenter(function(){ 
        // some code 
    }).delay(5000).mouseleave(function(){ 
                   // some code
    });

答案 2 :(得分:0)

你可以使用触发器。



$(function(){
  
  // Bind mousedown/mouseup events
  $('.box')
  .on('mousedown', function(){
    $(this).addClass('active');    
  })
  .on('mouseup', function(){
    $(this).removeClass('active');
  });
  
  // Trigger mousedown
  $('.box').trigger('mousedown');
  // Trigger mouseup 5s later
  setTimeout(function(){
    $('.box').trigger('mouseup');
  },5000);
  
  
});

.box{
  width: 100px;
  height: 100px;
  border: 3px solid #f00;
}

.active{
  background: #f00;  
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="box"></div>
&#13;
&#13;
&#13;

相关问题