简体   繁体   English

为特定div的onmouseleave动作添加延迟-javascript,html,jquery

[英]add delay to onmouseleave action for a certain div - javascript, html, jquery

<div class="container4" style="position: relative; left: 0px; top: 0px; width: 80px; height: 90px; border: solid 1px black;" onmouseenter="this.style.border='solid 3px red';" onmouseleave="this.style.border='solid 1px black';" onclick="this.style.background='rgba(0, 0, 0, 0.5)';" oncontextmenu="return false;"></div>

http://jsfiddle.net/X4NXf/1/ http://jsfiddle.net/X4NXf/1/

I need a simple delay of the onmouseleave action for a certain class of divs. 对于特定的div类,我需要onmouseleave动作的简单延迟。

If possible, that delayed action should be canceled if you enter the div again before delayed action gets triggered, and then started again (with delay starting again from the beginning) after you leave the div again. 如果可能的话,如果您在触发延迟动作之前再次进入div,则应取消该延迟动作,然后在再次离开div之后再次启动(延迟从头开始)。

Preferred solution would be jquery function, but I am open for any other approach. 首选解决方案是jquery函数,但我愿意接受任何其他方法。

Try jQuery 试试jQuery

$('.container4').mouseenter(function(){
    var $this =  $(this);
    clearTimeout($this.data('timerMouseleave'));
    $this.css('border', 'solid 3px red')
}).mouseleave(function(){
    var $this =  $(this);
    var timer = setTimeout($.proxy(function(){
        $this.css('border', 'solid 1px black')
    }, this), 2000)
    $this.data('timerMouseleave', timer)
})

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM