Web前端概述

TomTao626 于 2023-03-29 发布
🥰本站访客数 👀本文阅读量

Web前端概述

说明:本文使用的部分插图来自 Jon DuckettHTML and CSS: Design and Build Websites一书,这是一本非常棒的前端入门书,有兴趣的读者可以在亚马逊或者其他网站上找到该书的购买链接。

HTML 是用来描述网页的一种语言,全称是 Hyper-Text Markup Language,即超文本标记语言。我们浏览网页时看到的文字、按钮、图片、视频等元素,它们都是通过 HTML 书写并通过浏览器来呈现的。

HTML简史

  1. 1991年10月:一个非正式CERN(欧洲核子研究中心)文件首次公开18个HTML标签,这个文件的作者是物理学家蒂姆·伯纳斯-李,因此他是万维网的发明者,也是万维网联盟的主席。
  2. 1995年11月:HTML 2.0标准发布(RFC 1866)。
  3. 1997年1月:HTML 3.2作为W3C推荐标准发布。
  4. 1997年12月:HTML 4.0作为W3C推荐标准发布。
  5. 1999年12月:HTML4.01作为W3C推荐标准发布。
  6. 2008年1月:HTML5由W3C作为工作草案发布。
  7. 2011年5月:W3C将HTML5推进至“最终征求”(Last Call)阶段。
  8. 2012年12月:W3C指定HTML5作为“候选推荐”阶段。
  9. 2014年10月:HTML5作为稳定W3C推荐标准发布,这意味着HTML5的标准化已经完成。

HTML5新特性

  1. 引入原生多媒体支持(audio和video标签)
  2. 引入可编程内容(canvas标签)
  3. 引入语义Web(article、aside、details、figure、footer、header、nav、section、summary等标签)
  4. 引入新的表单控件(日历、邮箱、搜索、滑条等)
  5. 引入对离线存储更好的支持(localStorage和sessionStorage)
  6. 引入对定位、拖放、WebSocket、后台任务等的支持

使用标签承载内容

结构

文本

列表(list)

链接(anchor)

图像(image)

表格(table)

表单(form)

音视频(audio / video)

窗口(frame)

其他

使用CSS渲染页面

简介

颜色(color)

文本(text / font)

盒子(box model)

列表、表格和表单

图像

布局

使用JavaScript控制行为

JavaScript基本语法

面向对象

BOM

DOM

JavaScript API

前端框架

渐进式框架 - Vue.js

前后端分离开发(前端渲染)必选框架。

快速上手
  1. 引入Vue的JavaScript文件,我们仍然推荐从CDN服务器加载它。

    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    
  2. 数据绑定(声明式渲染 )。

    <div id="app">
    	<h1>库存信息</h1>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			product: 'iPhone X'
    		}
    	});
    </script>
    
  3. 条件与循环。

    <div id="app">
    	<h1>库存信息</h1>
        <hr>
    	<ul>
    		<li v-for="product in products">
    			 - 
    			<span v-if="product.quantity === 0">
    				已经售罄
    			</span>
    		</li>
    	</ul>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			products: [
    				{"id": 1, "name": "iPhone X", "quantity": 20},
    				{"id": 2, "name": "华为 Mate20", "quantity": 0},
    				{"id": 3, "name": "小米 Mix3", "quantity": 50}
    			]
    		}
    	});
    </script>
    
  4. 计算属性。

    <div id="app">
    	<h1>库存信息</h1>
    	<hr>
    	<ul>
    		<li v-for="product in products">
    			 - 
    			<span v-if="product.quantity === 0">
    				已经售罄
    			</span>
    		</li>
    	</ul>
    	<h2>库存总量:台</h2>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			products: [
    				{"id": 1, "name": "iPhone X", "quantity": 20},
    				{"id": 2, "name": "华为 Mate20", "quantity": 0},
    				{"id": 3, "name": "小米 Mix3", "quantity": 50}
    			]
    		},
    		computed: {
    			totalQuantity() {
    				return this.products.reduce((sum, product) => {
    					return sum + product.quantity
    				}, 0);
    			}
    		}
    	});
    </script>
    
  5. 处理事件。

    <div id="app">
    	<h1>库存信息</h1>
    	<hr>
    	<ul>
    		<li v-for="product in products">
    			 - 
    			<span v-if="product.quantity === 0">
    				已经售罄
    			</span>
    			<button @click="product.quantity += 1">
    				增加库存
    			</button>
    		</li>
    	</ul>
    	<h2>库存总量:台</h2>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			products: [
    				{"id": 1, "name": "iPhone X", "quantity": 20},
    				{"id": 2, "name": "华为 Mate20", "quantity": 0},
    				{"id": 3, "name": "小米 Mix3", "quantity": 50}
    			]
    		},
    		computed: {
    			totalQuantity() {
    				return this.products.reduce((sum, product) => {
    					return sum + product.quantity
    				}, 0);
    			}
    		}
    	});
    </script>
    
  6. 用户输入。

    <div id="app">
    	<h1>库存信息</h1>
    	<hr>
    	<ul>
    		<li v-for="product in products">
    			 - 
    			<input type="number" v-model.number="product.quantity" min="0">
    			<span v-if="product.quantity === 0">
    				已经售罄
    			</span>
    			<button @click="product.quantity += 1">
    				增加库存
    			</button>
    		</li>
    	</ul>
    	<h2>库存总量:台</h2>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			products: [
    				{"id": 1, "name": "iPhone X", "quantity": 20},
    				{"id": 2, "name": "华为 Mate20", "quantity": 0},
    				{"id": 3, "name": "小米 Mix3", "quantity": 50}
    			]
    		},
    		computed: {
    			totalQuantity() {
    				return this.products.reduce((sum, product) => {
    					return sum + product.quantity
    				}, 0);
    			}
    		}
    	});
    </script>
    
  7. 通过网络加载JSON数据。

    <div id="app">
    	<h2>库存信息</h2>
    	<ul>
    		<li v-for="product in products">
    			 - 
    			<span v-if="product.quantity === 0">
    				已经售罄
    			</span>
    		</li>
    	</ul>
    </div>
       
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
    	const app = new Vue({
    		el: '#app',
    		data: {
    			products: []
    		},
    		created() {
    			fetch('https://jackfrued.top/api/products')
    				.then(response => response.json())
    				.then(json => {
    					this.products = json
    				});
    		}
    	});
    </script>
    
使用脚手架 - vue-cli

Vue为商业项目开发提供了非常便捷的脚手架工具vue-cli,通过工具可以省去手工配置开发环境、测试环境和运行环境的步骤,让开发者只需要关注要解决的问题。

  1. 安装脚手架。
  2. 创建项目。
  3. 安装依赖包。
  4. 运行项目。

UI框架 - Element

基于Vue 2.0的桌面端组件库,用于构造用户界面,支持响应式布局。

  1. 引入Element的CSS和JavaScript文件。

    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    
  2. 一个简单的例子。

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    	</head>
    	<body>
    		<div id="app">
    			<el-button @click="visible = true">点我</el-button>
    			<el-dialog :visible.sync="visible" title="Hello world">
    				<p>开始使用Element吧</p>
    			</el-dialog>
                </div>
    	</body>
    	<script src="https://unpkg.com/vue/dist/vue.js"></script>
    	<script src="https://unpkg.com/element-ui/lib/index.js"></script>
    	<script>
    		new Vue({
    			el: '#app',
    			data: {
    				visible: false,
    			}
    		})
    	</script>
    </html>
    
  3. 使用组件。

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    	</head>
    	<body>
    		<div id="app">
    			<el-table :data="tableData" stripe style="width: 100%">
    				<el-table-column prop="date" label="日期" width="180">
    				</el-table-column>
    				<el-table-column prop="name" label="姓名" width="180">
    				</el-table-column>
    				<el-table-column prop="address" label="地址">
    				</el-table-column>
    			</el-table>
    		</div>
    	</body>
    	<script src="https://unpkg.com/vue/dist/vue.js"></script>
    	<script src="https://unpkg.com/element-ui/lib/index.js"></script>
    	<script>
    		new Vue({
    			el: '#app',
    			data: {
    				tableData:  [
    					{
    						date: '2016-05-02',
    						name: '王一霸',
    						address: '上海市普陀区金沙江路 1518 弄'
    					}, 
    					{
    						date: '2016-05-04',
    						name: '刘二狗',
    						address: '上海市普陀区金沙江路 1517 弄'
    					}, 
    					{
    						date: '2016-05-01',
    						name: '杨三萌',
    						address: '上海市普陀区金沙江路 1519 弄'
    					}, 
    					{
    						date: '2016-05-03',
    						name: '陈四吹',
    						address: '上海市普陀区金沙江路 1516 弄'
    					}
    				]
    			}
    		})
    	</script>
    </html>
    

报表框架 - ECharts

百度出品的开源可视化库,常用于生成各种类型的报表。

基于弹性盒子的CSS框架 - Bulma

Bulma是一个基于Flexbox的现代化的CSS框架,其初衷就是移动优先(Mobile First),模块化设计,可以轻松用来实现各种简单或者复杂的内容布局,即使不懂CSS的开发者也能够使用它定制出漂亮的页面。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Bulma</title>
	<link href="https://cdn.bootcss.com/bulma/0.7.4/css/bulma.min.css" rel="stylesheet">
	<style type="text/css">
		div { margin-top: 10px; }
		.column { color: #fff; background-color: #063; margin: 10px 10px; text-align: center; }
	</style>
</head>
<body>
	<div class="columns">
		<div class="column">1</div>
		<div class="column">2</div>
		<div class="column">3</div>
		<div class="column">4</div>
	</div>
	<div>
		<a class="button is-primary">Primary</a>
		<a class="button is-link">Link</a>
		<a class="button is-info">Info</a>
		<a class="button is-success">Success</a>
		<a class="button is-warning">Warning</a>
		<a class="button is-danger">Danger</a>
	</div>
	<div>
		<progress class="progress is-danger is-medium" max="100">60%</progress>
	</div>
	<div>
		<table class="table is-hoverable">
			<tr>
				<th>One</th>
				<th>Two</th>
			</tr>
			<tr>
				<td>Three</td>
				<td>Four</td>
			</tr>
			<tr>
				<td>Five</td>
				<td>Six</td>
			</tr>
			<tr>
				<td>Seven</td>
				<td>Eight</td>
			</tr>
			<tr>
				<td>Nine</td>
				<td>Ten</td>
			</tr>
			<tr>
				<td>Eleven</td>
				<td>Twelve</td>
			</tr>
		</table>
	</div>
</body>
</html>

响应式布局框架 - Bootstrap

用于快速开发Web应用程序的前端框架,支持响应式布局。

  1. 特点
    • 支持主流的浏览器和移动设备
    • 容易上手
    • 响应式设计
  2. 内容
    • 网格系统
    • 封装的CSS
    • 现成的组件
    • JavaScript插件
  3. 可视化