学习使用js实现文字和背景颜色闪烁效果

231 篇文章 5 订阅
188 篇文章 1 订阅

学习使用js实现文字和背景颜色闪烁效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文字闪烁特效</title>
</head>
<body>

<span id="myDiv">奇葩天地网www.qipa250.com!</span>

<script>
    var flag = 0;
    var int = '';
    function start() {
        var text = document.getElementById("myDiv");
        if (!flag) {
            text.style.color = "red";
            text.style.background = "#0000ff";
            flag = 1;
        } else {
            text.style.color = "";
            text.style.background = "";
            flag = 0;
        }
        int = setTimeout("start()", 500);
    }

    start();
    setTimeout(function () {
        alert('停止了');
        window.clearInterval(int);
    }, 3000);
</script>
</body>
</html>

在这里插入图片描述

点击指定div闪烁代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文字闪烁特效</title>
</head>
<body>

<span id="myDiv_1" onclick="sendQipa250('myDiv_1')">奇葩天地网!</span>

<span id="myDiv_2" onclick="sendQipa250('myDiv_2')">www.qipa250.com!</span>

<span id="myDiv_3" onclick="sendQipa250('myDiv_3')">www.奇葩250.com!</span>


<script src="jquery-2.1.4.min.js"></script>


<script>


    var time = 6;

    function sendQipa250(div_id) {
        if (time == 0) {//重新获取验证码
            $("#" + div_id).attr("style", "");
            time = 6;
            return false;//清除定时器
        } else {

            //单双数,展示不同的效果
            if (time % 2 == 0) {
                $("#" + div_id).attr("style", "color:red;background:#0000ff;font-size:23px");
            } else {
                $("#" + div_id).attr("style", "font-size:13px");
            }
            time--;
        }
        //设置一个定时器
        setTimeout(function () {
            sendQipa250(div_id)
        }, 500)
    }


</script>
</body>
</html>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的万家灯火背景特效JS代码: ```html <!DOCTYPE html> <html> <head> <title>万家灯火背景特效</title> <style type="text/css"> body { margin: 0; padding: 0; height: 100vh; background-color: #000; overflow: hidden; } canvas { position: absolute; top: 0; left: 0; z-index: -1; } </style> </head> <body> <canvas id="myCanvas"></canvas> <script type="text/javascript"> window.onload = function() { var canvas = document.getElementById('myCanvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext('2d'); var particles = []; function Particle() { this.x = canvas.width * Math.random(); this.y = canvas.height * Math.random(); this.vx = (Math.random() - 0.5) * 5; this.vy = (Math.random() - 0.5) * 5; this.radius = 5; } Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = 'rgba(255, 255, 255, 0.8)'; ctx.fill(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > canvas.width) { this.vx *= -1; } if (this.y < 0 || this.y > canvas.height) { this.vy *= -1; } }; for (var i = 0; i < 1000; i++) { particles.push(new Particle()); } function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].draw(); particles[i].update(); } requestAnimationFrame(loop); } loop(); }; </script> </body> </html> ``` 该代码会在页面背景中生成许多随机移动的白色圆点,效果类似于万家灯火。可以通过调整代码中的参数来改变特效效果,例如圆点数量、运动速度、颜色透明度等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值