使用jquery 模拟元素上的“单击并按住5秒”
我使用“active”选择器在元素上完成了一些CSS3过渡。 我现在需要的是在页面加载时对该元素的“单击并保持4-5秒”行为进行编程,而无需用户实际点击它。
有没有办法模拟“点击并保持”特定的时间,使用jQuery或javascript?
就像是
$('div').click(5000);
这显然不起作用。
谢谢!
如果你坚持不得不用JQuery模拟它,那么mousedown事件应该可行。 像这样的东西:
$('div').mousedown(function(){ setTimeout(function(){ $('div').mouseup(); }, 5000); });
祝好运。
你可以使用jQuery的“延迟”来使用以下内容
$('div'). mouseenter(function(){ // some code }).delay(5000).mouseleave(function(){ // some code });
你可以使用触发器。
$(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; }