【CSS定位】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、定位
1.1 为什么需要定位?
提问: 以下情况使用标准流或者浮动能实现吗?
某个元素可以自由的在一个盒子内移动位置,并且压住其他盒子。
当我们滚动窗口的时候,盒子是固定屏幕某个位置的。
以上效果,标准流或浮动都无法快速实现,此时需要定位来实现。
所以:
- 浮动可以让多个块级盒子一行没有缝隙排列显示, 经常用于横向排列盒子。
- 定位则是可以让盒子自由的在某个盒子内移动位置或者固定屏幕中某个位置,并且可以压住其他盒子。
1.2 定位组成
定位:将盒子定在某一个位置,所以定位也是在摆放盒子, 按照定位的方式移动盒子。
定位 = 定位模式 + 边偏移
- 定位模式用于指定一个元素在文档中的定位方式
- 边偏移则决定了该元素的最终位置
(1)定位模式
定位模式决定元素的定位方式,它通过 CSS 的 position
属性来设置,其值可以分为四个。
值 |
语义 |
static |
静态定位 |
relative |
相对定位 |
absolute |
绝对定位 |
fixed |
固定定位 |
(2)边偏移
边偏移就是定位的盒子移动的最终位置。有 top
、bottom
、left
和 right
4 个属性。
注意:可以为负值。
边偏移属性 |
实例 |
描述 |
top |
top: 80px |
顶端偏移量,定义元素相对于其父元素上边线的距离 |
bottom |
bottom: 80px |
底部偏移量,定义元素相对于其父元素下边线的距离 |
left |
left: 80px |
左侧偏移量,定义元素相对于其父元素左边线的距离 |
rigth |
right: 80px |
右侧偏移量,定义元素相对于其父元素右边线的距离 |
1.3 静态定位 static(了解)
静态定位是元素的默认定位方式,无定位的意思。
语法:
1
| 选择器 { position: static; }
|
静态定位按照标准流特性摆放位置,它没有边偏移
静态定位在布局时很少用到
1.4 相对定位 relative(重要)
相对定位是元素在移动位置的时候相对于它原来的位置来说的定位(自恋型)。
语法:
1
| 选择器 { position: relative; }
|
相对定位的特点:(务必记住)
- 它是相对于自己原来的位置来移动的(移动位置的时候参照点是自己原来的位置点)
- 原来在标准流的位置继续占有,后面的盒子仍然以标准流的方式对待它
因此,相对定位并没有脱标。它最典型的应用是给绝对定位当爹的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>相对定位</title> <style> .box1 { position: relative; top: 100px; left: 100px; width: 200px; height: 200px; background-color: pink; }
.box2 { width: 200px; height: 200px; background-color: deeppink; } </style> </head>
<body> <div class="box1">
</div> <div class="box2">
</div>
</body>
</html>
|
1.5 绝对定位 absolute(重要)
绝对定位是元素在移动位置的时候相对于它祖先元素来说的定位(拼爹型)。
语法:
1
| 选择器 { position: absolute; }
|
绝对定位的特点:(务必记住)
- 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准定位(Document 文档)
- 如果祖先元素有定位(相对、绝对、固定定位),则以最近一级的有定位祖先元素为参考点移动位置
- 绝对定位不再占有原先的位置(脱标),并且脱标的程度大于浮动(会压住浮动)
所以绝对定位是脱离标准流的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>绝对定位-无父亲或者父亲无定位</title> <style> .father { width: 500px; height: 500px; background-color: skyblue; }
.son { position: absolute;
left: 0; bottom: 0; width: 200px; height: 200px; background-color: pink; } </style> </head>
<body> <div class="father"> <div class="son"></div> </div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>绝对定位-父级有定位-一级父亲</title> <style> .father { position: relative; width: 500px; height: 500px; background-color: skyblue; } .son { position: absolute;
left: 0; bottom: 0; width: 200px; height: 200px; background-color: pink; } </style> </head>
<body> <div class="father"> <div class="son"></div> </div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>绝对定位-父级有定位-多级父亲</title> <style> .yeye { position: relative; width: 800px; height: 800px; background-color: hotpink; padding: 50px; }
.father { width: 500px; height: 500px; background-color: skyblue; }
.son { position: absolute; left: 30px; bottom: 10px; width: 200px; height: 200px; background-color: pink; } </style> </head>
<body> <div class="yeye"> <div class="father"> <div class="son"></div> </div> </div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>绝对定位-脱标</title> <style> .father { position: relative; width: 500px; height: 500px; background-color: skyblue; }
.son { position: absolute; left: 0; bottom: 0; width: 200px; height: 200px; background-color: pink; }
.son2 { width: 200px; height: 200px; background-color: gray; } </style> </head>
<body> <div class="father"> <div class="son"></div> <div class="son2"></div> </div>
</body>
</html>
|
问题:
- 绝对定位和相对定位到底有什么使用场景呢?
- 为什么说相对定位给绝对定位当爹的呢?
1.6 子绝父相的由来
弄清楚这个口诀,就明白了绝对定位和相对定位的使用场景。
这个 “子绝父相” 太重要了,是我们学习定位的口诀,是定位中最常用的一种方式这句话的意思是:子级是绝对定位的话,父级要用相对定位。
- 子级绝对定位,不会占有位置,可以放到父盒子里面的任何一个地方,不会影响其他的兄弟盒子
- 父盒子需要加定位限制子盒子在父盒子内显示
- 父盒子布局时,需要占有位置,因此父亲只能是相对定位
这就是子绝父相的由来,所以相对定位经常用来作为绝对定位的父级。
总结: 因为父级需要占有位置,因此是相对定位, 子盒子不需要占有位置,则是绝对定位。
当然,子绝父相不是永远不变的,如果父元素不需要占有位置,“子绝父绝” 也会遇到。
思考:为什么非要用定位?浮动不可以吗?
答案:用浮动做某些布局远远没有定位简单和方便!例如,轮播图。
- 左右两边的图片切换按钮,利用浮动也可以做。但是,假如放置图片的盒子是在切换按钮之前添加的,那么根据浮动元素只能影响后面盒子的特性,切换按钮就只可能在图片底部之下,不可能浮于图片之上!
- 就算切换按钮用浮动实现了,但是左下角的轮播序号点图如果也用浮动实现,结果就是轮播序号点图会与切换按钮在一行并排浮动!
可见,浮动单纯用于左右排列盒子是非常适合的,但是用于空间层次上排列盒子就不适合了!应该用定位实现。
重点:竖向上布局找标准流,横向上布局找浮动,空间上布局找定位!
【案例:学成在线 hot new 模块】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <div class="box-bd"> <ul class="clearfix"> <li>
<em> <img src="images/hot.png" alt=""> </em> <img src="images/pic.png" alt=""> <h4> Think PHP 5.0 博客系统实战项目演练 </h4> <div class="info"> <span>高级</span> • 1125人在学习 </div> </li> ... </ul> </div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| .box-bd ul { width: 1225px; } .box-bd ul li { position: relative; float: left; width: 228px; height: 270px; background-color: #fff; margin-right: 15px; margin-bottom: 15px; } .box-bd ul li > img { width: 100%; } .box-bd ul li h4 { margin: 20px 20px 20px 25px; font-size: 14px; color: #050505; font-weight: 400; } .box-bd ul li em { position: absolute; top: 4px; right: -4px; } .box-bd .info { margin: 0 20px 0 25px; font-size: 12px; color: #999; } .box-bd .info span { color: #ff7c2d; }
|
1.7 固定定位 fixed (重要)
固定定位是元素固定于浏览器可视区的位置。
主要使用场景: 可以在浏览器页面滚动时元素的位置不会改变。
语法:
1
| 选择器 { position: fixed; }
|
固定定位的特点(务必记住):
- 以浏览器的可视窗口为参照点移动元素
- 固定定位不再占有原先的位置
- 固定定位也是脱标的,其实固定定位也可以看做是一种特殊的绝对定位。
应用举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>固定定位</title> <style> .dj { position: fixed; top: 100px; left: 200px; } </style> </head>
<body> <div class="dj"> <img src="images/pvp.png" alt=""> </div> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p> <p>请尽情吩咐妲己,主人</p>
</body>
</html>
|
1.8 固定定位小技巧:固定在版心右侧位置
小算法:
- 让固定定位的盒子
left: 50%
,走到浏览器可视区(也可以看做版心) 一半的位置
- 让固定定位的盒子
margin-left: 版心宽度的一半距离
,多走版心宽度的一半位置
就可以让固定定位的盒子贴着版心右侧对齐了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>固定定位小技巧-固定到版心右侧</title> <style> .w { width: 800px; height: 1400px; background-color: pink; margin: 0 auto; }
.fixed { position: fixed; left: 50%; margin-left: 405px; width: 50px; height: 150px; background-color: skyblue; } </style> </head>
<body> <div class="fixed"></div> <div class="w">版心盒子 800像素</div>
</body>
</html>
|
1.9 粘性定位 sticky(了解)
粘性定位可以被认为是相对定位和固定定位的混合。
Sticky 粘性的。
语法:
1
| 选择器 { position: sticky; top: 10px; }
|
粘性定位的特点:
- 以浏览器的可视窗口为参照点移动元素(固定定位特点)
- 粘性定位占有原先的位置(相对定位特点)
- 必须添加 top 、left、right、bottom 其中一个才有效
跟页面滚动搭配使用。 兼容性较差,IE 不支持。
未来开发的趋势,但目前并不常用(目前用 javascript 来实现粘性定位效果)。
应用举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>粘性定位</title> <style> body { height: 3000px; }
.nav { position: sticky; top: 0; width: 800px; height: 50px; background-color: pink; margin: 100px auto; } </style> </head>
<body> <div class="nav">我是导航栏</div> </body>
</html>
|
1.10 定位的总结
定位模式 |
是否脱标 |
移动位置 |
是否常用 |
static 静态定位 |
否 |
不能使用边偏移 |
很少 |
relative 相对定位 |
否(占有位置) |
相对于自身位置移动 |
常用 |
absolute 绝对定位 |
是(不占有位置) |
带有定位的父级 |
常用 |
fixed 固定定位 |
是(不占有位置) |
浏览器可视区 |
常用 |
sticky 粘性定位 |
否(占有位置) |
浏览器可视区 |
当前阶段少 |
- 一定记住,相对定位、固定定位、绝对定位 两个大的特点: 1. 是否占有位置(脱标否) 2. 以谁为基准点移动位置。
- 学习定位重点学会子绝父相。
1.11 定位叠放次序 z-index
在使用定位布局时,可能会出现盒子重叠的情况。此时,可以使用 z-index 来控制盒子的前后次序(z轴)。
语法:
- 数值可以是正整数、负整数或 0,默认是 auto,数值越大,盒子越靠上
- 如果属性值相同,则按照书写顺序,后来居上
- 数字后面不能加单位
- 只有定位的盒子才有 z-index 属性
1.12 定位的拓展
(1)绝对定位的盒子居中
加了绝对定位的盒子不能通过 margin: 0 auto
水平居中,但是可以通过以下计算方法实现水平和垂直居中。
left: 50%;
:让盒子的左侧移动到父级元素的水平中心位置。
margin-left: -0.5widthpx;
:让盒子向左移动自身宽度的一半。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>绝对定位水平垂直居中</title> <style> .box { position: absolute; left: 50%; margin-left: -100px; top: 50%; margin-top: -100px; width: 200px; height: 200px; background-color: pink; } </style> </head>
<body> <div class="box"></div> </body>
</html>
|
(2)定位特殊特性
绝对定位和固定定位也和浮动类似。
- 行内元素添加绝对或者固定定位,可以直接设置高度和宽度。
- 块级元素添加绝对或者固定定位,如果不给宽度或者高度,默认大小是内容的大小。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>定位的特殊特性</title> <style> span { position: absolute; top: 100px; width: 200px; height: 150px; background-color: pink; }
div { position: absolute; background-color: skyblue; } </style> </head>
<body> <span>123</span>
<div>abcd</div> </body>
</html>
|
(3)脱标的盒子不会触发外边距塌陷
浮动元素、绝对定位(固定定位)元素的都不会触发外边距合并的问题。
(4)绝对定位(固定定位)会完全压住盒子
浮动元素不同,只会压住它下面标准流的盒子,但是不会压住下面标准流盒子里面的文字(图片)。
但是绝对定位(固定定位) 会压住下面标准流所有的内容。
浮动之所以不会压住文字,因为浮动产生的目的最初是为了做文字环绕效果的。 文字会围绕浮动元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动产生原来的目的是做文字环绕效果</title> <style> img { float: left; } </style> </head>
<body> 1993年,在古装片《战神传说》中扮演一个武功超群的渔民;同年,主演动作喜剧片《至尊三十六计之偷天换日》,在片中饰演赌术高明的千门高手钱文迪;此外,他还主演了爱情片《天长地久》,在片中塑造了一个风流不羁的江湖浪子形象。 1994年,刘德华投资并主演了剧情片《天与地》,在片中饰演面对恶势力却毫不退缩的禁毒专员张一鹏。1995年,主演赛车励志片《烈火战车》,在片中饰演叛逆、倔强的阿祖,并凭借该片获得第15届香港电影金像奖最佳男主角提名;同年在动作片《大冒险家》中演绎了立仁从小时候父母双亡到长大后进入泰国空军的故事。 1996年,主演黑帮题材的电影《新上海滩》,在片中饰演对冯程程痴情一片的丁力。1997年,担任剧情片《香港制造》的制作人;同年,主演爱情片《天若有情之烽火佳人》,在片中饰演家世显赫的空军少尉刘天伟;12月,与梁家辉联袂主演警匪动作片《黑金》,在片中饰演精明干练、嫉恶如仇的调查局机动组组长方国辉。1998年,主演动作片《龙在江湖》 <img src="images/img.jpg" alt=""> ,饰演重义气的黑帮成员韦吉祥;同年,出演喜剧片《赌侠1999》;此外,他还担任剧情片《去年烟花特别多》的制作人。 1993年,在古装片《战神传说》中扮演一个武功超群的渔民;同年,主演动作喜剧片《至尊三十六计之偷天换日》,在片中饰演赌术高明的千门高手钱文迪;此外,他还主演了爱情片《天长地久》,在片中塑造了一个风流不羁的江湖浪子形象。 1994年,刘德华投资并主演了剧情片《天与地》,在片中饰演面对恶势力却毫不退缩的禁毒专员张一鹏。1995年,主演赛车励志片《烈火战车》,在片中饰演叛逆、倔强的阿祖,并凭借该片获得第15届香港电影金像奖最佳男主角提名;同年在动作片《大冒险家》中演绎了立仁从小时候父母双亡到长大后进入泰国空军的故事。 1996年,主演黑帮题材的电影《新上海滩》,在片中饰演对冯程程痴情一片的丁力。1997年,担任剧情片《香港制造》的制作人;同年,主演爱情片《天若有情之烽火佳人》,在片中饰演家世显赫的空军少尉刘天伟;12月,与梁家辉联袂主演警匪动作片《黑金》,在片中饰演精明干练、嫉恶如仇的调查局机动组组长方国辉。1998年,主演动作片《龙在江湖》,饰演重义气的黑帮成员韦吉祥;同年,出演喜剧片《赌侠1999》;此外,他还担任剧情片《去年烟花特别多》的制作人。 </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>定位会完全压住标准流盒子内容</title> <style> .box { position: absolute; width: 150px; height: 150px; background-color: pink; } </style> </head>
<body> <div class="box"></div> <p>阁下何不同风起,扶摇直上九万里</p> </body>
</html>
|
二、综合案例
【案例:淘宝焦点图布局】
布局分析:
制作:
- 大盒子我们类名为: tb-promo 淘宝广告
- 里面先放一张图片
- 左右两个按钮用链接就好了,左箭头 prev 右箭头 next
- 底侧小圆点 ul 继续做,类名为 promo-nav
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>淘宝轮播图做法</title> <style> * { margin: 0; padding: 0; }
li { list-style: none; }
.tb-promo { position: relative; width: 520px; height: 280px; background-color: pink; margin: 100px auto; }
.tb-promo img { width: 520px; height: 280px; }
.prev, .next { position: absolute; top: 50%; margin-top: -15px; width: 20px; height: 30px; background: rgba(0, 0, 0, .3); text-align: center; line-height: 30px; color: #fff; text-decoration: none; }
.prev { left: 0; border-top-right-radius: 15px; border-bottom-right-radius: 15px; }
.next {
right: 0; border-top-left-radius: 15px; border-bottom-left-radius: 15px; }
.promo-nav { position: absolute; bottom: 15px; left: 50%; margin-left: -35px; width: 70px; height: 13px; background: rgba(255, 255, 255, .3); border-radius: 7px; }
.promo-nav li { float: left; width: 8px; height: 8px; background-color: #fff; border-radius: 50%; margin: 3px; }
.promo-nav .selected { background-color: #ff5000; } </style> </head>
<body> <div class="tb-promo"> <img src="images/tb.jpg" alt=""> <a href="#" class="prev"> < </a> <a href="#" class="next"> > </a> <ul class="promo-nav"> <li class="selected"></li> <li></li> <li></li> <li></li> <li></li> </ul> </div> </body>
</html>
|
三、网页布局总结
通过盒子模型,清楚知道大部分 html 标签是一个盒子。
通过 CSS 浮动、定位可以让每个盒子排列成为网页。
一个完整的网页,是标准流、浮动、定位一起完成布局的,每个都有自己的专门用法。
- 标准流
可以让盒子上下排列或者左右排列,垂直的块级盒子显示就用标准流布局。
- 浮动
可以让多个块级元素一行显示或者左右对齐盒子,多个块级盒子水平显示就用浮动布局。
- 定位
定位最大的特点是有层叠的概念,就是可以让多个盒子前后叠压来显示。如果元素自由在某个盒子内移动就用定位布局。
重点:竖向上布局找标准流,横向上布局找浮动,空间上布局找定位!
四、元素的显示与隐藏
类似网站广告,当我们点击关闭就不见了,但是我们重新刷新页面,会重新出现!
本质:让一个元素在页面中隐藏或者显示出来。
注意:是隐藏,不是删除!
- display 显示隐藏(脱标)
- visibility 显示隐藏(不脱标)
- overflow 溢出显示隐藏
4.1 display 属性
display 属性用于设置一个元素应如何显示。
display: none
:隐藏对象
display:block
:除了转换为块级元素之外,同时还有显示元素的意思
display 隐藏元素后,不再占有原来的位置(脱标)。
后面应用及其广泛,搭配 JS 可以做很多的网页特效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之display</title> <style> .peppa { display: none; width: 200px; height: 200px; background-color: pink;
}
.george { width: 200px; height: 200px; background-color: skyblue; } </style> </head>
<body> <div class="peppa">佩奇</div> <div class="george">乔治</div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之display</title> <style> .peppa { display: block; width: 200px; height: 200px; background-color: pink;
}
.george { width: 200px; height: 200px; background-color: skyblue; } </style> </head>
<body> <div class="peppa">佩奇</div> <div class="george">乔治</div> </body>
</html>
|
4.2 visibility 可见性
visibility 属性用于指定一个元素应可见还是隐藏。
visibility:visible
:元素可视
visibility:hidden
:元素隐藏
visibility 隐藏元素后,继续占有原来的位置。
如果隐藏元素想要原来位置, 就用 visibility:hidden。
如果隐藏元素不想要原来位置, 就用 display:none(用处更多,重点)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之display</title> <style> .baba { visibility: hidden; width: 200px; height: 200px; background-color: pink;
}
.mama { width: 200px; height: 200px; background-color: skyblue; } </style> </head>
<body> <div class="baba">猪爸爸</div> <div class="mama">猪妈妈</div> </body>
</html>
|
4.3 overflow 溢出
overflow 属性指定了如果内容溢出一个元素的框(超过其指定高度及宽度)时,会发生什么。
属性值 |
描述 |
visible |
不剪切内容也不添加滚动条(默认方式) |
hidden |
不显示超过对象尺寸的内容,超出的部分隐藏掉(并非删除) |
scroll |
不管超出的内容否,总是显示滚动条 |
auto |
超出自动显示滚动条,不超出不显示滚动条 |
一般情况下,我们都不想让溢出的内容显示出来,因为溢出的部分会影响布局。
但是如果有定位的盒子, 请慎用 overflow: hidden 因为它会隐藏多余的部分(例如:学成在线 hot new 模块,右上角有故意超出的部分,此时就不能使用 overflow: hidden)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、 导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。 故事围绕小猪佩奇与家人的愉快经历,幽默而有趣, 藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: visible; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、 导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。 故事围绕小猪佩奇与家人的愉快经历,幽默而有趣, 藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: hidden; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、 导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。 故事围绕小猪佩奇与家人的愉快经历,幽默而有趣, 藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: scroll; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、 导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。 故事围绕小猪佩奇与家人的愉快经历,幽默而有趣, 藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: scroll; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: auto; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、 导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。 故事围绕小猪佩奇与家人的愉快经历,幽默而有趣, 藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。 </div>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>显示隐藏元素之overflow</title> <style> .peppa { overflow: auto; width: 200px; height: 200px; border: 3px solid pink; margin: 100px auto; } </style> </head>
<body> <div class="peppa"> 小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa Pig》 </div>
</body>
</html>
|
4.4 总结
- display 显示隐藏元素 但是不保留位置
- visibility 显示隐藏元素 但是保留原来的位置
- overflow 溢出显示隐藏 但是只是对于溢出的部分处理
五、综合案例
【案例:土豆网鼠标经过显示遮罩】
- 练习元素的显示与隐藏
- 练习元素的定位
核心原理:原先半透明的黑色遮罩看不见, 鼠标经过大盒子,就显示出来。
遮罩的盒子不占有位置,就需要用绝对定位 和 display 配合使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>仿土豆网显示隐藏遮罩案例</title> <style> .tudou { position: relative; width: 444px; height: 320px; background-color: pink; margin: 30px auto; }
.tudou img { width: 100%; height: 100%; }
.mask { display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, .4) url(images/arr.png) no-repeat center; }
.tudou:hover .mask { display: block; } </style> </head>
<body> <div class="tudou"> <div class="mask"></div> <img src="images/tudou.jpg" alt=""> </div> </body>
</html>
|
【CSS精灵图、字体图标、三角、鼠标样式、用户界面样式、溢出省略号】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、精灵图
1.1 为什么需要精灵图?
一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器就会频繁地接收和发送
请求图片,造成服务器请求压力过大,这将大大降低页面的加载速度。
因此,为了有效地减少服务器接收和发送请求的次数,提高页面的加载速度,出现了 CSS 精灵技术(也称
CSS Sprites、CSS 雪碧)。
核心原理:将网页中的一些小背景图像整合到一张大图中 ,这样服务器只需要一次请求就可以了。
精灵技术目的:为了有效地减少服务器接收和发送请求的次数,提高页面的加载速度。
1.2 精灵图(sprites)的使用
使用精灵图核心:
- 精灵技术主要针对于背景图片使用。就是把多个小背景图片整合到一张大图片中
- 这个大图片也称为 sprites 精灵图 或者 雪碧图
- 移动背景图片位置以控制显示区域, 此时可以使用
background-position
- 移动的距离就是这个目标图片的
x
和 y
坐标。注意网页中的坐标有所不同
- 因为一般情况下都是将精灵图往上往左移动,所以两个坐标数值基本是负值
- 使用精灵图的时候需要精确测量,每个小背景图片的大小和位置
使用精灵图核心总结:
- 精灵图主要针对于小的背景图片使用
- 主要借助于背景位置来实现
background-position
- 一般情况下精灵图都是负值(千万注意网页中的坐标: x轴右边走是正值,左边走是负值, y轴同理)
【王者荣耀案例】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>精灵图使用</title> <style> .box1 { width: 60px; height: 60px; margin: 100px auto; background: url(images/sprites.png) no-repeat -182px 0;
}
.box2 { width: 27px; height: 25px; margin: 200px; background: url(images/sprites.png) no-repeat -155px -106px;
} </style> </head>
<body> <div class="box1"></div> <div class="box2"></div> </body>
</html>
|
1.3 案例:拼单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>利用精灵图拼出自己名字</title> <style> span { display: inline-block; background: url(images/abcd.jpg) no-repeat; }
.p { width: 100px; height: 112px; background-position: -493px -276px; }
.i { width: 60px; height: 108px; background-position: -327px -142px; }
.n { width: 115px; height: 112px; background-position: -255px -275px; }
.k { width: 105px; height: 114px; background-position: -495px -142px; } </style> </head>
<body> <span class="p">p</span> <span class="i">i</span> <span class="n">n</span> <span class="k">k</span> </body>
</html>
|
【PS 切片工具的使用】
二、字体图标
2.1 字体图标的产生
字体图标使用场景:主要用于显示网页中通用、常用的一些小图标。
精灵图是有诸多优点的,但是缺点很明显。
- 图片文件还是比较大的
- 图片本身放大和缩小会失真
- 一旦图片制作完毕想要更换非常复杂
此时,有一种技术的出现很好的解决了以上问题,就是字体图标 iconfont。
字体图标可以为前端工程师提供一种方便高效的图标使用方式,展示的是图标,但本质却属于字体。
2.2 字体图标的优点
- 轻量级:一个图标字体要比一系列的图像要小。一旦字体加载了,图标就会马上渲染出来,减少了服务器请求
- 灵活性:本质其实是文字,可以很随意的改变颜色、产生阴影、透明效果、旋转等
- 兼容性:几乎支持所有的浏览器,请放心使用
注意: 字体图标不能替代精灵技术,只是对工作中图标部分技术的提升和优化。
总结:
- 如果遇到一些结构和样式比较简单的小图标,就用字体图标
- 如果遇到一些结构和样式复杂一点的小图片,就用精灵图
字体图标是一些网页常见的小图标,我们直接网上下载即可。 因此使用可以分为:
- 字体图标的下载
- 字体图标的引入(引入到我们 html 页面中)
- 字体图标的追加(在原有的基础上添加新的小图标)
2.3 字体图标的下载
推荐下载网站:
IcoMoon 成立于 2011 年,推出了第一个自定义图标字体生成器,它允许用户选择所需要的图标,使它们成一字型。该字库内容种类繁多,非常全面,唯一的遗憾是国外服务器,打开网速较慢。
这个是阿里妈妈 M2UX 的一个 iconfont 字体图标字库,包含了淘宝图标库和阿里妈妈图标库。可以使用 AI 制作图标上传生成。 重点是,免费!
以下内容以 icomoon 字库 为例。
2.4 字体图标的引入
下载完毕之后,注意原先的文件不要删,后面会用!
- 把下载包里面的 fonts 文件夹放入页面根目录下
不同浏览器所支持的字体格式是不一样的,字体图标之所以兼容,就是因为包含了主流浏览器支持的字体文件。
- TureType (.ttf) 格式 .ttf 字体是 Windows 和 Mac 的最常见的字体,支持这种字体的浏览器有 IE9+、Firefox3.5+、Chrome4+、Safari3+、Opera10+、iOS Mobile、Safari4.2+;
- Web Open Font Format (.woff) 格式 woff 字体,支持这种字体的浏览器有 IE9+、Firefox3.5+、Chrome6+、Safari3.6+、Opera11.1+;
- Embedded Open Type (.eot) 格式 .eot 字体是 IE 专用字体,支持这种字体的浏览器有 IE4+;
- SVG (.svg) 格式 .svg 字体是基于 SVG 字体渲染的一种格式,支持这种字体的浏览器有 Chrome4+、Safari3.1+、Opera10.0+、iOS Mobile Safari3.2+;
- 在 CSS 样式中全局声明字体:简单理解把这些字体文件通过 css 引入到我们页面中
一定注意字体文件路径的问题。
1 2 3 4 5 6 7 8 9 10
| @font-face { font-family: 'icomoon'; src: url('fonts/icomoon.eot?7kkyc2'); src: url('fonts/icomoon.eot?7kkyc2#iefix') format('embedded-opentype'), url('fonts/icomoon.ttf?7kkyc2') format('truetype'), url('fonts/icomoon.woff?7kkyc2') format('woff'), url('fonts/icomoon.svg?7kkyc2#icomoon') format('svg'); font-weight: normal; font-style: normal; }
|
- html 标签内添加小图标
复制小图标对应的字符(一个小方框)到 html 中,一般建议放在 <span></span>
标签里。
- 给标签定义字体
1 2 3
| span { font-family: "icomoon"; }
|
注意:务必保证这个字体和上面 @font-face 里面的字体保持一致(默认为:icomoon)。
2.5 字体图标的追加
如果工作中,原来的字体图标不够用了,我们便需要添加新的字体图标到原来的字体文件中。
选择 Import Icons 按钮,把原压缩包里面的 selection.json 重新上传,然后选中自己想要新的图标,从新下载压缩包,并替换原来的文件即可。
2.6 字体图标加载的原理
服务器只需接受一次浏览器请求便可以将 fonts 文件一次性返回,如此而来网页中所有用到 fonts 字体图标的部分便一次性加载好了,大大减轻了服务器压力。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>字体图标的使用</title> <style> @font-face { font-family: 'icomoon'; src: url('fonts/icomoon.eot?p4ssmb'); src: url('fonts/icomoon.eot?p4ssmb#iefix') format('embedded-opentype'), url('fonts/icomoon.ttf?p4ssmb') format('truetype'), url('fonts/icomoon.woff?p4ssmb') format('woff'), url('fonts/icomoon.svg?p4ssmb#icomoon') format('svg'); font-weight: normal; font-style: normal; font-display: block; }
span { font-family: 'icomoon'; font-size: 100px; color: salmon; } </style> </head>
<body> <span></span> <span></span> </body>
</html>
|
三、CSS三角
网页中常见一些三角形,使用 CSS 直接画出来就可以,不必做成图片或者字体图标。
CSS 三角是怎么来的?原理如下:
对一个没有大小的盒子设置边框,那么只要边框足够粗,就可以呈现三角效果。
如果只需要一个三角,那么对其他三个边框设置透明色即可。
通常 CSS 三角要配合定位来布局。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS 三角制作</title> <style> .box1 { width: 0; height: 0; border-top: 30px solid hotpink; border-right: 30px solid black; border-bottom: 30px solid skyblue; border-left: 30px solid gray; }
.box2 { width: 0; height: 0; border: 50px solid transparent; border-left-color: black; margin: 50px; }
.jd { position: relative; width: 120px; height: 249px; background-color: black; }
.jd span { position: absolute; right: 15px; top: -20px; width: 0; height: 0; line-height: 0; font-size: 0; border: 10px solid transparent; border-bottom-color: black; } </style> </head>
<body> <div class="box1"></div> <div class="box2"></div> <div class="jd"> <span></span> </div> </body>
</html>
|
四、CSS用户界面样式
4.1 什么是界面样式
所谓的界面样式,就是更改一些用户操作样式,以提高更好的用户体验。
4.2 鼠标样式 cursor
设置或检索在对象上移动的鼠标指针采用何种系统预定义的光标形状。
属性值 |
描述 |
default |
默认箭头 |
pointer |
小手 |
move |
十字移动 |
text |
文本竖杠 |
not-allowed |
禁止 |
注意:除了以上类型,还有其他很多类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>用户界面样式-鼠标样式</title> </head>
<body> <ul> <li style="cursor: default;">我是默认的小白鼠标样式</li> <li style="cursor: pointer;">我是鼠标小手样式</li> <li style="cursor: move;">我是鼠标移动样式</li> <li style="cursor: text;">我是鼠标文本样式</li> <li style="cursor: not-allowed;">我是鼠标禁止样式</li> </ul> </body>
</html>
|
4.3 轮廓线 outline
给表单添加 outline: 0;
或者 outline: none;
样式之后,就可以去掉默认的边框。
1
| input { outline: none; }
|
默认样式:
修改后样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <!doctype html> <html lang="en">
<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>轮廓线 outline</title> <style> input { outline: none; } </style> </head>
<body> <input type="text"> </body>
</html>
|
4.4 防止拖拽文本域 resize
实际开发中,我们文本域右下角是不允许拖拽的。(会破坏布局!)
1
| textarea { resize: none; }
|
默认样式:
修改后样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <!doctype html> <html lang="en">
<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>防止拖拽文本域 resize</title> <style> textarea { outline: none; resize: none; } </style> </head>
<body>
<textarea name="" id="" cols="30" rows="10"></textarea> </body>
</html>
|
五、vertical-align 属性应用
CSS 的 vertical-align 属性使用场景:经常用于设置图片或者表单(行内块元素)与文字垂直对齐。
官方解释:用于设置一个元素的垂直对齐方式,但是它只针对于行内元素或者行内块元素有效。
语法:
1
| vertical-align: baseline | top | middle | bottom
|
值 |
描述 |
baseline |
默认。元素放置在父元素的基线上 |
top |
把元素的顶端与行中最高元素的顶端对齐 |
middle |
把此元素放置在父元素的中部 |
bottom |
把元素的顶端与行中最低的元素的顶端对齐 |
5.1 图片、表单和文字对齐
图片、表单都属于行内块元素,默认的 vertical-align 是基线对齐。
此时可以给图片、表单这些行内块元素的 vertical-align 属性设置为 middle 就可以让文字和图片垂直居中对齐了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>利用vertical-align实现图片文字垂直居中对齐</title> <style> img { vertical-align: middle; }
textarea { vertical-align: middle; } </style> </head>
<body> <img src="images/ldh.jpg" alt=""> pink老师是刘德华
<br> <textarea name="" id="" cols="30" rows="10"></textarea> 请您留言 </body>
</html>
|
运用重点:
我们知道,当对盒子设置 line-height: 盒子高度;
时,盒子内的 文字
会垂直居中,其实不只是文字可以垂直居中,盒子内的图片同样也能垂直居中,只不过图片默认是基于基线对齐的,所以要真正实现 垂直居中
需要在图片加上:vertical-align: middle;
5.2 解决图片底部默认空白缝隙问题
图片底侧会有一个空白缝隙,原因是行内块元素会和文字的基线对齐。
主要解决方法有两种:
- 给图片添加
vertical-align: middle | top | bottom
等(推荐)
- 把图片转换为块级元素
display: block;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>图片底侧空白缝隙解决方案</title> <style> div { border: 2px solid black; }
img { vertical-align: middle; } </style> </head>
<body> <div> <img src="images/ldh.jpg" alt=""> </div> </body>
</html>
|
六、溢出的文字省略号显示
6.1 单行文本溢出省略号显示
三个必要条件:
1 2 3 4 5 6
| white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
|
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>单行文本溢出显示省略号</title> <style> div { width: 150px; height: 80px; background-color: pink; margin: 100px auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> </head>
<body> <div> 啥也不说,此处省略一万字 </div> </body>
</html>
|
6.2 多行文本溢出省略号显示
多行文本溢出显示省略号,有较大兼容性问题, 适合于 webkit 浏览器或移动端(移动端大部分是 webkit 内核)。
1 2 3 4 5 6 7 8
| overflow: hidden; text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
|
更推荐让后台人员来做这个效果,因为后台人员可以设置显示多少个字,操作更简单。
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>单行文本溢出显示省略号</title> <style> div { width: 150px; height: 65px; background-color: pink; margin: 100px auto; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; } </style> </head>
<body> <div> 啥也不说,此处省略一万字,啥也不说,此处省略一万字此处省略一万字 </div> </body>
</html>
|
Chrome 浏览器效果:
【布局技巧:margin负值、文字围绕浮动、行内快、CSS三角、CSS初始化】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、margin负值的运用
如何实现以下效果呢?
多个盒子紧挨在一起,当鼠标放在其中一个盒子上时该盒子的边框自动变色。
- 让每个盒子 margin 往左侧移动 -1px 正好压住相邻盒子边框(否则边框会发生叠加 * 2)
- 鼠标经过某个盒子的时候,提高当前盒子的层级即可(如果周围盒子没有定位,则对当前盒子加相对定位(保留位置并显示在其他盒子之上);如果周围有定位,则提高当前盒子的 z-index)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>margin负值的巧妙运用</title> <style> ul li { position: relative; float: left; list-style: none; width: 150px; height: 200px; border: 1px solid red; margin-left: -1px; }
ul li:hover { z-index: 1; border: 1px solid blue; } </style> </head>
<body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </body>
</html>
|
二、文字围绕浮动元素
在制作文字位于图片周围的效果时,可以巧妙运用浮动元素不会压住文字的特性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>文字围绕浮动元素的妙用</title> <style> * { margin: 0; padding: 0; }
.box { width: 300px; height: 70px; background-color: #d4d4d4; margin: 0 auto; padding: 5px; }
.pic { float: left; width: 120px; height: 60px; margin-right: 5px; }
.pic img { width: 100%; } </style> </head> <body> <div class="box"> <div class="pic"> <img src="images/img.png" alt=""> </div> <p>【集锦】热身赛-巴西0-1秘鲁 内马尔替补两人血染赛场</p> </div> </body> </html>
|
三、行内块的巧妙运用
页码在页面中间显示:
- 把这些链接盒子转换为行内块, 之后给父级指定
text-align: center;
- 利用行内块元素中间有缝隙,并且给父级添加
text-align: center;
行内块元素会水平会居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>行内块的巧妙运用</title> <style> * { margin: 0; padding: 0; }
.box { text-align: center; }
.box a { display: inline-block; width: 36px; height: 36px; background-color: #f7f7f7; border: 1px solid #ccc; line-height: 36px; text-decoration: none; color: #333; font-size: 14px; }
.box .prev, .box .next { width: 85px; }
.box .current, .box .elp { background-color: #fff; border: none; }
.box input { height: 36px; width: 45px; border: 1px solid #ccc; outline: none; }
.box button { width: 60px; height: 36px; background-color: #f7f7f7; border: 1px solid #ccc;
} </style> </head> <body> <div class="box"> <a href="#" class="prev"><<上一页</a> <a href="#" class="current">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">5</a> <a href="#">6</a> <a href="#" class="elp">...</a> <a href="#" class="next">>>下一页</a> 到第 <input type="text"> 页 <button>确定</button> </div> </body> </html>
|
四、CSS三角强化
代码:
1 2 3 4 5
| width: 0; height: 0; border-color: transparent red transparent transparent; border-style: solid; border-width: 22px 8px 0 0;
|
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS三角强化的巧妙运用</title> <style> .box1 { width: 0; height: 0;
border-color: transparent red transparent transparent; border-style: solid; border-width: 100px 50px 0 0; }
.price { width: 160px; height: 24px; line-height: 24px; border: 1px solid red; margin: 0 auto; }
.miaosha { position: relative; float: left; width: 90px; height: 100%; background-color: red; text-align: center; color: #fff; font-weight: 700; margin-right: 8px;
}
.miaosha i { position: absolute; right: 0; top: 0; width: 0; height: 0; border-color: transparent #fff transparent transparent; border-style: solid; border-width: 24px 10px 0 0; }
.origin { font-size: 12px; color: gray; text-decoration: line-through; } </style> </head> <body> <div class="box1"></div> <div class="price"> <span class="miaosha"> ¥1650 <i></i> </span> <span class="origin">¥5650</span> </div> </body> </html>
|
五、CSS初始化
不同浏览器对有些标签的默认值是不同的,为了消除不同浏览器对 HTML 文本呈现的差异,照顾浏览器的兼容,我们需要对 CSS 初始化。
简单理解:CSS 初始化是指重设浏览器的样式。(也称为 CSS reset)
每个网页都必须首先进行 CSS 初始化。
这里我们以 京东 CSS 初始化代码为例。
Unicode 编码字体:
把中文字体的名称用相应的 Unicode 编码来代替,这样就可以有效的避免浏览器解释 CSS 代码时候出现乱码的问题。
比如:
黑体 \9ED1\4F53
宋体 \5B8B\4F53
微软雅黑 \5FAE\8F6F\96C5\9ED1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| * { margin: 0; padding: 0 }
em, i { font-style: normal }
li { list-style: none }
img { border: 0; vertical-align: middle }
button { cursor: pointer }
a { color: #666; text-decoration: none }
a:hover { color: #c81623 }
button, input { font-family: Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53", sans-serif }
body { -webkit-font-smoothing: antialiased; background-color: #fff; font: 12px/1.5 Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53", sans-serif; color: #666 }
.hide, .none { display: none }
.clearfix:after { visibility: hidden; clear: both; display: block; content: "."; height: 0 }
.clearfix { *zoom: 1 }
|
【HTML5&CSS3提高】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、HTML5的新特性
HTML5 的新增特性主要是针对于以前的不足,增加了一些新的标签、新的表单和新的表单属性等。
这些新特性都有兼容性问题,基本是 IE9+ 以上版本的浏览器才支持,如果不考虑兼容性问题(例如:移动端),便可以大量使用这些新特性。
声明:新特性增加了很多,但是我们只需专注于开发常用的新特性即可
1.1 HTML5新增的语义化标签
以前布局,我们基本用 div 来做。div 对于搜索引擎来说,是没有语义的。
1 2 3 4
| <div class=“header”> </div> <div class=“nav”> </div> <div class=“content”> </div> <div class=“footer”> </div>
|
<header>
:头部标签
<nav>
:导航标签
<article>
:内容标签
<section>
:定义文档的某个区域
<aside>
:侧边栏标签
<footer>
:尾部标签
注意:
- 这种语义化标准主要是针对搜索引擎的
- 这些新标签页面中可以使用多次
- 在 IE9 中,需要把这些元素转换为块级元素
- 其实,我们移动端更喜欢使用这些标签
- HTML5 还增加了很多其他标签,我们后面再慢慢学
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增语义化标签</title> <style> header, nav { height: 120px; background-color: pink; border-radius: 15px; width: 800px; margin: 15px auto; }
section { width: 500px; height: 300px; background-color: skyblue; } </style> </head> <body> <header>头部标签</header> <nav>导航栏标签</nav> <section>某个区域</section> </body> </html>
|
1.2 HTML5新增的多媒体标签
新增的多媒体标签主要包含两个:
- 音频:
<audio>
- 视频:
<video>
使用它们可以很方便的在页面中嵌入音频和视频,而不再去使用 flash 和其他浏览器插件。
1.2.1 视频
HTML5 在不使用插件的情况下,也可以原生的支持视频格式文件的播放。当然,支持的格式是有限的。
当前 <video>
元素支持三种视频格式:尽量使用 mp4 格式。
语法:
1
| <video src="文件地址" controls="controls"></video>
|
1 2 3 4 5
| <video controls="controls" width="300"> <source src="move.ogg" type="video/ogg"> <source src="move.mp4" type="video/mp4"> 您的浏览器暂不支持 <video> 标签播放视频 </ video >
|
常见属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增视频标签</title> <style> video { width: 100%; } </style> </head> <body> <video src="media/mi.mp4" autoplay="autoplay" muted="muted" loop="loop" poster="media/mi9.jpg"></video> </body> </html>
|
以上视频会自动播放。
1.2.2 音频
HTML5 在不使用插件的情况下,也可以原生的支持音频格式文件的播放。当然,支持的格式是有限的。
当前 <audio>
元素支持三种音频格式:尽量使用 mp3 格式。
语法:
1
| <audio src="文件地址" controls="controls"></audio>
|
1 2 3 4 5
| <audio controls="controls"> <source src="happy.mp3" type="audio/mpeg"> <source src="happy.ogg" type="audio/ogg"> 您的浏览器暂不支持 <audio> 标签。 </audio>
|
常见属性:
1 2 3 4 5 6 7 8 9 10 11 12
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增音频标签</title> </head> <body> <audio src="media/music.mp3" autoplay="autoplay" controls="controls"></audio> </body> </html>
|
1.2.3 多媒体标签总结
- 音频标签和视频标签使用方式基本一致
- 浏览器支持情况不同
- 谷歌浏览器把音频和视频自动播放禁止了
- 我们可以给视频标签添加 muted 属性来静音播放视频,音频不可以(可以通过 JavaScript 解决)
- 视频标签是重点,我们经常设置自动播放,不使用 controls 控件,循环和设置大小属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head>
<body>
<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="number"/></li> <li>手机号码: <input type="tel"/></li> <li>搜索: <input type="search"/></li> <li>颜色: <input type="color"/></li> <li><input type="submit" value="提交"></li> </ul> </form> </body>
</html>
|
注意:HTML5 所提供的 input 类型可以依据具体的系统环境适配界面样式。
当为数值框时,输入法自动打开数字键盘!
1.4 HTML5新增的表单属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增表单属性</title> <style> input::placeholder { color: hotpink; } </style> </head> <body> <form action=""> <input type="search" name="sear" id="one" required="required" placeholder="pink老师" autofocus="autofocus" autocomplete="off"> <input type="file" name="" id="two" multiple="multiple"> <input type="submit" value="提交"> </form>
</body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增表单属性</title> <style> input::placeholder { color: hotpink; } </style> </head>
<body> <form action=""> <input type="search" name="sear" id="one" required="required" placeholder="pink老师" autofocus="autofocus" autocomplete="on"> <input type="submit" value="提交"> </form>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML5新增表单属性</title> <style> input::placeholder { color: hotpink; } </style> </head>
<body> <form action=""> <input type="search" name="sear" id="one" required="required" placeholder="pink老师" autofocus="autofocus" autofocus="autofocus"> <input type="submit" value="提交"> </form>
</body>
</html>
|
自动聚焦:
二、CSS3的新特性
2.1 CSS3的现状
- 新增的 CSS3 特性有兼容性问题,IE9+ 才支持
- 移动端支持优于 PC 端
- 不断改进中
- 应用相对广泛
- 现阶段主要学习:新增选择器、盒子模型 以及 其他特性
2.2 CSS3新增选择器
CSS3 给我们新增了选择器,可以更加便捷,更加自由的选择目标元素。
- 属性选择器
- 结构伪类选择器
- 伪元素选择器
2.2.1 属性选择器
属性选择器可以根据元素特定属性来选择元素。这样就可以不用借助于类或者 id 选择器。
选择符 |
简介 |
E[att] |
选择具有 att 属性的 E 元素 |
E[att="val"] |
选择具有 att 属性且属性值等于 val 的 E 元素 |
E[att^="val"] |
匹配具有 att 属性且值以 val 开头的 E 元素 |
E[arr$="val"] |
匹配具有 att 属性且值以 val 结尾的 E 元素 |
E[att*="val"] |
匹配具有 att 属性且值中含有 val 的 E 元素 |
注意:类选择器、属性选择器、伪类选择器,权重为 10。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3新增属性选择器</title> <style>
input[type=text] { color: hotpink; }
div[class^=icon] { color: red; }
section[class$=data] { color: blue; }
div.icon1 { color: skyblue; } </style> </head> <body>
<input type="text" name="" id="one"> <input type="password" name="" id="two">
<div class="icon1">小图标1</div> <div class="icon2">小图标2</div> <div class="icon3">小图标3</div> <div class="icon4">小图标4</div> <div>我是打酱油的</div>
<section class="icon1-data">我是安其拉</section> <section class="icon2-data">我是哥斯拉</section> <section class="icon3-ico">哪我是谁</section>
</body> </html>
|
2.2.2 结构伪类选择器
nth-child(n)
选择某个父元素的一个或多个特定的子元素(重点)。
- n 可以是数字,关键字和公式
- n 如果是数字,就是选择第 n 个子元素,里面数字从 1 开始……
- n 可以是关键字:even 偶数,odd 奇数
- n 可以是公式:常见的公式如下(如果 n 是公式,则从 n = 0 开始计算,但是第 0 个元素和超出了元素的个数会被忽略)
公式 |
取值 |
2n |
偶数(2、4、6、……) |
2n+1 |
奇数(1、3、5、……) |
5n |
5 10 15… |
n+5 |
从第 5 个开始(包含第 5 个)到最后 |
-n+5 |
前 5 个(包含第 5 个) |
结构伪类选择器主要根据文档结构来选择元素,常用于根据父级来选择其子元素。
选择器 |
简介 |
E:first-child |
匹配父元素中的第一个子元素 E |
E:last-child |
匹配父元素中最后一个 E 元素 |
E:nth-child(n) |
匹配父元素中的第 n 个子元素 E |
E:first-of-type |
指定类型 E 的第一个 |
E:last-of-type |
指定类型 E 的最后一个 |
E:nth-of-type(n) |
指定类型 E 的第 n 个 |
区别:
- nth-child 对父元素里面所有孩子排序选择(序号是固定的) 先找到第 n 个孩子,然后看看是否和 E 匹配
- nth-of-type 对父元素里面指定子元素进行排序选择。 先去匹配 E,然后再根据 E 找第 n 个孩子
小结:
- 结构伪类选择器一般用于选择父级里面的第几个孩子
- nth-child 对父元素里面所有孩子排序选择(序号是固定的) 先找到第 n 个孩子,然后看看是否和 E 匹配
- nth-of-type 对父元素里面指定子元素进行排序选择。 先去匹配 E,然后再根据 E 找第 n 个孩子
- 若父元素内都是同一种标签(如:列表),优先用 nth-child,否则就只能使用 nth-of-type
- 类选择器、属性选择器、伪类选择器,权重为 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3新增结构伪类选择器</title> <style> ul li:first-child { background-color: pink; }
ul li:last-child { background-color: pink; }
ul li:nth-child(2) { background-color: skyblue; }
ul li:nth-child(6) { background-color: skyblue; } </style> </head> <body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3新增结构伪类选择器-nth-child</title> <style> ul li:nth-child(even) { background-color: #ccc; }
ul li:nth-child(odd) { background-color: gray; }
ol li:nth-child(-n+3) { background-color: pink; } </style> </head>
<body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul> <ol> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ol> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3新增选择器nth-type-of</title> <style> ul li:first-of-type { background-color: pink; }
ul li:last-of-type { background-color: pink; }
ul li:nth-of-type(even) { background-color: skyblue; }
section div:nth-child(1) { background-color: red; }
section div:nth-of-type(1) { background-color: blue; } </style> </head>
<body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul>
<section> <p>光头强</p> <div>熊大</div> <div>熊二</div> </section> </body>
</html>
|
2.2.3 伪元素选择器(重点)
伪元素选择器可以帮助我们利用 CSS 创建新标签元素,而不需要 HTML 标签,从而简化 HTML 结构。
选择器 |
简介 |
::before |
在元素内容的前面插入内容 |
::after |
在元素内容的后面插入内容 |
注意:
- before 和 after 创建一个元素,属于行内元素
- 新创建的这个元素在文档树中是找不到的,所以我们称为伪元素
- 语法:
element::before{}
- before 和 after 必须有 content 属性
- before 在父元素内容的前面创建元素,after 在父元素内容的后面创建元素
- 伪元素选择器和标签选择器一样,权重为 1
(1)伪元素选择器使用场景1:伪元素字体图标
1 2 3 4 5 6 7
| p::before { position: absolute; right: 20px; top: 10px; content: '\e91e'; font-size: 20px; }
|
(2)伪元素选择器使用场景2:仿土豆效果
1 2 3 4 5
| .tudou:hover::before { display: block; }
|
(3)伪元素选择器使用场景3:伪元素清除浮动
- 额外标签法也称为隔墙法,是 W3C 推荐的做法
- 父级添加 overflow 属性
- 父级添加 after 伪元素
- 父级添加双伪元素
额外标签法也称为隔墙法,是 W3C 推荐的做法。
注意:要求这个新的空标签必须是块级元素。
后面两种伪元素清除浮动算是第一种额外标签法的一个升级和优化。
1 2 3 4 5 6 7
| .clearfix:after { content: ""; display: block; height: 0; clear: both; visibility: hidden; }
|
1 2 3 4 5 6 7 8 9
| .clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
|
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>伪元素选择器before和after</title> <style> div { width: 200px; height: 200px; background-color: salmon; }
div::before { content: '我';
}
div::after { content: '小猪佩奇'; } </style> </head> <body> <div> 是 </div> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>伪元素选择器使用场景-字体图标</title> <style> @font-face { font-family: 'icomoon'; src: url('fonts/icomoon.eot?1lv3na'); src: url('fonts/icomoon.eot?1lv3na#iefix') format('embedded-opentype'), url('fonts/icomoon.ttf?1lv3na') format('truetype'), url('fonts/icomoon.woff?1lv3na') format('woff'), url('fonts/icomoon.svg?1lv3na#icomoon') format('svg'); font-weight: normal; font-style: normal; font-display: block; }
div { position: relative; width: 200px; height: 35px; border: 1px solid red; }
div::after { position: absolute; top: 10px; right: 10px; font-family: 'icomoon'; content: '\e91e'; color: red; font-size: 18px; } </style> </head>
<body> <div></div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>伪元素选择器使用场景2-仿土豆网显示隐藏遮罩案例</title> <style> .tudou { position: relative; width: 444px; height: 320px; background-color: pink; margin: 30px auto; }
.tudou img { width: 100%; height: 100%; }
.tudou::before { content: ''; display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, .4) url(images/arr.png) no-repeat center; }
.tudou:hover::before { display: block; } </style> </head>
<body> <div class="tudou"> <img src="images/tudou.jpg" alt=""> </div> </body>
</html>
|
2.3 CSS3盒子模型
CSS3 中可以通过 box-sizing 来指定盒模型,有 2 个值:即可指定为 content-box、border-box,这样我们计算盒子大小的方式就发生了改变。
可以分成两种情况:
- box-sizing: content-box 盒子大小为 width + padding + border (以前默认的)
- box-sizing: border-box 盒子大小为 width
如果盒子模型我们改为了 box-sizing: border-box, 那 padding 和 border 就不会撑大盒子了(前提 padding 和 border 不会超过 width 宽度)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3盒子模型</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; }
div { width: 200px; height: 200px; background-color: pink; border: 20px solid red; padding: 15px; box-sizing: content-box; }
p { width: 200px; height: 200px; background-color: pink; border: 20px solid red; padding: 15px; box-sizing: border-box; } </style> </head> <body> <div> 小猪乔治 </div> <p> 小猪佩奇 </p> </body> </html>
|
2.4 CSS3其他特性(了解)
- 图片变模糊
- 计算盒子宽度 width:calc 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>图片模糊处理filter</title> <style> img { filter: blur(15px); }
img:hover { filter: blur(0); } </style> </head> <body> <img src="images/pink.jpg" alt=""> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3属性calc函数</title> <style> .father { width: 500px; height: 500px; background-color: black; }
.son { width: calc(100% - 100px); height: 200px; background-color: salmon; } </style> </head> <body>
<div class="father"> <div class="son"></div> </div> </body> </html>
|
2.4.1 CSS3滤镜 filter
filter CSS 属性将模糊或颜色偏移等图形效果应用于元素。
1
| filter: 函数(); 例如:filter: blur(5px); blur 模糊处理,数值越大越模糊
|
2.4.2 CSS3 calc 函数
calc() 此 CSS 函数让你在声明 CSS 属性值时执行一些计算。
1
| width: calc(100% - 80px);
|
括号里面可以使用 +
-
*
/
来进行计算。
2.5 CSS3过渡(重点)
2.5.1 过渡
过渡(transition)是 CSS3 中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果。
过渡动画:是从一个状态渐渐的过渡到另外一个状态。
可以让我们页面更好看,更动感十足,虽然低版本浏览器不支持(IE9 以下版本) 但是不会影响页面布局。
我们现在经常和 :hover
一起搭配使用。
语法:
1
| transition: 要过渡的属性 花费时间 运动曲线 何时开始;
|
- 属性:想要变化的 css 属性,宽度高度、背景颜色、内外边距都可以 。如果想要所有的属性都变化过渡,写一个
all
就可以
- 花费时间:单位是秒(必须写单位)比如
0.5s
- 运动曲线:默认是
ease
(可以省略)
- 何时开始:单位是秒(必须写单位)可以设置延迟触发时间默认是
0s
(可以省略)
记住过渡的使用口诀:谁做过渡给谁加!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3过渡效果</title> <style> div { width: 200px; height: 100px; background-color: black; transition: all 0.5s; }
div:hover { width: 400px; height: 200px; background-color: gray; } </style> </head> <body> <div></div> </body> </html>
|
2.5.2 进度条案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3过渡练习-进度条</title> <style> .bar { width: 150px; height: 15px; border: 1px solid red; border-radius: 7px; padding: 1px; }
.bar_in { width: 50%; height: 100%; border-radius: 7px; background-color: red; transition: all .7s; }
.bar:hover .bar_in { width: 100%; } </style> </head> <body> <div class="bar"> <div class="bar_in"></div> </div> </body> </html>
|
三、狭义的 HTML5 CSS3
HTML5 结构本身
CSS3 相关样式
四、广义的 HTML5
- 广义的 HTML5 是 HTML5 + CSS3 + JavaScript
- 这个集合有时称为 H5
- 虽然 HTML5 的一些特性仍然不被某些浏览器支持,但是它是一种发展趋势
【CSS3提高:2D转换、动画、3D转换、浏览器私有前缀】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、CSS3 2D转换
转换(transform)是 CSS3 中具有颠覆性的特征之一。可以实现元素的位移、旋转、缩放等效果。
转换(transform)你可以简单理解为变形。
- 移动:translate
- 旋转:rotate
- 缩放:scale
1.1 二维坐标系
2D 转换是改变标签在二维平面上的位置和形状的一种技术,先来学习二维坐标系。
1.2 2D 转换之移动 translate
2D 移动是 2D 转换里面的一种功能,可以改变元素在页面中的位置,类似定位。
语法:
1 2 3 4
| transform: translate(x, y);
transform: translateX(n); transform: translateY(n);
|
重点:
- 定义 2D 转换中的移动,沿着 X 和 Y 轴移动元素
- translate 最大的优点:不会影响到任何其他元素的位置(优于定位的地方)
- translate 中的百分比单位是相对于自身元素的 translate: (50%, 50%);
- 对行内元素没有效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>2D转换之移动translate</title> <style>
div { width: 200px; height: 200px; background-color: hotpink; }
div:first-child { transform: translate(30px, 30px); }
div:last-child { background-color: black; } </style> </head>
<body> <div></div> <div></div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>让一个盒子水平居中</title> <style> * { margin: 0; padding: 0; } div { position: relative; width: 500px; height: 500px; background-color: hotpink; }
p { position: absolute; top: 50%; left: 50%; width: 200px; height: 200px; background-color: black;
transform: translate(-50%, -50%); }
span { transform: translate(300px, 300px); } </style> </head>
<body> <div> <p></p> </div> <span>123</span> </body>
</html>
|
1.3 2D 转换之旋转 rotate
2D 旋转指的是让元素在 2 维平面内顺时针旋转或者逆时针旋转。
语法:
重点:
- rotate 里面跟度数,单位是 deg,比如 rotate(45deg)
- 角度为正时,顺时针;负时,逆时针
- 默认旋转的中心点是元素的中心点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>2D转换之旋转rotate</title> <style> img { width: 150px; border-radius: 50%; border: 5px solid pink; transition: all 0.5s; }
img:hover { transform: rotate(360deg); } </style> </head>
<body> <img src="media/pic.jpg" alt=""> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>旋转三角</title> <style> div { position: relative; width: 249px; height: 35px; border: 1px solid #000; }
div::after { content: ""; position: absolute; top: 8px; right: 15px; width: 10px; height: 10px; border-right: 1px solid #000; border-bottom: 1px solid #000; transform: rotate(45deg); transition: all 0.2s; }
div:hover::after { transform: rotate(225deg); } </style> </head>
<body> <div></div> </body>
</html>
|
我们可以设置元素转换的中心点。
语法:
重点:
- 注意后面的参数 x 和 y 用空格隔开
- x y 默认转换的中心点是元素的中心点(50% 50%)
- 还可以给 x y 设置 像素 或者 方位名词(top bottom left right center)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>transform-origin</title> <style> div { width: 100px; height: 100px; background-color: pink; margin: 100px auto; transition: all 1s; transform-origin: 25px 25px; }
div:hover { transform: rotate(360deg); } </style> </head>
<body> <div></div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>旋转中心点</title> <style> div { overflow: hidden; width: 200px; height: 200px; border: 1px solid pink; margin: 10px; float: left; }
div::before { content: "黑马"; display: block; width: 100%; height: 100%; background-color: hotpink; transform: rotate(180deg); transform-origin: left bottom; transition: all 0.4s; }
div:hover::before { transform: rotate(0deg); } </style> </head>
<body> <div></div> <div></div> <div></div> </body>
</html>
|
1.5 2D 转换之缩放 scale
缩放,顾名思义,可以放大和缩小。只要给元素添加上了这个属性就能控制它放大还是缩小。
语法:
注意:
- 注意其中的 x 和 y 用逗号分隔
- transform: scale(1, 1) :宽和高都放大一倍,相当于没有放大
- transform: scale(2, 2) :宽和高都放大了 2 倍
- transform: scale(2) :只写一个参数,第二个参数默认等于第一个参数,相当于 scale(2, 2)
- transform: scale(0.5, 0.5) :缩小
- scale 缩放最大的优势:可以设置缩放的基准点(默认以中心点缩放);并且缩放不会影响其他盒子的位置(以上两个特点都是直接设置 width 和 height 都无法做到的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>2D转换之缩放</title> <style> div { width: 200px; height: 200px; background-color: pink; margin: 100px auto; } div:hover {
transform: scale(2); } </style> </head>
<body> <div></div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>图片放大案例</title> <style> div { width: 225px; height: 137px; overflow: hidden; float: left; margin: 10px; }
div img { transition: all .4s; }
div img:hover { transform: scale(1.1); } </style> </head>
<body> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </div> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </div> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </div> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> li { float: left; width: 30px; height: 30px; border: 1px solid hotpink; margin: 10px; text-align: center; line-height: 30px; list-style: none; border-radius: 50%; cursor: pointer; transition: all .4s; }
li:hover { transform: scale(1.2); } </style> </head>
<body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> </ul> </body>
</html>
|
1.6 2D 转换综合写法
注意:
- 同时使用多个转换,其格式为:
transform: translate() rotate() scale()
…等
- 其顺序会影转换的效果。(先旋转会改变坐标轴方向)
- 当我们同时有位移和其他属性的时候,记得要将位移放到最前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { width: 200px; height: 200px; background-color: pink; transition: all 1s; }
div:hover { transform: translate(150px, 50px) rotate(180deg) scale(1.2); } </style> </head>
<body> <div></div> </body>
</html>
|
1.7 2D 转换总结
- 转换 transform 我们简单理解就是变形,有 2D 和 3D 之分
- 我们暂且学了三个,分别是:位移、旋转 和 缩放
- 2D 移动 translate(x, y) 最大的优势是不影响其他盒子,里面参数用 %,是相对于自身宽度和高度来计算的
- 可以分开写比如 translateX(x) 和 translateY(y)
- 2D 旋转 rotate(度数) 可以实现旋转元素,度数的单位是 deg
- 2D 缩放 sacle(x, y) 里面参数是数字,不跟单位,可以是小数。最大的优势在于不影响其他盒子
- 设置转换中心点 transform-origin : x y; 参数可以百分比、像素或者是方位名词
- 当我们进行综合写法,同时有位移和其他属性的时候,记得要将位移放到最前
二、CSS3 动画
动画(animation)是 CSS3 中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果。
相比较过渡,动画可以实现更多变化,更多控制,连续自动播放等效果。
2.1 动画的基本使用
制作动画分为两步:
- 先定义动画
- 再使用(调用)动画
2.1.1 用 keyframes 定义动画(类似定义类选择器)
1 2 3 4 5 6 7 8
| @keyframes 动画名称 { 0% { width: 100px; } 100% { width: 200px; } }
|
动画序列
- 0% 是动画的开始,100% 是动画的完成。这样的规则就是动画序列
- 在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果
- 动画是使元素从一种样式逐渐变化为另一种样式的效果。您可以改变任意多的样式任意多的次数
- 请用百分比来规定变化发生的时间,或用关键词 “from” 和 “to”,等同于 0% 和 100%
2.1.2 元素使用动画
1 2 3 4 5 6 7 8 9 10
| div { width: 200px; height: 200px; background-color: aqua; margin: 100px auto; animation-name: 动画名称; animation-duration: 持续时间; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS3动画的基本使用</title> <style> @keyframes move { 0% { transform: translateX(0px); } 100% { transform: translateX(1000px); } }
div { width: 200px; height: 200px; background-color: pink; animation-name: move; animation-duration: 2s; } </style> </head>
<body> <div></div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>动画序列</title> <style>
@keyframes move { 0% { transform: translate(0, 0); } 25% { transform: translate(1000px, 0) } 50% { transform: translate(1000px, 500px); } 75% { transform: translate(0, 500px); } 100% { transform: translate(0, 0); } }
div { width: 100px; height: 100px; background-color: pink; animation-name: move; animation-duration: 10s; } </style> </head>
<body> <div>
</div> </body>
</html>
|
2.2 动画常用属性
属性 |
描述 |
@keyframes |
规定动画 |
animation |
所有动画属性的简写属性,除了animation-play-state 属性 |
animation-name |
规定 @keyframes 动画的名称(必须的) |
animation-duration |
规定动画完成一个周期所花费的秒或毫秒,默认是 0(必须的) |
animation-timing-function |
规定动画的速度曲线,默认是 “ease” |
animation-delay |
规定动画何时开始,默认是 0 |
animation-iteration-count |
规定动画被播放的次数,默认是 1,还有 infinite |
animation-direction |
规定动画是否在下一周期逆向播放,默认是 “normal”, alternate 逆播放 |
animation-play-state |
规定动画是否正在运行或暂停。默认是 “running”, 还有 “paused” |
animation-fill-mode |
规定动画结束后状态,保持 forwards 回到起始 backwards |
2.3 动画简写属性
animation:动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 动画起始或者结束的状态。
1
| animation: myfirst 5s linear 2s infinite alternate;
|
- 简写属性里面不包含 animation-play-state
- 暂停动画:animation-play-state: puased; 经常和鼠标经过等其他配合使用
- 想要动画走回来,而不是直接跳回来:animation-direction: alternate
- 盒子动画结束后,停在结束位置:animation-fill-mode: forwards
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>动画属性</title> <style> @keyframes move { 0% { transform: translate(0, 0); } 100% { transform: translate(1000px, 0); } }
div { width: 100px; height: 100px; background-color: pink; animation-name: move; animation-delay: 1s; }
div:hover { animation-play-state: paused; } </style> </head>
<body> <div>
</div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>大数据热点图</title> <style> body { background-color: #333; }
.map { position: relative; width: 747px; height: 616px; background: url(media/map.png) no-repeat; margin: 0 auto; }
.city { position: absolute; top: 227px; right: 193px; color: #fff; }
.tb {
top: 500px; right: 80px; }
.dotted { width: 8px; height: 8px; background-color: #09f; border-radius: 50%; }
.city div[class^="pulse"] { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 8px; height: 8px; box-shadow: 0 0 12px #009dfd; border-radius: 50%; animation: pulse 1.2s linear infinite; }
.city div.pulse2 { animation-delay: 0.4s; }
.city div.pulse3 { animation-delay: 0.8s; }
@keyframes pulse { 0% { } 70% { width: 40px; height: 40px; opacity: 1; } 100% { width: 70px; height: 70px; opacity: 0; } } </style> </head>
<body> <div class="map"> <div class="city"> <div class="dotted"></div> <div class="pulse1"></div> <div class="pulse2"></div> <div class="pulse3"></div> </div> <div class="city tb"> <div class="dotted"></div> <div class="pulse1"></div> <div class="pulse2"></div> <div class="pulse3"></div> </div> </div> </body>
</html>
|
2.4 速度曲线细节
animation-timing-function:规定动画的速度曲线,默认是 “ease”。
值 |
描述 |
linear |
动画从头到尾的速度是相同的(匀速) |
ease |
默认。动画以低速开始,然后加快,在结束前变慢 |
ease-in |
动画以低速开始 |
ease-out |
动画以低速结束 |
ease-in-out |
动画以低速开始和结束 |
steps() |
指定了时间函数中的间隔数量(步长) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>速度曲线步长</title> <style> div { overflow: hidden; font-size: 20px; width: 0; height: 30px; background-color: pink; white-space: nowrap; animation: w 4s steps(10) forwards; }
@keyframes w { 0% { width: 0; } 100% { width: 200px; } } </style> </head>
<body> <div>世纪佳缘我在这里等你</div> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>奔跑的熊大案例</title> <style> body { background-color: #ccc; }
div { position: absolute; width: 200px; height: 100px; background: url(media/bear.png) no-repeat; animation: bear .4s steps(8) infinite, move 3s forwards; }
@keyframes bear { 0% { background-position: 0 0; } 100% { background-position: -1600px 0; } }
@keyframes move { 0% { left: 0; } 100% { left: 50%; transform: translateX(-50%); } } </style> </head>
<body> <div></div> </body>
</html>
|
三、CSS3 3D转换
我们生活的环境是 3D 的,照片就是 3D 物体在 2D 平面呈现的例子。
有什么特点
当我们在网页上构建 3D 效果的时候参考这些特点就能产出 3D 效果。
3.1 三维坐标系
三维坐标系其实就是指立体空间,立体空间是由3个轴共同组成的。
- x 轴:水平向右(注意:x 右边是正值,左边是负值)
- y 轴:垂直向下(注意:y 下面是正值,上面是负值)
- z 轴:垂直屏幕(注意:往外面是正值,往里面是负值)
3D 转换我们主要学习工作中最常用的 3D 位移 和 3D 旋转。
主要知识点
- 3D 位移:translate3d(x, y, z)
- 3D 旋转:rotate3d(x, y, z)
- 透视:perspective
- 3D 呈现:transfrom-style
3.2 3D移动 translate3d
3D 移动在 2D 移动的基础上多加了一个可以移动的方向,就是 z 轴方向。
- transform:translateX(100px):仅仅是在 X 轴上移动
- transform:translateY(100px):仅仅是在 Y 轴上移动
- transform:translateZ(100px):仅仅是在 Z 轴上移动(注意:translateZ 一般用 px 单位)
- transform:translate3d(x, y, z):其中 x、y、z 分别指要移动的轴的方向的距离
因为 z 轴是垂直屏幕,由里指向外面,所以默认是看不到元素在 z 轴的方向上移动(要借助透视)。
3.3 透视 perspective
在 2D 平面产生近大远小视觉立体,但是效果只是二维的。
- 如果想要在网页产生 3D 效果需要透视(理解成 3D 物体投影在 2D 平面内)
- 模拟人类的视觉位置,可认为安排一只眼睛去看
- 透视我们也称为视距:视距就是人的眼睛到屏幕的距离
- 距离视觉点越近的,在电脑平面成像越大,越远成像越小
- 透视的单位是像素
透视写在被观察元素的父盒子上面。
d:就是视距,视距就是一个距离人的眼睛到屏幕的距离。
z:就是 z 轴,物体距离屏幕的距离,z 轴越大(正值)我们看到的物体就越大。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>3D移动translate3d</title> <style> body { perspective: 200px; }
div { width: 200px; height: 200px; background-color: pink; transform: translate3d(400px, 100px, 100px); } </style> </head>
<body> <div></div> </body>
</html>
|
3.4 translateZ
translform:translateZ(100px):仅仅是在 Z 轴上移动。有了透视,就能看到 translateZ 引起的变化了。
- translateZ:近大远小
- translateZ:往外是正值
- translateZ:往里是负值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>translateZ</title> <style> body { perspective: 500px; }
div { width: 200px; height: 200px; background-color: pink; margin: 100px auto; transform: translateZ(0); } </style> </head>
<body> <div></div> <div></div> <div></div> </body>
</html>
|
3.5 3D旋转 rotate3d
3D旋转指可以让元素在三维平面内沿着 x轴,y轴,z轴或者自定义轴进行旋转。
语法
- transform: rotateX(45deg):沿着 x 轴正方向旋转 45 度
- transform: rotateY(45deg):沿着 y 轴正方向旋转 45deg
- transform: rotateZ(45deg):沿着 z 轴正方向旋转 45deg
- transform: rotate3d(x, y, z, deg):沿着自定义轴旋转 deg 为角度(了解即可)
对于元素旋转的方向的判断,我们需要先学习一个左手准则。
左手准则
- 左手的手拇指指向 x 轴的正方向
- 其余手指的弯曲方向就是该元素沿着 x 轴旋转的方向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>rotateX</title> <style> body { perspective: 300px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateX(45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
- 左手的手拇指指向 y 轴的正方向
- 其余手指的弯曲方向就是该元素沿着 y 轴旋转的方向(正值)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>rotateY</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateY(45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>rotateZ</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateZ(180deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
transform: rotate3d(x, y, z, deg):沿着自定义轴旋转 deg 为角度(了解即可)。
xyz 是表示旋转轴的矢量,表示你是否希望沿着该轴旋转,最后一个表示旋转的角度。
- transform: rotate3d(1, 0, 0, 45deg):就是沿着 x 轴旋转 45deg
- transform: rotate3d(0, 1, 0, 45deg):就是沿着 y 轴旋转 45deg
- transform: rotate3d(0, 0, 1, 45deg):就是沿着 z 轴旋转 45deg
- transform: rotate3d(1, 1, 0, 45deg):就是沿着对角线(矢量计算)旋转 45deg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>rotate3d</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotate3d(1, 1, 0, 45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
3.6 3D呈现 transfrom-style
- 控制子元素是否开启三维立体环境
- transform-style: flat 子元素不开启 3d 立体空间(默认的)
- transform-style: preserve-3d; 子元素开启立体空间
- 代码写给父级,但是影响的是子盒子
- 这个属性很重要,后面必用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>transform-style</title> <style> body { perspective: 500px; }
.box { position: relative; width: 200px; height: 200px; margin: 100px auto; transition: all 2s; transform-style: preserve-3d; }
.box:hover { transform: rotateY(60deg); }
.box div { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: pink; }
.box div:last-child { background-color: purple; transform: rotateX(60deg); } </style> </head>
<body> <div class="box"> <div></div> <div></div> </div> </body>
</html>
|
【案例:两面翻转的盒子】
实现步骤:
- 搭建 HTML 结构
1 2 3 4
| <div class="box"> <div class="front">黑马程序员</div> <div class="back">pink老师等你</div> </div>
|
- box 父盒子里面包含前后两个子盒子
- box 是翻转的盒子 front 是前面盒子 back 是后面盒子
- CSS 样式
- box 指定大小,切记要添加 3d 呈现
- back 盒子要沿着 Y 轴翻转 180 度
- 最后鼠标经过 box 沿着 Y 旋转 180deg
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>两面翻转的盒子</title> <style> body { perspective: 400px; }
.box { position: relative; width: 300px; height: 300px; margin: 100px auto; transition: all .4s; transform-style: preserve-3d; }
.box:hover { transform: rotateY(180deg); }
.front, .back { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; font-size: 30px; color: #fff; text-align: center; line-height: 300px; }
.front { background-color: pink; z-index: 1; }
.back { background-color: purple; transform: rotateY(180deg); } </style> </head>
<body> <div class="box"> <div class="front">黑马程序员</div> <div class="back">pink老师这里等你</div> </div> </body>
</html>
|
【案例:3D 导航栏】
实现步骤:
- 搭建 HTML 结构
1 2 3 4 5 6 7 8
| <ul> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> </ul>
|
- li 做导航栏
- .box 是翻转的盒子 front 是前面盒子 bottom 是底下盒子
- CSS 样式
- li 设置大小,加透视和 3d 呈现
- front 需要前移 17.5 像素
- bottom 需要下移 17.5 像素并且要沿着 x 轴翻转 负 90 度
- 鼠标放到 box 让盒子旋转 90 度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>3D导航栏案例</title> <style> * { margin: 0; padding: 0; }
ul { margin: 100px; }
ul li { float: left; margin: 0 5px; width: 120px; height: 35px; list-style: none; perspective: 500px; }
.box { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: all .4s; }
.box:hover { transform: rotateX(90deg); }
.front, .bottom { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
.front { background-color: pink; z-index: 1; transform: translateZ(17.5px); }
.bottom { background-color: purple; transform: translateY(17.5px) rotateX(-90deg); } </style> </head>
<body> <ul> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> </ul> </body>
</html>
|
【综合案例:旋转木马】
- 搭建 HTML 结构
1 2 3 4 5 6 7 8
| <section> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section>
|
- 里面的 6 个 div 分别是 6 个狗狗图片
- 注意最终旋转是 section 标签旋转
- CSS 样式
- 给 body 添加 透视效果 perspective: 1000px;
- 给 section 添加大小,一定不要忘记添加 3d 呈现效果控制里面的 6 个 div
- 里面 6 个 div 全部绝对定位叠到一起,然后移动不同角度旋转和距离
- 注意:旋转角度用 rotateY 距离肯定用 translateZ 来控制
- 给 section 添加动画 animation,让它可以自动旋转即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>综合案例:旋转木马</title> <style> body { perspective: 1000px; }
section { position: relative; width: 300px; height: 200px; margin: 150px auto; transform-style: preserve-3d; animation: rotate 10s linear infinite; background: url(media/pig.jpg) no-repeat; }
section:hover { animation-play-state: paused; }
@keyframes rotate { 0% { transform: rotateY(0); } 100% { transform: rotateY(360deg); } }
section div { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: url(media/dog.jpg) no-repeat; }
section div:nth-child(1) { transform: rotateY(0) translateZ(300px); }
section div:nth-child(2) { transform: rotateY(60deg) translateZ(300px); }
section div:nth-child(3) { transform: rotateY(120deg) translateZ(300px); }
section div:nth-child(4) { transform: rotateY(180deg) translateZ(300px); }
section div:nth-child(5) { transform: rotateY(240deg) translateZ(300px); }
section div:nth-child(6) { transform: rotateY(300deg) translateZ(300px); } </style> </head>
<body> <section> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section> </body>
</html>
|
四、浏览器私有前缀
浏览器私有前缀是为了兼容老版本的写法,比较新版本的浏览器无须添加。
4.1 私有前缀
- -moz-:代表 firefox 浏览器私有属性
- -ms-:代表 ie 浏览器私有属性
- -webkit-:代表 safari、chrome 私有属性
- -o-:代表 Opera 私有属性
4.2 提倡的写法
1 2 3 4
| -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; border-radius: 10px;
|