N0ts Blog
最爱冰激凌
wordpress小工具添加恋爱计时
前言
换了新主题过后,一直都在改改改这个主题,改完后呢又觉得想改改工具栏,然后花了半个小时硬塞一段代码,终于实现了我想要的效果 技术不是很好,但是写的代码勉强能用哈哈,这里我就分享出来吧
时间统计代码来源于网络,其他都是自己写的啦
<div id="lovexhj" style="width: 100%; height: 100px; text-align: center; font-size: 1rem;">
<div id="lovexhjImage" style="width: 220px; margin: 0 auto;">
<!-- 左边的头像 -->
<img src="https://q1.qlogo.cn/g?b=qq&nk=304915556&s=640" alt="love" style="width: 60px; border-radius: 50%;">
<!-- 中间的图片 -->
<img src="https://n0tssss.oss-cn-shanghai.aliyuncs.com/n0ts-blog/uploads/2020/02/1582858549-lovexhjlogo.gif" alt="love" style="width: 60px; border-radius: 50%;">
<!-- 右边的头像 -->
<img src="https://q1.qlogo.cn/g?b=qq&nk=1656071287&s=640" alt="love" style="width: 60px; border-radius: 50%;">
</div>
<p id="lovexhjSitetime" style="font-size: 0.8rem;"></p>
<script language=javascript>
function lovexhjSitetime() {
window.setTimeout("lovexhjSitetime()", 1000);
var seconds = 1000
var minutes = seconds * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var today = new Date()
var todayYear = today.getFullYear()
var todayMonth = today.getMonth()
var todayDate = today.getDate()
var todayHour = today.getHours()
var todayMinute = today.getMinutes()
var todaySecond = today.getSeconds()
// 时间设置
var t1 = Date.UTC(2018, 11, 19, 00, 00, 00)
var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond)
var diff = t2 - t1
var diffYears = Math.floor(diff / years)
var diffDays = Math.floor((diff / days) - diffYears * 365)
var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours)
var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes)
var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
diffMinutes * minutes) / seconds)
document.getElementById("lovexhjSitetime").innerHTML = "我们相恋了" + diffYears + "年" + diffDays + "天" +
diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒啦"
}
lovexhjSitetime()
</script>
</div>
食用方法
html的代码直接在wordpress后台 - 外观 - 小工具 ,然后添加一个自定义html的小工具把代码复制进去就行啦!
更改时间的代码位置与头像的位置我已经标注出来啦,调用的是QQ头像的接口
https://q1.qlogo.cn/g?b=qq&nk=您的QQ号&s=640
只需要更改一下QQ账号即可,大小不用管已经设置好啦!
如果有主题不一样什么的就自己改改代码啦,这个代码很简单的哈哈,
所有大小、距离用的都是基础代码
如果还有问题欢迎留言