HTML5和CSS3七CSS3四

这篇具有很好参考价值的文章主要介绍了HTML5和CSS3七CSS3四。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

代码下载地址

Animation动画

Transision过渡动画是从一个状态到另一个状态,而Animation动画可以有多个关键帧

animation-name:设置动画ID

animation-duration:设置动画总时长

animation-timing-function:设置动画时间函数,同过渡动画

animation-iteration-count:设置动画播放次数,默认1次,可以是具体次数也可以是infinite(表示无限次)

animation-direction:设置交替动画,alternate来回交替

animation-delay:设置动画延迟

animation-fill-mode:设置动画开始/结束时状态,默认动画执行完毕会恢复到原始状态

  • forwards:动画执行完毕保持动画结束状态,但在有延迟的情况下不会立马进入动画的开始状态
  • backwards:动画执行完毕不会保持动画结束状态,但在有延迟的情况下会立马进入动画的开始状态
  • both:动画执行完毕既会保持动画结束状态,但在有延迟的情况下也会立马进入动画的开始状态

animation-play-state:设置动画播放running/暂停paused状态

@keyframes identifier { }:创建动画

n% {}:创建关键帧,百分比指的是动画耗时的百分比,也可以使用fromto分别表示 0%、100%两处的关键帧

<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>Animation</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        h2 {
            margin: 20px;
            text-align: center;
        }
        h3 {
            margin: 15px;
            text-align: center;
        }

        div.base {
            margin: auto;
            width: 200px;
            height: 250px;
        }
        div.base > input {
            margin-top: 115px;
        }
        div.animation {
            width: 100px;
            height: 100px;
            background-color: black;

            animation-name: animationBase;
            animation-duration: 3s;
            animation-timing-function: ease-in-out;
            animation-iteration-count: 1;
            animation-direction: alternate;
            animation-delay: 1s;
            animation-fill-mode: backwards;
            animation-play-state: running;
        }
        
        /* 创建动画 */
        @keyframes animationBase {
            /* 百分比指的是动画耗时的百分比 */
            0% {
                transform: translate(0px, 0px) rotate(90deg);
            }
            25% {
                transform: translate(100px);
            }
            50% {
                transform: translate(100px, 100px);
            }
            75% {
                transform: translate(0px, 100px);
            }
            100% {
                transform: translate(0px, 0px);
            }
        }
    </style>
</head>
<body>
    <h2>Animation动画</h2>
    <h3>动画基础</h3>
    <div class="base">
        <div class="animation"></div>

        <input id="paused" type="button" value="暂停">
        <input id="running" type="button" value="开始">
    </div>

    <script>
        var animationEle = document.querySelector(".animation");
        document.querySelector("#paused").onclick = function () {
            animationEle.style.animationPlayState = "paused";
        }
        document.querySelector("#running").onclick = function () {
            animationEle.style.animationPlayState = "running";
        }
    </script>
</body>

无缝滚动案例

实现思想:

  • 首先将图片放入父类容器中浮动排列成一排
  • 对父容器执行动画,并复制一份图片用于填补动画过程中出现的空缺,并使用overflow: hidden来隐藏超出父容器的图片
  • 对父容器添加hover事件,实现鼠标停留位置决定动画播放与暂停
        div.scrollDiv {
            margin: auto;
            width: 480px;
            height: 120px;
            overflow: hidden;
        }
        div.scrollDiv > ul.scrollUl {
            width: 960px;
            animation-name: scrollAnimation;
            animation-duration: 5s;
            animation-timing-function: linear;
            animation-iteration-count: infinite;
        }
        div.scrollDiv:hover > ul.scrollUl {
            /* 光标为小手 */
            cursor: pointer;
            animation-play-state: paused;
        }
        div.scrollDiv > ul.scrollUl > li {
            width: 120px;
            float: left;
        }
        div.scrollDiv > ul.scrollUl > li > img {
            width: 120px;
            height: 120px;
        }
        @keyframes scrollAnimation {
            from {
                transform: translate(0);
            }
            to {
                transform: translate(-480px);
            }
        }
        
    <h3>案例无缝滚动</h3>
    <div class="scrollDiv">
        <ul class="scrollUl">
            <li><img src="images/scroll1.png" alt=""></li>
            <li><img src="images/scroll2.png" alt=""></li>
            <li><img src="images/scroll3.png" alt=""></li>
            <li><img src="images/scroll4.png" alt=""></li>
            <li><img src="images/scroll1.png" alt=""></li>
            <li><img src="images/scroll2.png" alt=""></li>
            <li><img src="images/scroll3.png" alt=""></li>
            <li><img src="images/scroll4.png" alt=""></li>
        </ul>
    </div>

时钟案例

时钟案例主要用到了position定位、transform变换、animation动画这三个方面的知识点,如下几点需要注意:

  • border-radius百分比值参照的是父元素的实际(注意border、padding的影响)宽高,position定位百分比参照父元素,transform的百分比参照自身
  • 动画会覆盖元素的属性,需要将属性值在动画中重新设置

动画简写
animation: id 时长 时间函数 延迟时间 执行次数 交替方向

        div.clock {
            width: 200px;
            height: 200px;
            margin: auto;
            margin-bottom: 20px;
            border: solid 8px black;
            border-radius: 50%;
            position: relative;
        }
        div.line {
            width: 8px;
            height: 200px;
            background-color: black;
            position: absolute;
            left: 50%;
            transform: translate(-50%);
        }
        div.line1, div.line4 {
            width: 10px;
        }
        div.line2 {
            transform: translate(-50%) rotate(30deg);
        }
        div.line3 {
            transform: translate(-50%) rotate(60deg);
        }
        div.line4 {
            transform: translate(-50%) rotate(90deg);
        }
        div.line5 {
            transform: translate(-50%) rotate(120deg);
        }
        div.line6 {
            transform: translate(-50%) rotate(150deg);
        }
        div.cover {
            width: 184px;
            height: 184px;
            border-radius: 92px;
            background-color: white;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        div.pin {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -100%);
            transform-origin: center bottom;
        }
        div.hour {
            background-color: red;
            width: 7px;
            height: 50px;
            animation: clockAnimation 43200s linear infinite;
        }
        div.minute {
            background-color: green;
            width: 5px;
            height: 65px;
            animation: clockAnimation 3600s linear infinite;
        }
        div.second {
            background-color: blue;
            width: 3px;
            height: 80px;
            animation: clockAnimation 60s steps(60) infinite;
        }
        div.center {
            width: 15px;
            height: 15px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border-radius: 50%;
            background-color: darkgray;
        }

        @keyframes clockAnimation {
            from {
                transform: translate(-50%, -100%) rotate(0);
            }
            to {
                transform: translate(-50%, -100%) rotate(360deg);
            }
        }
        
    <h3>时钟案例</h3>
    <div class="clock">
        <div class="line line1"></div>
        <div class="line line2"></div>
        <div class="line line3"></div>
        <div class="line line4"></div>
        <div class="line line5"></div>
        <div class="line line6"></div>

        <div class="cover"></div>

        <div class="pin hour"></div>
        <div class="pin minute"></div>
        <div class="pin second"></div>

        <div class="center"></div>
    </div>

web字体和字体图标

可以为网页指定特殊字体而无需考虑用户是否安装此字体,不同系统不同浏览器支持的字体文件不同(主要有ttf、otf、woff、eot、svg)。

首先得自定义想生成对应字体文件的内容,然后利用网络资源(这里使用的是阿里webfont)生成对应的web字体,使用方法如下:

  • 使用@font-face申明字体(注意,路径一定要改为本地文件路径,这个字体文件只包含生成文件的文字)
  • 定义使用 webfont 的样式
  • 为文字加上对应的样式
    <style>
        @font-face {
            font-family: 'webfont';
            font-display: swap;
            src: url('fonts/webfont_mjhhrv4rmgj/webfont.eot'); /* IE9 */
            src: url('fonts/webfont_mjhhrv4rmgj/webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
            url('fonts/webfont_mjhhrv4rmgj/webfont.woff2') format('woff2'),
            url('fonts/webfont_mjhhrv4rmgj/webfont.woff') format('woff'), /* chrome、firefox */
            url('fonts/webfont_mjhhrv4rmgj/webfont.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
            url('fonts/webfont_mjhhrv4rmgj/webfont.svg#webfont') format('svg'); /* iOS 4.1- */
        }
        .web-font {
            font-family: webfont;
        }
    </style>

<body>
    <span class="web-font">可以为网页指定特殊字体而无需考虑用户是否安装此字体,不同系统不同浏览器支持的字体文件不同(主要有ttf、otf、woff、eot、svg)。撒娇时代峰峻</span>
</body>

把常用图标借助工具生成字体包,就可以像文字一样使用图标了,这样做具有如下有点:

  • 打包成字体库,减少请求
  • 具有矢量性,保证清晰度
  • 使用灵活,便于维护
        @font-face {
            font-family: 'iconfont';
            src: url('fonts/font_ny15g6aig4b/iconfont.ttf') format('truetype');
        }
        .icon-font {
            font-family: iconfont;
            font-size: 50px;
            color: red;
        }
        
    <span class="icon-font">&#xe8ae;&#xe8b0;&#xe8b9;</span> <br>

直接使用编码来表示图标,不直观也不方便,可以利用伪元素更加方便直观的表示:

        .dingdan::before {
            font-family: iconfont;
            content: "\e8ae";
            font-size: 50px;
            color: red;
        }
        .fengxiang::before {
            font-family: iconfont;
            content: "\e8b0";
            font-size: 50px;
            color: green;
        }
        .shouji::before {
            font-family: iconfont;
            content: "\e8b9";
            font-size: 50px;
            color: blue;
        }
        
    <span class="dingdan"></span>
    <span class="fengxiang"></span>
    <span class="shouji"></span>

多列布局

CSS3 中的多列布局(multi-column)是对传统 html 块状布局的有力扩充。常用属性如下:

  • column-count:设置列数
  • column-width:设置列的宽度
  • column-gap:设置列间隙(取大优先,如果人为设置宽度大则取更大的值但会填充屏幕——意味着最终宽度更大来适配屏幕;如果设置宽度更小则使用计算的宽度)
  • column-rule:设置列之间的宽度、颜色和样式(与边框样式添加一样)
  • column-span:设置元素横跨多少列(n:夸n列,all:夸所有列;但是设置为大于1小于列数的值都会只夸一列)
        div.wrapper {
            box-sizing: border-box;
            width: 100%;
            padding: 20px;
            column-count: 3;
            column-rule: 3px dashed black;
            column-gap: 50px;
            column-width: 200px;
        }
        h2 {
            text-align: center;
            column-span: all;
        }

伸缩布局

布局的传统方式基于盒模型,依赖 position、display、float 属性。对于一些特殊布局非常不方便,CSS3 做了非常大的改进,使得对块级元素的布局十分灵活,适应性强,其强大的伸缩性在响应式开发中发挥极大作用。

display:flex:一个容器设置这个属性,那么子元素自动变成伸缩项(lex item)

justify-content:设置弹性盒子在主轴方向上的排列方式,其他属性值如下:
flex-start:子元素向行起始位置对齐
flex-end:子元素向行结束位置对齐
center:子元素向行中间位置对齐
space-between:子元素平均分布在行里,首末子元素分别向行起始、结束位置对齐,其余子元素中间平均分布产生相同间距
space-around:将多余空间平均分布在每个子元素两侧,造成中间间距是两边的两倍

    <style>
        h2 {
            text-align: center;
        }
        div.box {
            box-sizing: border-box;
            border: 1px solid darkgray;
            margin: auto;
            width: 1000px;
            height: 200px;
            display: flex;
            justify-content: space-around;
        }
        div.box > div {
            width: 200px;
            height: 100%;
        }
        div.box > div.first {
            background-color: red;
        }
        div.box > div.second {
            background-color: green;
        }
        div.box > div.third {
            background-color: blue;
        }
        div.box > div.fourth {
            background-color: cyan;
        }
    </style>
</head>
<body>
    <h2>伸缩布局</h2>
    <div class="box">
        <div class="first"></div>
        <div class="second"></div>
        <div class="third"></div>
        <div class="fourth"></div>
    </div>
</body>

当子元素宽度和大于父元素时,子元素会自动平均收缩

flex-wrap:控制子元素是否换行显示,默认不换行(nowrap:不换行收缩显示,wraper:换行显示,wraper-reverse:翻转从最后一行开始排列)

flex-direction:设置元素主轴即排列方向,默认主轴是 row 水平方向排列(row:水平方向从左到右排列,row-reverse:水平方向从右到左排列,column:垂直方向从上到下排列,column:垂直方向从下到上排列)

flex-flow:是 flex-wrap 和 flex-direction 的综合

        div.box1 {
            margin: 20px auto;
            width: 900px;
            height: 600px;
            box-sizing: border-box;
            border: 1px solid darkgray;
            display: flex;
            justify-content: space-around;
            /* flex-wrap: wrap-reverse;
            flex-direction: column-reverse; */
            flex-flow: wrap-reverse column-reverse;
        }
        div.box1 > div {
            width: 200px;
            height: 200px;
        }
        div.box1 > div.one {
            background-color: red;
        }
        div.box1 > div.two {
            background-color: green;
        }
        div.box1 > div.three {
            background-color: blue;
        }
        div.box1 > div.four {
            background-color: cyan;
        }
        div.box1 > div.five {
            background-color: pink;
        }
        
    <div class="box1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
        <div class="five">5</div>
    </div>

flex-grow:可以扩展子元素的宽度,设置当前元素应该占据剩余空间的比例值(当前元素的flex-grow值除以所有兄弟元素flex-grow值之和),默认值为0(并不会占据剩余空间)。

        div.box2 {
            margin: auto;
            width: 900px;
            height: 400px;
            border: 1px solid darkgray;
            display: flex;
            justify-content: space-around;
        }
        div.box2 > div {
            width: 200px;
            height: 200px;
        }
        div.box2 > div.one {
            background-color: red;
            flex-grow: 1;
        }
        div.box2 > div.two {
            background-color: green;
        }
        div.box2 > div.three {
            background-color: blue;
            flex-grow: 1;
        }
        
    <h3>flex-grow</h3>
    <div class="box2">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>

flex-shrink:可以收缩子元素的宽度,设置当前元素应该占据收缩空间的比例值(当前元素的flex-shrink值除以所有兄弟元素flex-shrink值之和),默认值为1(平均占收缩空间)。

        div.box3 {
            margin: auto;
            width: 500px;
            height: 400px;
            border: 1px solid darkgray;
            display: flex;
            justify-content: space-around;
        }
        div.box3 > div {
            width: 200px;
            height: 200px;
        }
        div.box3 > div.one {
            background-color: red;
            flex-shrink: 2;
        }
        div.box3 > div.two {
            background-color: green;
            flex-shrink: 1;
        }
        div.box3 > div.three {
            background-color: blue;
            flex-shrink: 1;
        }
        
    <h3>flex-shrink</h3>
    <div class="box3">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>

flex:flex属性是flex-grow、flex-shrink和flex-basis的简写(默认值0 1 auto),后两个属性为可选。

        div.box4 {
            width: 100%;
            height: 500px;
            background-color: antiquewhite;
            display: flex;
        }
        div.box4 > div.left {
            flex: 1;
            background-color: red;
        }
        div.box4 > div.right {
            flex: 4;
            background-color: green;
        }

    <h3>flex</h3>
    <div class="box4">
        <div class="left"></div>
        <div class="right"></div>
    </div>

在不知道具体数量的情况下,无法通过设置width的百分比来做到占据整个父容器,此时使用flex就可以轻松做到:

        div.box5 {
            width: 600px;
            height: 500px;
            margin: auto;
            border: 1px dotted darkgray;
        }
        div.box5 > ul {
            margin: 0;
            padding: 0;
            width: 100%;
            list-style: none;
            display: flex;
        }
        div.box5 > ul > li {
            flex: 1;
            margin: 0;
            line-height: 44px;
            box-sizing: border-box;
            text-align: center;
            border: 1px solid skyblue;
            background-color: aquamarine;
        }
        
    <h3>伸缩菜单项</h3>
    <div class="box5">
        <ul>
            <li>首页</li>
            <li>商品分类</li>
            <li>我的订单</li>
            <li>最新商品</li>
            <li>联系我们</li>
        </ul>
    </div>

align-items:设置子元素(伸缩项)在侧轴方向上的对齐方式(center:侧轴方向居中对齐;flex-start:侧轴方向顶对齐;flex-end:侧轴方向底对齐;strench:让子元素在侧轴方向拉伸填充整个整个侧轴方向,这个是默认值,但是设置高度后会失效;baseline:侧轴方向文本基线对齐)。

align-self:设置单个元素在侧轴方向的对齐方式

        div.box6 {
            margin: auto;
            border: 1px solid darkgray;
            width: 900px;
            height: 600px;
            display: flex;
            justify-content: space-around;
            align-items: baseline;
        }
        div.box6 > div {
            width: 200px;
            height: 200px;
        }
        div.box6 > div.one {
            background-color: red;
        }
        div.box6 > div.two {
            background-color: green;
        }
        div.box6 > div.three {
            background-color: blue;
            align-self: flex-end;
        }
        
    <h3>align-items</h3>
    <div class="box6">
        <div class="one" style="font-size: 20px;">One</div>
        <div class="two" style="font-size: 100px;">Two</div>
        <div class="three" style="font-size: 50px;">Three</div>
    </div>

伸缩盒子案例:

        div.display {
            margin: auto;
            width: 500px;
            height: 600px;
            background-color: lightgray;
            /* 设置父容器为伸缩盒子 */
            display: flex;
            /* 默认主轴为row,这里需要以列的方式排列 */
            flex-direction: column;
        }
        header {
            width: 100%;
            height: 44px;
            background-color: red;
        }
        main {
            width: 100%;
            background-color: green;
            /* 占据父容器剩余空间 */
            flex: 1;
            display: flex;
        }
        article {
            height: 100%;
            background-color: cyan;
            flex: 1;
        }
        aside {
            height: 100%;
            background-color: pink;
            flex: 3;
        }
        footer {
            width: 100%;
            height: 49px;
            background-color: blue;
        }
        
    <h3>综合案例</h3>
    <div class="display">
        <header></header>
        <main>
            <article></article>
            <aside></aside>
        </main>
        <footer></footer>
    </div>

案例总结

案例一携程网,主要使用了语义化标签、伪类选择器和伸缩布局两个知识点,这里是代码链接。

案例二切割轮播图,主要使用了伪类选择器、三维变换、transition动画以及jquery,这里是代码链接。

案例三360引导页,主要使用了伪类选择器、二维变换、transition动画,fullpage以及jquery,这里是代码链接。文章来源地址https://www.toymoban.com/news/detail-422945.html

到了这里,关于HTML5和CSS3七CSS3四的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • HTML5CSS3提高

    HTML5 的新增特性主要是针对于以前的不足,增加了一些新的标签、新的表单和新的表单属性等。 这些新特性都有兼容性问题,基本是 IE9+ 以上版本的浏览器 才支持,如果不考虑兼容性问题,可以大量使用这些新特性。 以前布局,我们基本用 div 来做。div 对于搜索引擎来说,

    2024年02月14日
    浏览(33)
  • 尚硅谷html5+css3(3)布局

    1.文档流normal flow -网页是一个多层结构 -通过CSS可以分别为每一层设置样式 -用户只能看到最顶层 -最底层:文档流(我们所创建的元素默认都是从文档流中进行排列) 2.盒子模型   1.盒子模型-边框 2.盒子模型-内边距 3.盒子模型-外边距 4.盒子模型-水平方向的布局 5.盒子模型

    2024年04月09日
    浏览(50)
  • h5(html5)+css3前端笔记二

    一、表格标签 表格的主要作用: 表格主要用于 显示、展示数据 ,因为它可以让数据显示的非常的规整,可读性非常好。特别是后台展示数据的时候,能够熟练运用表格就显得很重要。一个清爽简约的表格能够把繁杂的数据表现得很有条理。 1. table/table 用来定义表格的标签

    2024年02月14日
    浏览(45)
  • HTML5+CSS3实现小米商城 (完整版)

    对于小米商城,也是自己初学前端的一个小作品吧,这个网页大概写了有三四天吧,总体感受就是写着还行,只要有耐心,就一定能成功。 毕竟第一次做,代码写的可能比较乱,命名可能也不是太规范,以后多加改正。 基本还原了原网页,并且只用到了CSS3和HTML5。 链接:

    2024年02月04日
    浏览(39)
  • 响应式Web开发项目教程(HTML5+CSS3+Bootstrap)第2版 第1章 HTML5+CSS3初体验 项目1-1 三栏布局页面

    三栏布局是一种常用的网页布局结构。 除了头部区域、底部区域外,中间的区域(主体区域)划分成了三个栏目,分别是左侧边栏、内容区域和右侧边栏,这三个栏目就构成了三栏布局。当浏览器的宽度发声变化时,页面中左侧边栏和右侧边栏的宽度固定不变,而内容区域的

    2024年01月17日
    浏览(47)
  • 携程网移动端首页制作(html5+css3)

    主要是自己做记录,记录做的过程以及遇到的一些问题 1.首先引入normal.css(Normalize.css: Make browsers render all elements more consistently.)。其中搜索符号使用图标,阿里图标(iconfont-阿里巴巴矢量图标库):打开网址搜索需要的图标,添加到自己的项目中,点击“download code” ,下载

    2024年02月05日
    浏览(34)
  • 【前端基础篇】HTML5 + CSS3 入门知识

    万维网的核心语言、标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改(这是一项推荐标准、外语原文:W3C Recommendation) HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定。 HTML5的设计目的是为了在移动设备上支持多媒体。 HTML5 简

    2024年02月09日
    浏览(64)
  • HTML5+CSS3实现小米官网(完整版)

    小米官网其实是一个结构非常简单的网页,相比与小米商城,难度降低了很多很多。我也写过一次小米商城,可以移步到我的主页。本篇文章简单分析一下小米官网的静态结构,文章末尾附源代码和素材。 先展示一下页面的效果吧! 1. 头部导航栏 头部导航栏可以用 fixed 定位

    2024年02月13日
    浏览(61)
  • 网页设计(八)HTML5基础与CSS3应用

    当当网企业用户注册页面 改版后当当网企业用户注册页面 验证码 HTML5新增加结构元素设计布局页面 HAB公司行业案例局部初始页面 HAB公司行业案例局部鼠标盘旋时特效页面 鼠标盘旋时指向“查看更多”时超链接效果 文字素材: “服务浦东政务”“HAB公司连续多年保持政务信

    2024年01月17日
    浏览(42)
  • html5和css3实现3d正方体旋转

    5.用动画keyframes让其旋转起来:

    2024年02月02日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包