CSS3技巧38:3D 翻转数字效果

23,019次阅读
没有评论

共计 2289 个字符,预计需要花费 6 分钟才能阅读完成。

博主其它 CSS3 3D 的文章:

CSS3 干货 4:CSS 中 3D 运用_css 3d-CSDN 博客

CSS3 干货 5:CSS 中 3D 运用 -2_中 3d-2-CSDN 博客

CSS3 干货 6:CSS 中 3D 运用 -3_css3d 使用 -CSDN 博客

=======================

最近工作上烦心的事情太多,只有周末才能让我冷静一下 coding 一会玩~

今天做一个 3D 翻转数字效果。示例图如下:

CSS3 技巧 38:3D 翻转数字效果

这个东西看着比较难,其实很简单。

一、结构分析

它由两部分组成:

1. 底层的半截数字

CSS3 技巧 38:3D 翻转数字效果

2. 翻动的半截数字

CSS3 技巧 38:3D 翻转数字效果

每部分都是一个 div。半截数字由 ::before、::after 伪标签制作。

数字由伪标签的 content 设置。

HTML 结构如下:


二、CSS 制作半截数字

半截数字,在这里要分为上半截和下半截。

1. 上半截要设置 line-height 为整个  section 的高。

2. 下半截则要设置 line-height 为 0。

 &::before{
        line-height: $height;
        content: attr(data-before);
}
&::after{
        line-height: 0;
        content: attr(data-after);
}

三、翻转部分制作

翻转的部分,其实就是 2 个半截数字绝对定位,进行重叠。其中,数字 2 的上半截,还要翻转 180deg,因为它要翻转下才会摆正。

 &::after{
    line-height: $height;
    top:0;
    transform-origin: bottom center;
    transform: rotateX(-180deg);
   }

为了保证效果,还要设置翻转部分的 3D 效果。

section  div:nth-child(2){
    transform-style: preserve-3d;
    transition: all 0.5s ease-in-out;
}

四、完成 SCSS 代码

这里用 SCSS 完整整个 CSS。

SCSS 分为了 5 个文件

  • _public.scss  放公用样式。略。
  • _vars.scss 放变量设置。
$page-width: 100vw;
$page-height: 100vh;

$width :200px;
$height: 400px;
  • _mixins.scss 放 SCSS 函数。
@mixin setSize($w, $h) {
    width: $w;
    height: $h;
}
@mixin flex($justify:center, $align:center){
    display: flex;
    justify-content: $justify;
    align-items: $align;
}
  • _pages.scss 存放页面样式
@charset "UTF-8";
// 页面设置
body {@include setSize($page-width, $page-height);
  @include flex();  // 启用 flex 布局,让内容居中
  background: #ddd;
}
// 每个数字
section {@include setSize($width, $height);
  margin-left: auto;
  margin-right: auto;
  position: relative;
  perspective: 1000px;

  div{
    position: absolute;
    font-family: Arial;
    @include setSize($width, $height);
    // 数字的样式
    &::before,
    &::after {
        border-radius: 20px;
        display: block;
        width: $width;
        height: $height/2;
        color: #fff;
        background: linear-gradient(to bottom, #4c4c4c 0%,#0f0f0f 100%);
        font-size: $height*0.8;
        font-weight: bold;
        overflow: hidden;
        line-height: $height;
        text-align: center;
    }
    &::before{
        line-height: $height;
        content: attr(data-before);
    }
    &::after{
        line-height: 0;
        content: attr(data-after);
    }
  } 
}
// 数字翻转
section  div:nth-child(2){
    transform-style: preserve-3d;
    transition: all 0.5s ease-in-out;
   &::before,
   &::after{
    position: absolute;
    backface-visibility: hidden;
    transition: all 0.5s ease-in-out;
   }
   &::before{
    line-height: 0;
    top:$height/2;
    transform-origin: top center;
   }
   &::after{
    line-height: $height;
    top:0;
    transform-origin: bottom center;
    transform: rotateX(-180deg);
   }
}
// 鼠标悬停
section:hover  div:nth-child(2){transform: rotateX(180deg);
}
  • app.scss 依次载入对应的 SCSS 文件。
@import "_vars.scss";
@import "_mixins.scss";
@import "_public.scss";
@import "_page.scss";

生成的 CSS 文件,引入 HTML 即可。

原文地址: CSS3 技巧 38:3D 翻转数字效果

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-04发表,共计2289字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)