整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

HTML5新特性

大有学问#

#头条创作挑战赛#

在HTML基础上增加了新的标签、新的表单标签、新的表单属性。

新增的语义化标签

div标签对搜索引擎来说,是没有语义的。新增的语义标签主要是针对搜索引擎识别的。

头部标签:<header>

导航标签:<nav>

内容标签:<article>

定义文档某个区域:<section>

侧边栏标签:<aside>

尾部标签:<footer>

代码练习

<header>头部标签</header>
<nav>导航栏标签</nav>
<article class="clearF">
  <article class="article1">
    内容标签
    <section>定义文档某个区域</section>
  </article>
  <aside>侧边栏标签</aside>
</article>
<footer>尾部标签</footer>
* {
  margin: 0;
  padding: 0;
}
.clearF {
  clear: both;
}
header,
nav,
article,
footer {
  width: 1200px;
  background-color: antiquewhite;
  border-radius: 15px;
  margin: 10px auto;
  text-align: center;
}
header {
  height: 100px;
  line-height: 100px;
}
nav {
  height: 50px;
  line-height: 50px;
}
article {
  background-color: white;
  height: 300px;
}
.article1 {
  width: 580px;
  text-align: center;
  background-color: antiquewhite;
  border-radius: 15px;
  margin: 0;
  float: left;
}
aside {
  width: 580px;
  height: 300px;
  line-height: 300px;
  background-color: antiquewhite;
  border-radius: 15px;
  float: right;
}  
section {
  width: 300px;
  height: 50px;
  line-height: 50px;
  border-radius: 15px;
  background-color: white;
  margin: auto;
}
footer {
  height: 50px;
  line-height: 50px;
}

代码运行结果

新增多媒体标签

  • 视频标签:<video></video>

语法:

<video src="文件地址"></video>

属性:

src:视频文件路径。

width:播放器宽度。

height:播放器高度。

autoplay:视频就绪自动播放。

controls:向用户显示播放控件。

loop:是否循环播放。

poster:加载等待的画面图片。

muted:静音播放。

代码练习

video {
  width: 500px;
}
<h3>视频文件:自动播放</h3>
<video src="../images/video.mp4" autoplay="autoplay" muted="muted"></video>
<br>
<h3>视频文件:添加播放控件</h3>
<video src="../images/video.mp4" autoplay="autoplay" muted="muted" controls="controls"></video>
<br>
<h3>视频文件设置循环播放</h3>
<video src="../images/video.mp4" autoplay="autoplay" muted="muted" controls="controls" loop="loop"></video>
<br>
<h3>视频文件设置加载图片</h3>
<video src="../images/video.mp4" controls="controls" poster="../images/meitizhub.jpg"></video>
  • 音频标签<audio></audio>

语法:

<audio src="文件地址"></audio>

属性:

src:文件路径。

autoplay:音频就绪自动播放。

controls:向用户显示播放控件。

loop:循环播放。

代码练习

<audio src="../images/audio.mp3" controls="controls"></audio>
<h3>给音频文件添加循环播放</h3>
<audio src="../images/audio.mp3" controls="controls" loop="loop"></audio>

input标签

属性值

  • type属性值

email:输入类型为Email类型

url:输入类型为url类型

date:输入类型为日期。

time:输入类型为时间。

month:输入类型为月。

week:输入类型为周。

number:输入类型为数字。

tel:输入类型为手机号码

search:输入类型为搜索框。

color:生成一个颜色选择表单。

<form action="">
  <ul>
    <li>邮箱:<input type="email"></li>
    <li>网址:<input type="url"></li>
    <li>日期:<input type="date"></li>
    <li>时间:<input type="time"></li>
    <li>月份:<input type="month"></li>
    <li>周:<input type="week"></li>
    <li>数量:<input type="number"></li>
    <li>手机号码:<input type="tel"></li>
    <li>搜索:<input type="search"></li>
    <li>颜色:<input type="color"></li>
    <li><input type="button" value="提交"></li>
  </ul>
</form>


  • 其他属性

required:内容不能为空。

placehoder:提示文本。

autofocus:自动聚焦属性。

autocomplete:on| off 是否显示历史输入。

multiple:提交时可多选文件。

<form action="">
  <input type="srarch" name="srar" id="" required="required" placeholder="请输入内容" autofocus="autofocus" autocomplete="on" >
  <input type="file" multiple="multiple">
  <input type="submit" value="提交">
</form>

下篇文章学习CSS新特性。

编写HTML5页面时,我们很多时候都需要用到表单属性,那么HTML5作为一个新晋IT界红人,HTML5表单属性有哪些呢?今天先来分享一下其中的<form> / <input> autocomplete 属性、<input> autofocus 属性、<input> form 属性以及<input> formaction 属性。

<form> / <input> autocomplete 属性

autocomplete 属性规定 form 或 input 域应该拥有自动完成功能。

当用户在自动完成域中开始输入时,浏览器应该在该域中显示填写的选项。

提示: autocomplete 属性有可能在 form元素中是开启的,而在input元素中是关闭的。

注意: autocomplete 适用于 <form> 标签,以及以下类型的 <input> 标签:text, search, url, telephone, email, password, datepickers, range 以及 color。

实例

HTML form 中开启 autocomplete (一个 input 字段关闭 autocomplete ):

<form action="demo-form.php" autocomplete="on">

First name:<input type="text" name="fname"><br>

Last name: <input type="text" name="lname"><br>

E-mail: <input type="email" name="email" autocomplete="off"><br>

<input type="submit">

</form>

提示:某些浏览器中,您可能需要启用自动完成功能,以使该属性生效。

<form> novalidate 属性

novalidate 属性的一个boolean 属性.

novalidate 属性规定在提交表单时不应该验证 form 或 input 域。

实例

无需验证提交的表单数据

<form action="demo-form.php" novalidate>

E-mail: <input type="email" name="user_email">

<input type="submit">

</form>

<input> autofocus 属性

autofocus 属性是一个 boolean 属性.

autofocus 属性规定在页面加载时,域自动地获得焦点。

实例

让 "First name" input 输入域在页面载入时自动聚焦:

First name:<input type="text" name="fname" autofocus>

<input> form 属性

form 属性规定输入域所属的一个或多个表单。

提示:如需引用一个以上的表单,请使用空格分隔的列表。

实例

位于form表单外的input 字段引用了 HTML form (该 input 表单仍然属于form表单的一部分):

<form action="demo-form.php" id="form1">

First name: <input type="text" name="fname"><br>

<input type="submit" value="Submit">

</form>

Last name: <input type="text" name="lname" form="form1">

<input> formaction 属性

The formaction 属性用于描述表单提交的URL地址.

The formaction 属性会覆盖<form> 元素中的action属性.

注意: The formaction 属性用于 type="submit" 和 type="image".

实例

以下HTMLform表单包含了两个不同地址的提交按钮:

<form action="demo-form.php">

First name: <input type="text" name="fname"><br>

Last name: <input type="text" name="lname"><br>

<input type="submit" value="Submit"><br>

<input type="submit" formaction="demo-admin.php"

value="Submit as admin">

</form>

了解更多计算机相关知识,可登陆北大青鸟广州新嘉华官网查看,或联系在线值班老师了解详情。

元素和伪类

说到这个,我们先回顾一下,伪类选择器伪元素选择器,老版的浏览器没有严格区分下面 2 种写法。

a:after{}
a::after{}

在新的标准中,单冒号(:)用于 CSS3 伪类,双冒号(::)用于 CSS3 伪元素,我们平时开发时可以注意一下,当然大多数浏览器两种写法都能识别。

常见伪元素和伪类伪类

:link, :visited, :hover, :active, :focus, :first-child, :last-child, :nth-child, :nth-last-child, :not()

伪类一般用于一个元素的某个状态,比如说鼠标悬浮,按钮点击,链接已经访问,输入框聚焦等,还用于选择某个特殊元素,比如说多个元素中的第一个,最后一个,偶数,奇数等。其作用是对某个符合以上条件的元素添加一些样式。

a:hover{
        text-decoration: underline;
    }
a:active {
    color: blue;
}
a:link {
    color: red;
}
a:visited {
    color: green;
}

上面的例子展示了一个a标签在不同状态下的不同样式,在未点击链接之前,a标签呈现红色字体(link),在鼠标移到a标签上是,a标签出现下划线(hover),在鼠标按下的时候,a标签变为蓝色(active),点击完了之后,a标签变为绿色(visited)。可以看到,伪类的作用是为了给不同状态的标签添加样式。

伪元素

::first-letter, ::first-line, ::before, ::after

在内容模块中提到,伪元素如果没有设置“content”属性,伪元素是无用的。
使用伪元素插入的内容在页面的源码里是不可见的,只能在 css 里可见。
插入的元素在默认情况下是内联元素(或者,在 html5 中,在文本语义的类别里)。因此,为了给插入的元素赋予高度,填充,边距等等,你通常必须显式地定义它是一个块级元素。
还要注意的是典型的 CSS 继承规则适用于插入的元素。例如,你有字体系列黑体,宋体,无衬线字体应用到 body 元素里,然后伪元素会像其他元素一样继承这些字体系列。
伪元素不会自然继承自父元素(如 padding margins)的样式。
你的直觉是 :before 和 :after 伪元素可能是插入的内容会被注入到目标元素的前或后注入。其实不是这样的,注入的内容将是有关联的目标元素的子元素,但它会被置于这个元素的任何内容的“前”或“后”。

<head>
    <style type="text/css">
        p.box::before {
          content: "#";
          border: solid 1px black;
          padding: 2px;
          margin: 0 10px 0 0;
        }
        p.box::after {
          content: "#";
          border: solid 1px black;
          padding: 2px;
          margin: 0 10px 0 0;
        }
    </style>
</head>
<body>
<p class="box">Other content.</p>
</body>

运行效果:


可以看到,我们html部分只写了一个元素,但是我们利用伪元素渲染出来 3 个部分,前中后,这里我们可以认为,伪元素一般用来辅助html的元素。但在内容页面的源码又看不到,利用伪元素可以实现很多神奇的功能,这里不做具体讲解,后面再出具体教程。

神奇的伪类:focus-within

言归正传,回到我们的主角focus-within,我们知道,伪类focus是指一个元素获得焦点时,为其添加样式。focus-within的范围更广,它表示一个元素获得焦点,或该元素的后代元素获得焦点。划重点,它或它的后代获得焦点。这也就意味着,它或它的后代获得焦点,都可以触发 :focus-within。

这个属性有点类似 Javascript 的事件冒泡,从可获焦元素开始一直冒泡到根元素 html,都可以接收触发 :focus-within 事件,类似下面这个简单的例子这样:

<html>
    <div class="box g-father">
        <div class="box g-children">
            <div class="box button" tabindex="1">button</div>
        </div>
  </div>
  <div class="g-body">HTML</div>
  <style>
    div {
      box-sizing: border-box;
    }
    .button,.g-children {
        width: 100%;
        height: 100%;
        padding: 20px;
        border: 1px solid;
    }
    .g-father {
        width: 200px;
        height: 200px;
        padding: 20px;
        border: 1px solid;
    }
    .g-body {
        margin-top: 20px;
        width: 200px;
        border: 1px solid;
    }
    .g-body:focus-within {
        background-color: #5daf34;
    }
    .g-father:focus-within {
        background-color: #3a8ee6;
    }
    .g-children:focus-within{
        background-color: #2c3e50;
    }
    .button:focus-within {
        background-color: #606266;
        color: red;
    }
        </style>
</html>

运行结果:


可以看到,在button获得焦点时,因为冒泡的原因,它的父级元素全部应用了:focus-within的样式。这里值得注意的是,正常的div是不能获得焦点的,设置 tabindex 属性才能获取焦点,同时按键盘 Tab 键也可让其获取焦点,其中 tabindex 的值越小在 tab 键切换的时候就会首先聚焦。根据:focus-within的特性,我们在不利用 js 的情况下,实现很多实用性的功能。

感应用户聚焦区域

利用focus-within可以增加用户的感知区域,让用户获得更好的视觉反馈。

<html>
    <div class="g-container">
      <input type="text" placeholder="user name" class="g_input" >
      <input type="text" placeholder="code" class="g_input" >
  </div>
  <style>
    .g-container {
        margin-top: 10vh;
    }
    .g-container {
        padding: 10px;
        width: 30vw;
        border: 1px solid #eee;
        transition: all .3s;
        text-align: center;
    }
    .g-container:focus-within {
        transform: translateY(-4px);
        box-shadow: 0 0 10px #ddd;
        border-color: hsl(199, 98%, 48%);
    }
    .g_input {
        border: none;
        width: 20vw;
        padding: 15px;
        font-size: 18px;
        box-sizing: border-box;
        border: 1px solid #ddd;
        overflow: hidden;
        transition: 0.3s;
        box-shadow: 0 0 0px #ddd;
        &:focus {
            box-shadow: 0 0 10px #ddd;
            border-color: hsl(199, 98%, 48%);
        }
    }
    </style>
</html>


可以看到在没有任何javascript逻辑控制情况下,用focus-within就实现了上面的效果。

实现离屏导航

我们先看一下效果:


可以看到是一个很棒的导航效果,而且真个实现没有使用javascript控制,这无疑在性能和体验上都有不少提升。具体源码可以看下面的地址:https://codepen.io/dannievinther/pen/NvZjvz

实现 B 站,掘金等网站登录动效切换

我们平时可能注意到了,B 站和掘金在用户输入密码的时候,上面的图片是捂着眼睛的,这里我们也可以用focus-within来实现。

<html>
    <div class="g-wrap"></div>
        <div class="g-container">
            <h2>登录</h2>
            <div class="g-username">
                <input maxlength="64" placeholder="请输入手机号或邮箱" class="input">
                <img src="https://b-gold-cdn.xitu.io/v3/static/img/greeting.1415c1c.png" class="g-username">
            </div>
            <div class="g-password">
                <input type="password" maxlength="64" placeholder="请输入密码" class="input">
                <img src="https://b-gold-cdn.xitu.io/v3/static/img/blindfold.58ce423.png" class="g-password">
            </div>
            <img src="https://b-gold-cdn.xitu.io/v3/static/img/normal.0447fe9.png" class="g-normal">
    </div>
<style>
.g-wrap {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.3);
}
.g-container {
  position: relative;
  width: 318px;
  margin: 100px auto;
  height: 370px;
  padding: 20px;
  box-sizing: border-box;
  background: #fff;
  z-index: 10;
}
.g-container h2 {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
}
.g-container input {
  outline: none;
  padding: 10px;
  width: 100%;
  border: 1px solid #e9e9e9;
  border-radius: 2px;
  outline: none;
  box-sizing: border-box;
  font-size: 16px;
}
img {
  position: absolute;
  top: -20%;
  left: 50%;
  width: 120px;
  height: 95px;
  transform: translate(-50%, 0);
}
.g-username {
  margin-bottom: 10px;
}
.g-username img {
  display: none;
  width: 120px;
  height: 113px;
}
.g-username:focus-within ~ img {
  display: none;
}
.g-username:focus-within input {
  border-color: #007fff;
}
.g-username:focus-within img {
  display: block;
}
.g-password {
  margin-bottom: 10px;
}
.g-password img {
  display: none;
  width: 103px;
  height: 84px;
  top: -15%;
}
.g-password:focus-within ~ img {
  display: none;
}
.g-password:focus-within input {
  border-color: #007fff;
}
.g-password:focus-within img {
  display: block;
}
</style>
</html>

可以看到,在不适用js的情况下,也能实现动态切换图片的效果,但是还是有一些局限,dom排列只能是父级向上,不能把元素放在focus元素的子元素里面。所以没有js灵活,但是代码量更少。

focus-within 兼容性

因为 css3 的新增特性一直存在兼容问题,这里查询了一下它的兼容性,看到红色区域还是不算太惨淡,出来 ie,其他浏览器基本都支持了。


所有的源码都可以在我的仓库地址:https://github.com/jackzhujie/vue-study
个人博客:http://blog.aizhifou.cn
文章参考链接:https://www.cnblogs.com/coco1s/p/9406413.html

学习如逆水行舟,不进则退,前端技术飞速发展,如果每天不坚持学习,就会跟不上,我会陪着大家,每天坚持推送博文,跟大家一同进步,希望大家能关注我,第一时间收到最新文章。