2

このdivからマウスを離したときにdivの色を変更しようとして
います。このdivで5秒戻ってからdivの背景色を変更するのではなく
、divから最後のマウスアウト時間を取得するためのコードです

<html>
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Effects - Animate demo</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <style>
    #effect { width: 240px; padding: 0.4em; position: relative; background: yellow; }
  </style>
  <script>
  var sec = 0;
        function pad ( val ) { return val > 9 ? val : "0" + val; }
        setInterval( function(){
            $("#effect").attr("name",pad(++sec%60));
        }, 1000);
        $(document).ready(function(){
        $("#effect").mouseout(function (){
            var time = $(this).attr("name");
            alert("at Mouse out:- "+time+" Second ");
        });
        });
  </script>
</head>
<body>
  <div id="effect" class="ui-widget-content ui-corner-all">ON Ready Status...
</div>
</body>
</html>
<script>
</script>

5秒後にdivの色を変更するにはどうすればよいですか? マウスアウトの?

4

3 に答える 3

2
    $("#test").on('mouseenter', function() {
        $("#test").css('background-color', 'red');
    });


    $("#test").on('mouseleave', function() {
            var timer=self.setInterval(function() {   
           $("#test").css('background-color', 'blue');
            window.clearInterval(timer);},5000);
    });
});

フィドル: http://jsfiddle.net/jsAgX/

于 2013-10-09T07:53:13.137 に答える
1

試す

#effect.mouseout {
    background: red;
}

$(document).ready(function () {
    $("#effect").hover(function () {
        var $this = $(this)
        clearTimeout($this.data('mouseouttimer'));
        $this.removeClass('mouseout');
    }, function () {
        var $this = $(this)
        var timer = setTimeout(function () {
            $this.addClass('mouseout');
        }, 2000);
        $this.data('mouseouttimer', timer);
    }).trigger('mouseleave');
});

デモ:フィドル

于 2013-10-09T07:51:37.300 に答える
1

以下を使用して、5 秒後にコードを実行します

setTimeout(function() {
//your code
},5000);
于 2013-10-09T07:49:47.180 に答える