Html案例:时钟特效

效果图

这个案例的每个位置的时间的切换是由两个<span>标签完成的

   <li ref="hours1">
                    <span>0</span>
                    <span>0</span>
    </li>

通过position: absolute;top:0;属性来达到切换的效果,显示当前时间的<span>的top值是0,之前的时间的<span>标签的top值改变为100%,哪个<span>标签获得spantop样式,就会隐藏起来,在这个过程中会逐渐变得模糊

//spantop样式
.time-box li .spantop {
    top: -100%;
    opacity: 0;
}
...........
//实现spantop的切换和时间的更新
repeat(a,c,d){
            a.forEach(element => {
                element.classList.remove('spantop')
            });
            let b = c?[0,1]:[1,0]
            a[b[0]].innerText = this.noatime[d]
            a[b[1]].classList.add('spantop')
  }

获取时间的方法,执行该方法会将data的noatime变量进行更新 this.noatime = this.acquisitiontime().split(''),利用split('')将得到的字符串变成一个数组

acquisitiontime() {
            const date = new Date()
            let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
            let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
            let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()

            return hours + '' + minutes + '' + seconds
        },

再通过if查看数组里的内容和<span>标签里的内容有无差别,有差别就会把新的内容给带有spantop样式的<span>标签,然后清除spantop样式

            this.noatime = this.acquisitiontime().split('')
            let a =this.statement()
            if (this.noatime[0]!==a.hours1[0].innerText&& this.noatime[0]!==a.hours1[1].innerText) {
                this.repeat(a.hours1,this.hours1,0)
                this.hours1 = !this.hours1
            }
.....................................
 repeat(a,c,d){
            a.forEach(element => {
                element.classList.remove('spantop')
            });
            let b = c?[0,1]:[1,0]
            a[b[0]].innerText = this.noatime[d]
            a[b[1]].classList.add('spantop')
        }

下面是所有的特效代码:

<!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>
    <link rel="stylesheet" href="./css/index.css">
</head>
<body>
    <div id="app">
        <div class="big-box">
            <h3>{{title}}</h3>
            <ul class="time-box">
                <li ref="hours1">
                    <span>0</span>
                    <span>0</span>
                </li>
                <li ref="hours2">
                    <span>0</span>
                    <span>0</span>
                </li>
                <li>
                   <i>:</i>
                </li>
                <li ref="minutes1">
                    <span>0</span>
                    <span>0</span>
                </li>
                <li ref="minutes2">
                    <span>0</span>
                    <span>0</span>
                </li>
                <li>
                   <i>:</i>
                </li>
                <li ref="seconds1">
                    <span>0</span>
                    <span>0</span>
                </li>
                <li ref="seconds2">
                    <span>0</span>
                    <span>0</span>
                </li>
            </ul>
        </div>
    </div>
    <script src="./js/vue.js"></script>
    <script src="./js/index.js"></script>
</body>
</html>
body {
    margin: 0;
    padding: 0;
    background: linear-gradient(-31deg,#222225 0%,#47474c 100%,#6e7ff3 100%);
}

ul,li {
    margin: 0;
    padding: 0;
    list-style: none;
}

h3,p {
    margin: 0;
    padding: 0;
}

.big-box {
    width: 100%;
    height: 100%;
    margin: 100px auto;
    text-align: center;
}

.big-box h3 {
    font-size: 40px;
    margin: 10px auto;
    color: #4d5dc9;
    text-shadow: -1px 1px 6px #4d5dc9a2;
}

.time-box {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    width: 700px;
    height: 280px;
    margin: 0 auto;
    padding: 0 20px;
    background-color: #4d5dc9;
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.5);
}

.time-box li {
    width: 100px;
    height: 200px;
    line-height: 200px;
    text-align: center;
    color: white;
    font-size: 80px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    border-radius: 16px;
}

.time-box li:nth-child(3),.time-box li:nth-child(6){
    width: 20px;
    box-shadow: none;
}

.time-box li span {
    display: block;
    position: absolute;
    top: 0;
    width: 100px;
    height: 200px;
    line-height: 200px;
    background-color: #9ca7ee;
    opacity: 1;
    transition: all 0.6s;
}

.time-box li .spantop {
    top: -100%;
    opacity: 0;
}

.time-box li i {
    display: block;
    font-size: 60px;
    margin: 0;
    padding: 0;
    font-style: normal;
}
Vue.config.productionTip = false //阻止vue在启动时生成生产提示

new Vue({
    el: '#app',
    data: {
        noatime: '',
        title: 'The time now is',
        seconds2: true,
        seconds1: true,
        minutes2: true,
        minutes1: true,
        hours2: true,
        hours1: true,
    },
    methods: {
        acquisitiontime() {
            const date = new Date()
            let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
            let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
            let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()

            return hours + '' + minutes + '' + seconds
        },
        statement(){
            let seconds1 = this.$refs.seconds1.querySelectorAll('span')
            let seconds2 = this.$refs.seconds2.querySelectorAll('span')
            let minutes1 = this.$refs.minutes1.querySelectorAll('span')
            let minutes2 = this.$refs.minutes2.querySelectorAll('span')
            let hours1 = this.$refs.hours1.querySelectorAll('span')
            let hours2 = this.$refs.hours2.querySelectorAll('span')

            return {
                seconds1,
                seconds2,
                minutes1,
                minutes2,
                hours1,
                hours2
            }
        },
        circulate(){
            this.noatime = this.acquisitiontime().split('')
            let a =this.statement()
            if (this.noatime[0]!==a.hours1[0].innerText&& this.noatime[0]!==a.hours1[1].innerText) {
                this.repeat(a.hours1,this.hours1,0)
                this.hours1 = !this.hours1
            }
            if (this.noatime[1]!==a.hours2[0].innerText&& this.noatime[1]!==a.hours2[1].innerText) {
                this.repeat(a.hours2,this.hours2,1)
                this.hours2 = !this.hours2
            }
            if (this.noatime[2]!==a.minutes1[0].innerText&& this.noatime[2]!==a.minutes1[1].innerText) {
                this.repeat(a.minutes1,this.minutes1,2)
                this.minutes1 = !this.minutes1
            }
            if (this.noatime[3]!==a.minutes2[0].innerText&& this.noatime[3]!==a.minutes2[1].innerText) {
                this.repeat(a.minutes2,this.minutes2,3)
                this.minutes2 = !this.minutes2
            }
            if (this.noatime[4]!==a.seconds1[0].innerText&& this.noatime[4]!==a.seconds1[1].innerText) {
                this.repeat(a.seconds1,this.seconds1,4)
                this.seconds1 = !this.seconds1
            }
            if (this.noatime[5]!==a.seconds2[0].innerText&& this.noatime[5]!==a.seconds2[1].innerText) {
                this.repeat(a.seconds2,this.seconds2,5)
                this.seconds2 = !this.seconds2
            }
        },
        repeat(a,c,d){
            a.forEach(element => {
                element.classList.remove('spantop')
            });
            let b = c?[0,1]:[1,0]
            a[b[0]].innerText = this.noatime[d]
            a[b[1]].classList.add('spantop')
        }
    },
    mounted() {
        setInterval(()=>{
            this.circulate()
        },1000)
    },

})
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,112评论 2 34
  • client,page和screen的区别? clientX,clientY是触摸点相对于viewport视口x,...
    change_22fa阅读 1,611评论 1 1
  • 一、html基础1、你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么?IE: trident内核Firef...
    Smallbore阅读 933评论 0 15
  • date/****js相关********************************************...
    Agping阅读 435评论 0 1
  • 前端常见的一些问题 1.前端性能优化手段? 1. 尽可能使用雪碧图2. 使用字体图标代替图片3. 对HTML,cs...
    十八人言阅读 1,090评论 0 1