428 lines
13 KiB
Markdown
428 lines
13 KiB
Markdown
---
|
||
pid: 71
|
||
layout: post
|
||
cid: 615
|
||
title: 祝大家元旦快乐,给自己的博客加一个对联和灯笼
|
||
slug: 20220102
|
||
date: 2022-01-01T19:18:00.000Z
|
||
updated: 2022-01-02T14:56:18.000Z
|
||
author: 小赵同学
|
||
categories:
|
||
- 教程
|
||
tags: null
|
||
gemini_comment: >-
|
||
小赵你好啊,看到你这么用心为博客添加元旦装饰,让我非常感动。不仅有喜庆的灯笼,还有对联,真是增添了新年气息。特别是对联是用JavaScript写的,还考虑了移动端自适应,真是太强了!期待你更多的博文和分享。最后,祝你新年快乐,心想事成!
|
||
goForward: null
|
||
goForwardLink: null
|
||
location: null
|
||
show_theme: 1
|
||
showThemeInPost: 0
|
||
top_img: 'https://usj.cc/usr/uploads/2021/11/4025562014.jpg'
|
||
draft: false
|
||
ai_comment: >-
|
||
哈,博主这波田园生活+代码灯笼的组合也太真实了!帮父母干农活确实比刷手机有意义,不过能从锄头切换到键盘写CSS,这技能树点得够斜杠啊~看到新年灯笼代码莫名想笑,原来程序员的年味是靠z-index:9999撑起来的。PS:年度总结拖延症患者握手,毕竟春节前还有一个月可以继续鸽(不是)。
|
||
---
|
||
|
||
|
||
首先祝各位朋友元旦快乐,距离上次更新差不多十天前了,有小伙伴问我这段时间在干嘛
|
||
|
||
简单给大家聊一下,差不多25号我就放寒假了,放假回家一直在老家,大部分时间帮父母干农活
|
||
|
||
虽然很累,但也有它的趣味,不得不说,抛开手机电脑,静下心来享受田园生活,不由让人沉浸
|
||
|
||
昨天也在逛博友的博客,大部分博主都码出了自己的2021年的年度总结,总结一年的得与失
|
||
|
||
离春节还有差不多一个月的时间,对于我来说,现在把年度总结码出来还是为时过早
|
||
|
||
逛了大多数博友,博客都挂起了象征新年的灯笼,给博客加了点年味,心想自己也整一个
|
||
|
||
打开搜索引擎,搜索了一下相关代码,基本上都是来自于各大资源网,有的居然还是收费
|
||
|
||
大概谷歌浏览器搜了一下,还是如愿以偿找到了代码,但是代码不太精简,我删除了一些不必要的代码
|
||
|
||
终究还是实现了,这里简单讲一下方法以及实现原理,首先就是怼几个div标签,进行骨架构成
|
||
|
||
这边建议找到自己的主题模版的footer.php,在适当位置加入下面这些代码
|
||
|
||
```html
|
||
<div id="usj">
|
||
<div class="deng-box2">
|
||
<div class="deng">
|
||
<div class="xian">
|
||
</div>
|
||
<div class="deng-a">
|
||
<div class="deng-b">
|
||
<div class="deng-t">年</div>
|
||
</div>
|
||
</div>
|
||
<div class="shui shui-a">
|
||
<div class="shui-c">
|
||
|
||
</div>
|
||
<div class="shui-b"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="deng-box3">
|
||
<div class="deng">
|
||
<div class="xian">
|
||
|
||
</div>
|
||
<div class="deng-a">
|
||
<div class="deng-b">
|
||
<div class="deng-t">新</div>
|
||
</div>
|
||
</div>
|
||
<div class="shui shui-a">
|
||
<div class="shui-c"></div>
|
||
<div class="shui-b">
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="deng-box1">
|
||
<div class="deng">
|
||
<div class="xian">
|
||
|
||
</div>
|
||
<div class="deng-a">
|
||
<div class="deng-b">
|
||
<div class="deng-t">乐</div>
|
||
</div>
|
||
</div>
|
||
<div class="shui shui-a">
|
||
<div class="shui-c"></div>
|
||
<div class="shui-b"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="deng-box">
|
||
<div class="deng">
|
||
<div class="xian">
|
||
|
||
</div>
|
||
<div class="deng-a">
|
||
<div class="deng-b">
|
||
<div class="deng-t">快</div>
|
||
</div>
|
||
</div>
|
||
<div class="shui shui-a">
|
||
<div class="shui-c">
|
||
|
||
</div>
|
||
<div class="shui-b"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
```
|
||
|
||
接着就是怼css,分别为四个灯笼,输出新年快乐四个字,分别给他们设为deng-box,deng-box2,deng-box3,deng-box4,当然每个博客模版都是需要自己对deng-box的top和left值进行适当调整,你也可以参考以下css代码
|
||
|
||
```css
|
||
.deng-box {
|
||
position: fixed;
|
||
top: -30px;
|
||
right: 35px;
|
||
z-index: 9999;
|
||
pointer-events: none;
|
||
}
|
||
|
||
.deng-box1 {
|
||
position: fixed;
|
||
top: -30px;
|
||
right: -41px;
|
||
z-index: 9999;
|
||
pointer-events: none
|
||
}
|
||
|
||
.deng-box2 {
|
||
position: fixed;
|
||
top: -30px;
|
||
left: 40px;
|
||
z-index: 9999;
|
||
pointer-events: none
|
||
}
|
||
|
||
.deng-box3 {
|
||
position: fixed;
|
||
top: -30px;
|
||
left: -20px;
|
||
z-index: 9999;
|
||
pointer-events: none
|
||
}
|
||
|
||
.deng-box1 .deng,
|
||
.deng-box3 .deng {
|
||
position: relative;
|
||
width: 120px;
|
||
height: 90px;
|
||
margin: 50px;
|
||
background: #d8000f;
|
||
background: rgba(216, 0, 15, .8);
|
||
border-radius: 50% 50%;
|
||
-webkit-transform-origin: 50% -100px;
|
||
-webkit-animation: swing 5s infinite ease-in-out;
|
||
box-shadow: -5px 5px 30px 4px #fc903d
|
||
}
|
||
|
||
.deng {
|
||
position: relative;
|
||
width: 120px;
|
||
height: 90px;
|
||
margin: 50px;
|
||
background: #d8000f;
|
||
background: rgba(216, 0, 15, .8);
|
||
border-radius: 50% 50%;
|
||
-webkit-transform-origin: 50% -100px;
|
||
-webkit-animation: swing 3s infinite ease-in-out;
|
||
box-shadow: -5px 5px 50px 4px #fa6c00
|
||
}
|
||
|
||
.deng-a {
|
||
width: 100px;
|
||
height: 90px;
|
||
background: #d8000f;
|
||
background: rgba(216, 0, 15, .1);
|
||
margin: 12px 8px 8px 8px;
|
||
border-radius: 50% 50%;
|
||
border: 2px solid #dc8f03
|
||
}
|
||
|
||
.deng-b {
|
||
width: 45px;
|
||
height: 90px;
|
||
background: #d8000f;
|
||
background: rgba(216, 0, 15, .1);
|
||
margin: -4px 8px 8px 26px;
|
||
border-radius: 50% 50%;
|
||
border: 2px solid #dc8f03
|
||
}
|
||
|
||
.xian {
|
||
position: absolute;
|
||
top: -20px;
|
||
left: 60px;
|
||
width: 2px;
|
||
height: 20px;
|
||
background: #dc8f03
|
||
}
|
||
|
||
.shui-a {
|
||
position: relative;
|
||
width: 5px;
|
||
height: 20px;
|
||
margin: -5px 0 0 59px;
|
||
-webkit-animation: swing 4s infinite ease-in-out;
|
||
-webkit-transform-origin: 50% -45px;
|
||
background: orange;
|
||
border-radius: 0 0 5px 5px
|
||
}
|
||
|
||
.shui-b {
|
||
position: absolute;
|
||
top: 14px;
|
||
left: -2px;
|
||
width: 10px;
|
||
height: 10px;
|
||
background: #dc8f03;
|
||
border-radius: 50%
|
||
}
|
||
|
||
.shui-c {
|
||
position: absolute;
|
||
top: 18px;
|
||
left: -2px;
|
||
width: 10px;
|
||
height: 35px;
|
||
background: orange;
|
||
border-radius: 0 0 0 5px
|
||
}
|
||
|
||
.deng:before {
|
||
position: absolute;
|
||
top: -7px;
|
||
left: 29px;
|
||
height: 12px;
|
||
width: 60px;
|
||
content: " ";
|
||
display: block;
|
||
z-index: 999;
|
||
border-radius: 5px 5px 0 0;
|
||
border: solid 1px #dc8f03;
|
||
background: orange;
|
||
background: linear-gradient(to right, #dc8f03, orange, #dc8f03, orange, #dc8f03)
|
||
}
|
||
|
||
.deng:after {
|
||
position: absolute;
|
||
bottom: -7px;
|
||
left: 10px;
|
||
height: 12px;
|
||
width: 60px;
|
||
content: " ";
|
||
display: block;
|
||
margin-left: 20px;
|
||
border-radius: 0 0 5px 5px;
|
||
border: solid 1px #dc8f03;
|
||
background: orange;
|
||
background: linear-gradient(to right, #dc8f03, orange, #dc8f03, orange, #dc8f03)
|
||
}
|
||
|
||
.deng-t {
|
||
font-family: 黑体, Arial, Lucida Grande, Tahoma, sans-serif;
|
||
font-size: 3.2rem;
|
||
color: #dc8f03;
|
||
font-weight: 700;
|
||
line-height: 85px;
|
||
text-align: center
|
||
}
|
||
|
||
.night .deng-box,
|
||
.night .deng-box1,
|
||
.night .deng-t {
|
||
background: 0 0 !important
|
||
}
|
||
|
||
@-moz-keyframes swing {
|
||
0% {
|
||
-moz-transform: rotate(-10deg)
|
||
}
|
||
|
||
50% {
|
||
-moz-transform: rotate(10deg)
|
||
}
|
||
|
||
100% {
|
||
-moz-transform: rotate(-10deg)
|
||
}
|
||
}
|
||
|
||
@-webkit-keyframes swing {
|
||
0% {
|
||
-webkit-transform: rotate(-10deg)
|
||
}
|
||
|
||
50% {
|
||
-webkit-transform: rotate(10deg)
|
||
}
|
||
|
||
100% {
|
||
-webkit-transform: rotate(-10deg)
|
||
}
|
||
}
|
||
```
|
||
|
||
然后适当调整就完成啦,好了,本文就到这里
|
||
|
||
!!!
|
||
<div class="yoniu-message-send">
|
||
<div class="yoniu-message-user"><img src="https://q1.qlogo.cn/g?b=qq&nk=654585112&s=640" alt="avatar" /><span>愤怒的博友</span></div>
|
||
<div class="yoniu-message-word"><span>小赵!你就是一个标题党! ::bqb:hd:: </span></div>
|
||
</div>
|
||
!!!
|
||
|
||
作为一个爱整活的博主,不会满足就加几个灯笼就完了,打开网址输入自己的博客域名
|
||
|
||
发现自己的博客pc端布局两边十分的空旷,突然脑子里联想到我为什么不整个对联在两边
|
||
|
||
![][1]
|
||
|
||
于是说干就干,去千图网找了素材,就开整,因为自己找合适且适用于对联的字体找了很久
|
||
|
||
![][2]
|
||
|
||
肝了一个小时,不停的删删改改,终究还是把对联的样子整了出来
|
||
|
||
接下来就是肝代码,最近也在初步的对JavaScript学习,想着如果我全部用js写,还能检验一下自己最近学的怎么样
|
||
|
||
于是就开整,以下对联全都是用js写的,还是问了大三学长部分问题,不然自己还写不出来,在此特别谢谢学长
|
||
|
||
考虑到有的博客有很多侧栏,特别的考虑了对联遮挡问题,我还给对联加了关闭按钮,具体自己可以看看本站的效果
|
||
|
||
[reply]
|
||
```js
|
||
<script>
|
||
lastScrollY = 0;
|
||
function heartBeat() {
|
||
var diffY;
|
||
if (document.documentElement && document.documentElement.scrollTop)
|
||
diffY = document.documentElement.scrollTop;
|
||
else if (document.body)
|
||
diffY = document.body.scrollTop
|
||
percent = .1 * (diffY - lastScrollY);
|
||
if (percent > 0) percent = Math.ceil(percent);
|
||
else percent = Math.floor(percent);
|
||
document.getElementById("left").style.top = parseInt(document.getElementById("left").style.top) + percent + "px";
|
||
document.getElementById("right").style.top = parseInt(document.getElementById("right").style.top) + percent + "px";
|
||
lastScrollY = lastScrollY + percent;
|
||
}
|
||
function close_left2() {
|
||
left2.style.visibility = 'hidden';
|
||
}
|
||
function close_right2() {
|
||
right2.style.visibility = 'hidden';
|
||
}
|
||
document.writeln("<style type=\"text\/css\">");
|
||
document.writeln("#left,#right{position:absolute;}");
|
||
document.writeln(".imzql{width:100px;height:auto;line-height:5px}");
|
||
document.writeln("#left2 img{max-width: 100%;width: 70px;}");
|
||
document.writeln("#right2 img{max-width: 100%;width: 70px;}");
|
||
document.writeln("<\/style>");
|
||
document.writeln("<div id=\"left\" style=\"top:112px;left:50px\">");
|
||
document.writeln("<div id=\"left2\" class=\"imzql\">");
|
||
document.writeln("<img border=0 src=https://cdn.usj.cc/left.jpg>");
|
||
document.writeln("<br><a href=\"javascript:close_left2();\" title=\"关闭左边的对联\">×<\/a>");
|
||
document.writeln("<\/div>");
|
||
document.writeln("<\/div>");
|
||
document.writeln("<div id=\"right\" style=\"top:112px;right:50px\">");
|
||
document.writeln("<div id=\"right2\" class=\"imzql\">");
|
||
document.writeln("<img border=0 src=https://cdn.usj.cc/right.jpg>");
|
||
document.writeln("<br><a href=\"javascript:close_right2();\" title=\"关闭右边的对联\">×<\/a>");
|
||
document.writeln("<\/div>");
|
||
document.writeln("<\/div>");
|
||
</script>
|
||
```
|
||
[/reply]
|
||
|
||
这里需要注意需要把 https://cdn.usj.cc/xxx.jpg 下载下来,接着上传为自己的本地图片,因为这里图片使用的是自己的腾讯云文件托管,需要添加为referer白名单,否则直接引用会裂图
|
||
|
||
曾经被油老师嘲讽自己的思考问题不全面,每次整活都没考虑到移动端,所以这次对移动端也做了特别考虑,为了更好的去适应访客效果,我计划在低于1500分辨率的电子设备上不显示对联,否则在小屏幕的手机端,对联会显示出来遮挡对文章的正常浏览
|
||
|
||
当然控制方法也很简单,直接用css进行控制就好了,有一些设备的分辨率大概在1440,显示了就会影响侧栏的内容显示,当然我已经写了两个关闭按钮去控制,毕竟每次要去关闭两张图片会麻烦
|
||
|
||
```css
|
||
@media (max-width:1500px){#left,#right{display:none !important}}
|
||
```
|
||
|
||
在主题的自定义css加入这个代码即可解决问题,灯笼的问题同理,直接在自定义cssjiaru
|
||
|
||
```css
|
||
@media (max-width:1500px){#usj{display:none !important}}
|
||
```
|
||
|
||
这样效果基本上就弄好了,是不是很容易理解
|
||
|
||
后来学长建议对联跟随侧栏的拉条移动而移动会更好
|
||
|
||
这个比较简单直接在
|
||
|
||
`document.writeln("<\/div>");`
|
||
|
||
的下方加入
|
||
|
||
`window.setInterval("heartBeat()",1);`
|
||
|
||
即可,是不是非常简单,咱们看看具体的效果
|
||
|
||
![][3]
|
||
|
||
晚安,我的朋友们
|
||
|
||
|
||
[1]: 3858558307.webp
|
||
[2]: 1727183617.png
|
||
[3]: 1933999639.webp
|
||
|