日期:2014-05-16  浏览次数:20356 次

如何延迟鼠标事件
如何给鼠标点击加上一个延迟,例如下面是点击图片 会给图片加边框的代码,如何做到点击1秒后才出现边框?

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>JS+CSS给图片加上鼠标滑过的方框</title>
<style>
.borderimage{
border:5px solid #ffffff;
}
</style>
<script language="JavaScript">
function borderit(which,color){
//if IE 4+ or NS 6+
if (document.all||document.getElementById){
which.style.borderColor=color
}
}
</script>
<>
<img src="http://pic10.nipic.com/20100926/2874022_122448725818_2.jpg" class="borderimage" onMousedown="borderit(this,'#1c1c1c')" onMouseup="borderit(this,'white')">
</div>
</html>


------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>JS+CSS给图片加上鼠标滑过的方框</title>
<style>
    .borderimage
    {
        border: 5px solid #ffffff;
    }
</style>
<script language="JavaScript">
    var v;
    function borderit(which, color) {
        //if IE 4+ or NS 6+
        if (document.all || document.getElementById) {
            which.style.borderColor = color;
        }
    }
    function _borderit(which, color) {
        return function () {
            borderit(which, color);
        }
     }
    function myfunction(which, color) {

        v = setTimeout(_borderit(which,color), 1000);
    };


    function stopfunction(which, color) {
        clearTimeout(v); 
        borderit(which, color); 
    }
</script>
<div>
    <img src="http://pic10.nipic.com/20100926/2874022_122448725818_2.jpg" class="borderimage"
        onmousedown="myfunction(this,'#1c1c1c')" onmouseup="stopfunction(this,'white')">
</div>
</html>