前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >给博客添加时间倒计时模块

给博客添加时间倒计时模块

作者头像
团团生活志
发布2022-08-16 08:25:10
2680
发布2022-08-16 08:25:10
举报
文章被收录于专栏:Live专区

引入jquery 在主题的header.php引入以下

代码语言:javascript
复制
<script src="https://static.q6q.cc/qiangzai/javascript/jquery-1.9.1.min.js"></script>

然后把以下css样式放入主题的style.css里

代码语言:javascript
复制
.aside-count .content {
padding: 15px
}
.aside-count .content .item {
margin-bottom: 15px
} 
.aside-count .content .item:last-child {
margin-bottom: 0
}
.aside-count .content .item .title {
font-size: 12px;
color: var(--minor);
margin-bottom: 5px;
display: flex;
align-items: center
}
.aside-count .content .item .title span {
color: var(--theme);
font-weight: 500;
font-size: 14px;
margin: 0 5px
}
.aside-count .content .item .progress {
display: flex;
align-items: center
} 
.aside-count .content .item .progress .progress-bar {
height: 10px;
border-radius: 5px;
overflow: hidden;
background: var(--classC);
width: 0;
min-width: 0;
flex: 1;
margin-right: 5px
}
@keyframes progress {
0% {
    background-position: 0 0
}

100% {
    background-position: 30px 0
}
}
.aside-count .content .item .progress .progress-bar .progress-inner {
width: 0;
height: 100%;
border-radius: 5px;
transition: width 0.35s;
-webkit-animation: progress 750ms linear infinite;
animation: progress 750ms linear infinite
}
.aside-count .content .item .progress .progress-bar .progress-inner-1 {
background: #bde6ff;
background-image: linear-gradient(135deg, #50bfff 25%, transparent 25%, transparent 50%, 
#50bfff 50%, #50bfff 75%, transparent 75%, transparent 100%);
background-size: 30px 30px
}
.aside-count .content .item .progress .progress-bar .progress-inner-2 {
background: #ffd980;
background-image: linear-gradient(135deg, #f7ba2a 25%, transparent 25%, transparent 50%, 
#f7ba2a 50%, #f7ba2a 75%, transparent 75%, transparent 100%);
background-size: 30px 30px
}
.aside-count .content .item .progress .progress-bar .progress-inner-3 {
background: #ffa9a9;
background-image: linear-gradient(135deg, #ff4949 25%, transparent 25%, transparent 50%, 
#ff4949 50%, #ff4949 75%, transparent 75%, transparent 100%);
background-size: 30px 30px
} 
.aside-count .content .item .progress .progress-bar .progress-inner-4 {
background: #67c23a;
background-image: linear-gradient(135deg, #4f9e28 25%, transparent 25%, transparent 50%, 
#4f9e28 50%, #4f9e28 75%, transparent 75%, transparent 100%);
background-size: 30px 30px
}

之后引入以下js,具体是自建js引入还是放在主题的js文件里,自己请随意发挥。

代码语言:javascript
复制
function init_life_time() {
        function getAsideLifeTime() {
            let nowDate = +new Date();
            let todayStartDate = new Date(new Date().toLocaleDateString()).getTime();
            let todayPassHours = (nowDate - todayStartDate) / 1000 / 60 / 60;
            let todayPassHoursPercent = (todayPassHours / 24) * 100;
            $('#dayProgress .title span').html(parseInt(todayPassHours));
            $('#dayProgress .progress .progress-inner').css('width', 
parseInt(todayPassHoursPercent) + '%');
            $('#dayProgress .progress .progress- 
percentage').html(parseInt(todayPassHoursPercent) + '%');
            let weeks = {
                0: 7,
                1: 1,
                2: 2,
                3: 3,
                4: 4,
                5: 5,
                6: 6
            };
            let weekDay = weeks[new Date().getDay()];
            let weekDayPassPercent = (weekDay / 7) * 100;
            $('#weekProgress .title span').html(weekDay);
            $('#weekProgress .progress .progress-inner').css('width', 
parseInt(weekDayPassPercent) + '%');
            $('#weekProgress .progress .progress-percentage').html(parseInt(weekDayPassPercent) + '%');
            let year = new Date().getFullYear();
            let date = new Date().getDate();
            let month = new Date().getMonth() + 1;
            let monthAll = new Date(year, month, 0).getDate();
            let monthPassPercent = (date / monthAll) * 100;
            $('#monthProgress .title span').html(date);
            $('#monthProgress .progress .progress-inner').css('width', parseInt(monthPassPercent) + '%');
            $('#monthProgress .progress .progress-percentage').html(parseInt(monthPassPercent) + '%');
            let yearPass = (month / 12) * 100;
            $('#yearProgress .title span').html(month);
            $('#yearProgress .progress .progress-inner').css('width', parseInt(yearPass) + '%');
            $('#yearProgress .progress .progress-percentage').html(parseInt(yearPass) + '%');
        }
        getAsideLifeTime();
        setInterval(() => {
            getAsideLifeTime();
        }, 1000);
    }
    init_life_time()

到了最后一步 选择合适的位置添加代码

代码语言:javascript
复制
    <div class="aside aside-count">
    <div class="p-3"><span style="font-size: 1.2em; color: orange;"><i class="fas fa-hourglass-half"></i></span> 人生倒计时</div>
    <div class="content">
        <div class="item" id="dayProgress">
            <div class="title">今日已经过去<span></span>小时</div>
            <div class="progress">
                <div class="progress-bar">
                    <div class="progress-inner progress-inner-1"></div>
                </div>
                <div class="progress-percentage"></div>
            </div>
        </div>
        <div class="item" id="weekProgress">
            <div class="title">这周已经过去<span></span>天</div>
            <div class="progress">
                <div class="progress-bar">
                    <div class="progress-inner progress-inner-2"></div>
                </div>
                <div class="progress-percentage"></div>
            </div>
        </div>
        <div class="item" id="monthProgress">
            <div class="title">本月已经过去<span></span>天</div>
            <div class="progress">
                <div class="progress-bar">
                    <div class="progress-inner progress-inner-3"></div>
                </div>
                <div class="progress-percentage"></div>
            </div>
        </div>
        <div class="item" id="yearProgress">
            <div class="title">今年已经过去<span></span>个月</div>
            <div class="progress">
                <div class="progress-bar">
                    <div class="progress-inner progress-inner-4"></div>
                </div>
                <div class="progress-percentage"></div>
            </div>
        </div>
    </div>
</div>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档