javascript实现图片抖动

  javascript实现图片抖动源代码,通过设置marginTop和marginLeft实现

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>javascript实现图片抖动源代码</title>
<style type="text/css">.shakeimage{position:absolute; left:200px; top:200px;}</style>
</head>
<body>
<input type="button" value="开始抖动" onclick="start(this,10,20,'imgShake')" />
<img src="/logo.jpg" class="shakeimage" alt="编程设计网" id="imgShake" /> 
<script type="text/javascript">
    function shake(img,range) {
        var v = Math.floor(Math.random() * range), style = Math.floor(Math.random() * 2) == '0' ? 'marginTop' : 'marginLeft'; 
        img.style[style] = v + "px";
    }
    //range为抖动幅度,越大越厉害
    //timeout:抖动频率,单位ms
    function start(o, range, timeout, imgID) {
        if (o.value == '开始抖动') {
            var img = document.getElementById(imgID);
            shake(img, range);
            window.timer = setInterval(function () { shake(img, range); }, timeout);
            o.value = '停止抖动';
        }
        else {
            o.value = '开始抖动';
            clearInterval(window.timer);
        }
    }
</script> 
</body></html> 

 

加支付宝好友偷能量挖...


评论(0)网络
阅读(142)喜欢(1)JavaScript/Ajax开发技巧