七夕来袭,送给未来的你一次JS的创意表白

news/2024/7/19 13:48:07 标签: html, 前端, css, js, 程序员的创意表白
htmledit_views">

七夕来袭!是时候展现专属于程序员的浪漫了!

红豆生南国,春来发几枝。愿君多采撷,此物最相思。

记录一起走过的那些日子

  留给未来的我们。。。

创意代码表白

属于程序员方式的专业浪漫,值得拥有!

  • 效果演示
  • 制作步骤/过程
  • 代码文件

 

 ①css部分

 love.css脚本:

css">* {
    margin: 0;
    padding: 0;
    border: 0;
}
.icon-love {
    width: 400px;
}
html, body {
    width: 100%;
    height: 100%;
}

body {
    /*background-color: skyblue;*/
    overflow: hidden;   /*隐藏超出的部分*/
}

.container {
    width: 100%;
    height: 100%;
    position: relative;
}

/*---------------------- body_left -------------------------*/
.body_left {
    width: 300px;
    height: 300px;
    left: 0;
    bottom: 110px;
    position: absolute;
    z-index: 98;

}

/*---------------------- body_left -------------------------*/

/*---------------------- body_center -------------------------*/
.container .love {
    width: 520px; /* 13 * 40 */
    height: 440px; /* 11 * 40 */
    left: 50%;
    top: 50%;
    position: absolute;
    margin: -260px 0 0 -220px;
    /*background-color: gray;*/
}

.love .block {
    right: 0;
    position: absolute;
    visibility: hidden; /*未开始升空动画前隐藏*/
    background-color: yellow;
}

.love .block div {
    width: 40px;
    height: 40px;
    position: absolute;
    background: url("../images/heart.png") no-repeat;
    background-size: contain;
    /*background-color: #c40908;*/
    /*border: 1px solid silver;*/
    box-sizing: border-box;
}


/*---------------------- body_center -------------------------*/

/*---------------------- footer -------------------------*/
@keyframes border {
    0% {
        width: 0;
    }
    5% {
        width: 5%;
    }
    10% {
        width: 10%;
    }
    15% {
        width: 15%;
    }
    20% {
        width: 20%;
    }
    25% {
        width: 25%;
    }
    30% {
        width: 30%;
    }
    35% {
        width: 35%;
    }
    40% {
        width: 40%;
    }
    45% {
        width: 45%;
    }
    50% {
        width: 50%;
    }
    55% {
        width: 55%;
    }
    60% {
        width: 60%;
    }
    65% {
        width: 65%;
    }
    70% {
        width: 70%;
    }
    75% {
        width: 75%;
    }
    80% {
        width: 80%;
    }
    85% {
        width: 85%;
    }
    90% {
        width: 90%;
    }
    95% {
        width: 95%;
    }
    100% {
        width: 100%;
    }

}
.footer {
    bottom: 30px;
    position: relative;
    z-index: 99;
}
.footer .border .border-top {
    /*width: 0;*/
    /*display: inline-block;*/
    border-top: 3px solid black;
    transform-origin: left center;
    -webkit-animation: border 312 linear;
    -o-animation: border 12s linear;
    animation: border 12s linear;
    animation-fill-mode : both;
    /*border-bottom: none;*/
}

.footer .border .border-bottom {
    /*width: 0;*/
    /*display: inline-block;*/
    float: right;
    border-top: 3px solid red;
    transform-origin: right center;
    -webkit-animation: border 7s linear 12s;
    -o-animation: border 7s linear 12s;
    animation: border 7s linear 12s;
    animation-fill-mode : both;
    /*border-bottom: none;*/
}

.footer .copyright {
    width: 100%;
    height: 30px;
    position: absolute;
    bottom: -30px;
    text-align: center;
    /*background-color: gray;*/

}

.copyright div {
    width: 30%;
    line-height: 30px;
    display: inline-block;
}

.copyright div span {
    color: dimgray;
}

/*---------------------- footer -------------------------*/

②images部分

 

 

 

 

 ③js部分

 love.js代码:

const blk_pitn = { //各小方块相对【自身中心】的位置 -- 【自身中心】确定为#div22的方块
        block1: [[0, 1], [0, 0], [-1, 0], [-1, -1]],
        block2: [[0, 1], [0, 0], [-1, 0], [0, -1]],
        block3: [[-1, 1], [0, 0], [-1, 0], [-1, -1]],
        block4: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
        block5: [[-1, 1], [0, 0], [-1, 0], [0, -1]],
        block6: [[0, -1], [0, 0], [-1, 0], [1, -1]],
        block7: [[-1, -1], [0, 0], [-1, 0], [1, 0]],
        block8: [[-1, 1], [0, 0], [-1, 0], [-1, -1]], /* 3 */
        block9: [[0, -1], [0, 0], [-1, 0], [1, 0]],
        block10: [[-1, 1], [0, 0], [-1, 0], [1, 0]],
        block11: [[2, 0], [0, 0], [-1, 0], [1, 0]], /* — */
        block12: [[0, 1], [0, 0], [-1, 0], [0, -1]], /* 2 */
        block13: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
        block14: [[1, 1], [0, 0], [-1, 0], [1, 0]],
        block15: [[1, -1], [0, 0], [-1, 0], [1, 0]],
        block16: [[-1, -1], [0, 0], [-1, 0], [1, 0]], /* 7 */
        block17: [[0, 1], [0, 0], [-1, 0], [0, -1]], /* 2 */
        block18: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
        block19: [[0, -1], [0, 0], [-1, 0], [1, 0]], /* 9 */
        block20: [[1, -1], [0, 0], [-1, 0], [1, 0]],
        block21: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
        block22: [[1, 1], [0, 0], [-1, 0], [1, 0]], /* 14 */
        block23: [[0, 2], [0, 0], [0, -1], [0, 1]]      /* | */
    },
    offset_pitn = { //各方块block相对【爱心中心】的位置
        block1: [5, 3],
        block2: [5, 1],
        block3: [3, 4],
        block4: [3, 2],
        block5: [3, -1],
        block6: [2, 5],
        block7: [2, 1],
        block8: [1, -1],
        block9: [1, -3],
        block10: [1, 2],
        block11: [0, 3],
        block12: [0, 0], /* 【爱心中心】*/
        block13: [-1, -4],
        block14: [0, -2],
        block15: [-2, 4],
        block16: [-2, 2],
        block17: [-2, 0],
        block18: [-3, -2],
        block19: [-4, 0],
        block20: [-3, 5],
        block21: [-5, 3],
        block22: [-4, 1],
        block23: [-6, 1]    /* 因动画需要移动一个方块,故y轴坐标-1*/
    };

let blocks = document.getElementsByClassName("block"),
    block = blocks[0],
    love = document.getElementsByClassName("love")[0],
    timer = null,
    index = 0,  //记录拼接爱心的动画步骤
    clone_block;    //用于克隆方块

//1.移动方块的【自身中心】到【爱心中心】
block.style.top = "50%";
block.style.left = "50%";
block.style.margin = "-20px 0 0 -20px";

const block_left = parseFloat(window.getComputedStyle(block, null).left.slice(0, -2)), //【爱心中心】 左边距离父元素的距离
    block_top = parseFloat(window.getComputedStyle(block, null).top.slice(0, -2));  //【爱心中心】 顶部距离父元素的距离

function Next() {
    if (++index >= 24) {
        clearInterval(timer);

        Rise();
        // alert("已经是最后一个了!");
        return;
    }

    block.style.visibility = "visible"; //升空动画前允许可见

    //2.移动方块到指定的位置-即是移动【自身中心】到目标位置
    block.style.left = block_left + 40 * offset_pitn["block" + index][0] + "px";
    block.style.top = block_top - 40 * offset_pitn["block" + index][1] + "px";
    for (let i = 0; i < block.children.length; i++) {
        // block.children[1].innerText = index;    //编号便于调试
        block.children[i].style.left = blk_pitn["block" + index][i][0] * -40 + "px";
        /* -40 是因为逻辑坐标和浏览器的x,y轴方向不一样*/
        block.children[i].style.top = blk_pitn["block" + index][i][1] * -40 + "px";
    }

    //3.克隆方块—保存现在的位置
    /* 一共会克隆23个方块,加上原先的一个方块block,共24个方块,即多出原先的block方块*/
    clone_block = block.cloneNode(true);
    love.appendChild(clone_block);

    if (love.children.length >= 24) {
        blocks[blocks.length - 1].children[2].style.display = "none"; //去掉多余的小方块
        block.style.display = "none";   //隐藏多出的block方块
    }
}

function Rise() {
    //4.爱心升高,多出的那个小方块开始掉落
    console.log("开始升空");
    let timer2 = null,
        distance = 0;
    /* 升高时,移动的距离*/
    const target = 120, /* 目标距离*/
        speed = 1;
    /*移动速度*/

    let love_top = parseFloat(window.getComputedStyle(love, null).top.slice(0, -2));  //爱心盒子距离屏幕顶部的距离


    timer2 = setInterval(() => {
        distance += speed;
        // console.log(distance);
        if (distance >= target) {
            clearInterval(timer2);

            console.log("升空完毕");

        }

        love.style.top = (love_top - distance) + "px";

    }, 22);

}

window.onload = function () {
    setTimeout(() => {

        timer = setInterval(() => {
            Next();
        }, 300);


    }, 12000);   //gif图播放完毕所需时间为11.73s
};

 ④index.html代码

html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>爱心表白代码</title>

<link rel="stylesheet" href="css/love.css">

</head>
<body>

<div class="container" onselectstart="return false;" unselectable="on" style="-moz-user-select:none;">

    <div class="body_left">
        <img src="images/biubiubiu.gif" alt="" ondragstart='return false;'>
    </div>

    <div class="body_center love">
        <div class="block">
            <div class="div1"></div>
            <div class="div2"></div>
            <div class="div3"></div>
            <div class="div4"></div>
        </div>
    </div>

</div>

<div class="footer">
    <div class="border">
        <div class="border-top"></div>
        <div class="border-bottom"></div>
    </div>

    <div class="copyright">
        <div id="version"><span>Version:</span>&nbsp;0.0.1</div>
        <div id="createTime"><span>Time:</span>&nbsp;2023/1/12</div>
        <div id="author"><span>&copy;&nbsp;</span>syp</div>
    </div>
</div>

<script type="text/javascript" src="js/love.js"></script>

</body>
</html>

两情若是久长时,又岂在朝朝暮暮。

请记得约定,2023年1月12日! 


http://www.niftyadmin.cn/n/1367953.html

相关文章

判断vue数组中是否存在某个字符串

需求&#xff1a;得到后台数组&#xff0c;需要加一个特定字符串的校验 思路&#xff1a;在已知数组中&#xff0c;嵌套着字符串数组&#xff0c;需要遍历整个字符串数组才能找到那个值。 步骤&#xff1a;按照思路这样处理&#xff0c;无可厚非&#xff0c;肯定是可以的&…

Vue下载与安装

首先安装node.js环境&#xff1a; node.js安装推荐文章&#xff1a;https://www.cnblogs.com/zhouyu2017/p/6485265.html 基于node.js安装淘宝镜像npm install -g cnpm --registryhttps://registry.npm.taobao.org 安装全局vue-cli脚手架,用于帮助搭建所需的模板框架 在cmd…

简单介绍vue和bootstrap整合

一&#xff0c;提前把bootstrap的包下载下来&#xff0c;解压好。 二&#xff0c;在构建完成vue项目后&#xff0c;需要下载jquery插件&#xff1a;cnpm install jquery --save-dev 接着运行下面的3个命令&#xff1a; cnpm install style-loader --save-dev cnpm install …

关于时间插入MYSQL数据库少8个小时的问题

最近项目中遇到一个问题&#xff0c;在测试环境中&#xff0c;通过断点测试&#xff0c;new Date()得出来的时间跟本地时间是一致的&#xff0c;插入数据库的时间也是对的&#xff0c;就是直接返回给前端的时间少了8个小时。然而在正式环境中&#xff0c;new Date()得出来的时间…

Linux 添加项目开机启动

最近由于经常停电的原因&#xff0c;公司内部服务器开机又得重新部署项目&#xff0c;就想着写个脚本让项目在开机时自动启动。 首先进入etc/init.d 创建一个脚本文件&#xff0c;名字随便定义&#xff0c;后面以.sh结尾,如aaa.sh [rootiZo55jsscxjk6eZ bin]# cd / [rootiZo5…

VMware创建多个虚拟机,并分配IP地址,用xshell来连接

1.设置虚拟机的网络连接模式为NAT模式 2.取消DHCP服务。 3.查看虚拟机网段&#xff0c;网关地址 4.设置静态IP vi /etc/sysconfig/network-scripts/ifcfg-ens32 ,ens32这里根据每个人的不同而不同 TYPEEthernet PROXY_METHODnone BROWSER_ONLYno BOOTPROTOstatic …

linux上安装redis

Redis简介 关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库&#xff0c;最终会将数据持久化到磁盘上&#xff0c;而nosql数据 库是基于特殊的结构&#xff0c;并将数据存储到内存的数据库。从性能上而言&#xff0c;nosql数据库 要优于关系型数据库&#xff…

Spring源代码解析

Spring源代码解析(一&#xff09;&#xff1a;IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二)&#xff1a;IoC容器在Web容器中的启动:http://www.iteye.com/topic/86594 Spring源代码解析(三&#xff09;&#xff1a;Spring JDBC:http://www.iteye.com/topic/…