文字全屏化和动画效果的实现

本文介绍了如何通过JavaScript实现网页的全屏功能,并探讨了三种动画实现方式:使用setInterval、setTimeout以及requestAnimationFrame。文中提供了具体的代码示例,帮助读者理解不同方法的特点。
全屏化
  • 全屏化只有用户自己触发才会实现,如果不绑定事件无法实现
    document.onclick = function(){
    title.webkitRequestFullScreen(); //谷歌等内核
    title.mozRequestFullScreen(); //火狐内核
    }

  • 以下是全屏的方法

    var el = document.documentElement;
    var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;
    if (typeof rfs != "undefined" && rfs) {
    rfs.call(el);
    } else if (typeof window.ActiveXObject != "undefined") {
    var wscript = new ActiveXObject("WScript.Shell");
    if (wscript != null) {
    wscript.SendKeys("{F11}");
    }
    }

使用帧动画

动画可以用 ’ setInterval ’ 或 ’ timeout ’ 的自调实现,也可以用帧动画 ’ requestAnimationFrame ’ 实现(当然,还有canvas动画效果的实现,这里不谈)

这里是部分script代码:
““
var canvas = document.getElementById(‘canvas’);
var cxt = canvas.getContext(‘2d’);

    // 创建小球对象

    var ball = {
        x: 50,
        y: 50,
        r: 30,
        speedX: 5,
        speedY: 2,
        draw: function(){
            cxt.beginPath();
            cxt.arc(this.x,this.y,this.r,0,Math.PI*2,false);
            cxt.closePath();
            cxt.fill();
        },
        move: function (){
            this.x += this.speedX;
            this.y += this.speedY;

            if(this.x >= 500-this.r || this.x <= this.r) {this.speedX *= -1;}
            if(this.y >= 500-this.r || this.y <= this.r){this.speedY *= -1;}
        }
    }
````
  • setInterval 实现动画效果,定时器实现动画效果是通过设置间隔时间来实现的,每隔多少毫秒移动坐标
    ““
    setInterval(function(){
    cxt.clearRect(0,0,500,500);
    ball.draw();
    ball.move();
    },10);

    ““

  • timeout 本意是 setInterval 的延迟效果,但我们可以通过它的自调实现动画

    // 使用timeout实现interval的功能,实际上就是自己调用自己
    function gameloop(){
    cxt.clearRect(0,0,500,500);
    ball.move();
    ball.draw();
    setTimeout(gameloop,10);
    }
    gameloop();
  • requestAnimationFrame 帧动画,帧动画的实现,保证每隔一帧执行一次。两次执行中间的时间间隔不确定,由电脑的性能实现,人眼在16帧每秒以上才会觉得连贯。
    注意:帧动画可以通过利用 ’ 取余运算 ’ 实现事件多少帧执行一次,取消动画的方式和 interval 、 timeout 一样,都有单独的方法,都有把返回值做参数
    ““
    function gameloop(){
    cxt.clearRect(0,0,500,500);
    ball.move();
    ball.draw();

        window.requestAnimationFrame(gameloop);
    
    }
    gameloop();
    
    
    // 取消帧动画
    document.onclick = function(){
        window.cnacelAnimationFrame(a);
    }
    

    ““

多个setInterval的清除

如果我们有多个timer,那么清除的时候我们是否需要使用clearInterval进行一个个的清除呢?

相信大家都有答案,如果多个定时器的清除我们需要多个clearIntervval的话,会造成代码的冗余,一点都不简练。

这里我介绍一种清除全部定时器的方法,通过遍历可以实现全部定时器的清除
““
var timer1 = setInterval(function(){
console.log(‘timer1’);
},300);
var timer2 = setInterval(function(){
console.log(‘timer2’);
},300);
var timer3 = setInterval(function(){
console.log(‘timer3’);
},300);

    window.onclick = function(){
        for(var i =0 ; i < 4; i++){
            clearInterval(i);
        }
    }

““

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值