百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>CSS教程> 怎样运用CSS和Vanilla.js实现展现iphone设施的交互动画(附源码
分享文章到:

怎样运用CSS和Vanilla.js实现展现iphone设施的交互动画(附源码

发布时间:08/01 来源:未知 浏览: 关键词:
本篇文章给大家带来的内容是对于怎样用CSS和Vanilla.js实现展现iphone设施的交互动画(附源码),有一定的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。 本篇文章给大家带来的内容是对于怎样用CSS和Vanilla.js实现展现iphone设施的交互动画(附源码),有一定的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

结果预览

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,包括 5 个子元素,离别代表 苹果, mini, ipad, macbook, imac 这 5 种设施:

    
    
    
    
    

居中显示:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #aaa;
}

设定容器中子元素的布局方式:

.container {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
}

设定设施的共有属性,线性渐变图案将作为屏幕的配景:

.device {
    box-sizing: border-box;
    position: relative;
    display: flex;
    justify-content: center;
    background: linear-gradient(120deg, #ddd 30%, #ccc 30%);
}

.device::before,
.device::after {
    content: '';
    position: absolute;
}

苹果, mini, ipad 的外型类似,都有顶部摄像头、传感器开口和底部按钮,所以这些共有属性可以一起设定,用 ::before 伪元素画出顶部细节,::after 伪元素画出底部按钮:

.苹果::before,
.mini::before,
.ipad::before {
    width: 2px;
    height: 2px;
    border-style: solid;
    border-color: #a5adbe;
    border-width: 0 12px 0 2px;
}

.苹果::after,
.mini::after,
.ipad::after {
    width: 8px;
    height: 8px;
    background-color: white;
    border-radius: 50%;
}

接下来逐个画出设施。先画出 苹果 的轮廓:

.苹果 {
    width: 59px;
    height: 124px;
    border: #484f5e solid;
    border-width: 18px 4px;
    border-radius: 6px;
}

定位 苹果 的顶部和底部细节:

.苹果::before {
    top: -10px;
}

.苹果::after {
    bottom: -13px;
}

相似地,画出 mini:

.mini {
    width: 93px;
    height: 138px;
    border: #484f5e solid;
    border-width: 14px 5px;
    border-radius: 10px;
}

.mini::before {
    top: -8px;
}

.mini::after {
    bottom: -11px;
}

再画出 ipad:

.ipad {
    width: 134px;
    height: 176px;
    border: #484f5e solid;
    border-width: 18px 13px;
    border-radius: 12px;
}

.ipad::before {
    top: -10px;
}

.ipad::after {
    bottom: -13px;
}

接下来画 macbook,先画屏幕:

.macbook {
    width: 234px;
    height: 155px;
    border: 8px solid #484f5e;
    border-radius: 7px 7px 0 0;
}

::before 伪元素画出摄像头:

.macbook::before {
    width: 294px;
    height: 14px;
    background-color: #e8ebf0;
    top: calc(100% + 8px);
    border-radius: 0 0 14px 14px;
}

用 ::after 伪元素画出主机:

.macbook::after {
    width: 3px;
    height: 3px;
    background-color: #a5adbe;
    top: -6px;
    border-radius: 50%;
}

接下来画 imac,先画屏幕,屏幕的左、上、右的玄色边框没实用 border 属性画,是由于 border 会在端点处遗留一个歪角,所以改用 box-shadow 实现:

.imac {
    width: 360px;
    height: 215px;
    border-radius: 10px;
    box-shadow: 
        inset 0 14px #484f5e,
        inset 14px 0 #484f5e,
        inset -14px 0 #484f5e;
    border-bottom: 33px solid #e8ebf1;
    transform: translateY(14px);
}

用 ::before 伪元素画出梯形的底座:

.imac::before {
    width: 90px;
    height: 0;
    top: calc(100% + 33px);
    border: solid transparent;
    border-bottom-color: #e2e4e8;
    border-width: 0 10px 47px 10px;
}

用 ::after 伪元素画出顶部的摄像头和屏幕底部的按钮,注意按钮是用 box-shadow 实现的:

.imac::after {
    width: 4px;
    height: 4px;
    background-color: #a5adbe;
    top: 5px;
    border-radius: 50%;
    box-shadow: 0 191px 0 4px #464e5d;
}

至此,设施全部绘制完成。
删除除 苹果 以外的其他设施的 dom 元素,只保存 1 个 dom 元素,背面的动画结果都在这个 dom 元素上变化:

        
        
    

设定容器尺寸,子元素垂直居中,设施的高度占容器高度的 75%:

.container {
    width: 360px;
    height: 350px;
    justify-content: center;
}

.device {
    transform: translateY(-25%);
}

在 dom 中添加 2 个按钮元素,离别用 .left 和 .right 表示:

    
    
        
        
    

定位按钮的位置:

.buttons {
    position: absolute;
    width: inherit;
    font-size: 30px;
    height: 2em;
    bottom: 0;
    display: flex;
    justify-content: space-around;
}

.buttons > * {
    position: relative;
    width: 4em;
}

按钮为向左和向右的箭头:

.buttons > *::before {
    position: absolute;
}

.buttons .left::before {
    content: '←';
    right: 0;
}

.buttons .right::before {
    content: '→';
}

设定按钮样式为圆形:

.buttons > *t::before {
    position: absolute;
    width: 2em;
    height: 2em;
    background-color: #484f5e;
    color: silver;
    text-align: center;
    line-height: 2em;
    border-radius: 1em;
    cursor: pointer;
}

添加鼠标悬停结果:

.buttons > *::before {
    transition: 0.2s;
}

.buttons .left:hover::before {
    width: 4em;
    content: '?';
}

.buttons .right:hover::before {
    width: 4em;
    content: '?';
}

添加按钮点击结果:

.buttons > *:active {
    transform: scale(0.9);
    filter: brightness(0.8);
}

至此,按钮制作结束,接下来新建交互足本。

定义一个猎取元素的函数 $

const $ = (className) => document.getElementsByClassName(className)[0]

定义一个寄存设施名称的数组:

let devices = ['苹果', 'mini', 'ipad', 'macbook', 'imac']

定义点击行为对数据的加工办法,当点击左侧按钮时,把数组最左边的 1 个元素移到最右侧,相反地,当点击右边按钮时,把数组最右侧的 1 个元素移到最左边,这样就可以从 2 个标的目的轮回遍历数组了:

let loop = {
    'left': () => devices.unshift(devices.pop()),
    'right': () => devices.push(devices.shift())
}

定义点击事件,依据数组的变化切换设施:

Array.from($('buttons').children).forEach(element =>
    element.addEventListener('click', function(e) {
        loop[e.target.className]()
        $('device').className = 'device ' + devices[0]
    })
)

最后,设定设施切换的缓动结果:

.device,
.device::before,
.device::after {
    transition: 0.4s cubic-bezier(0.5, 1.7, 0.5, 1.2);
}

大功告成!

以上就是怎样运用CSS和Vanilla.js实现展现iphone设施的交互动画(附源码)的细致内容,更多请关注 百分百源码网 其它相干文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板