用JS 做的 动画 带停止和开始

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<div id="showImg" style="width:320px; height:375px;"></div>
<button id="start">开始</button>
<button id="stop">停止</button>
<input type="text" value="200" id="speed" />
</body>
</html>
<script type="text/javascript">
 

var FBFAnimation = (function(){
    function start() {
        if (this.timeLine) return;
        var speed = (this._json.speed ? this._json.speed : 500), bthis = this;
        this.timeLine = setInterval(function() {
            animation.apply(bthis);
        }, speed);
    };
 
    function stop() {
        if (this.timeLine) clearTimeout(this.timeLine);
        this.timeLine = null;
    };
 
    function setSpeed(val) {
        if (!val) return;
        this._json.speed = val;
    };
 
    function getPosition() {
        var x = 0, y = 0;
        if ((this.startFrames == this.endFrames) && this._json.loop) this.startFrames = 0;
        x = -(this.width * this.startFrames);
 
        this.startFrames++;
        return { x : x, y : y };
    };
 
    function animation() {
        if (!this._json.loop && this.startFrames == this.endFrames - 1) this.stop();
        var offset = getPosition.apply(this);
        this._json.target.style.backgroundPosition = ""+ offset.x +"px "+ offset.y +"px";
    };
 
    function init() {
        var target = this._json.target;
        this.width = 0;
        this.height = 0;
        this.startFrames = 0;
        this.endFrames = this._json.frames;
        target.style.backgroundImage = 'url(' + this._json.img + ')';
        target.style.backgroundRepeat = "no-repeat";
        target.style.backgroundPosition = "0 0";
 
        if (!this._json.width) this.width = parseInt(this._json.target.style.width);
        if (!this._json.height) this.height = parseInt(this._json.target.style.height);
    };
 
    return function(args) {
        this._json = args || {};
 
        if (!this._json.target) throw new Error('target is not exist!');
        if (!this._json.img) throw new Error('img not exist!');
        if (!this._json.frames) throw new Error('frames number not exist');
        if (typeof this._json.loop == "undefined") this._json.loop = true;
 
        init.apply(this);       //initialize
 
        this.start = start;     //interface
        this.stop = stop;
        this.setSpeed = setSpeed;
    };
})();
 /*{
target:null,
img:url,
frames:number, 帧数
speed:500 hm,
loop:true,
width:null, //舞台尺寸
height:null
}*/
var s = new FBFAnimation({
    target : document.getElementById('showImg'),
    img : 'img.png',
    frames : 7,
    speed : 200
});
 
document.getElementById('start').onclick = function() {
    s.start();
};
 
document.getElementById('stop').onclick = function() {
    s.stop();
};
 
document.getElementById('speed').onchange = function() {
    s.setSpeed(document.getElementById('speed').value);
}
</script>

转载于:https://www.cnblogs.com/lianyuewuxia/p/6492793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值