参考链接: juejin.im/post/5e8d52…
animation、transition、transform、translate 这几个属性要搞清楚:
水平居中:
垂直居中
BFC 是块级格式上下文,IFC 是行内格式上下文:
不会,因为 BFC 是页面中一个独立的隔离容器,其内部的元素不会与外部的元素相互影响,比如两个 div,上面的 div 设置了 float,那么如果下面的元素不是 BFC,也没有设置 float,会形成对上面的元素进行包裹内容的情况,如果设置了下面元素为 overflow:hidden;属性那么就能够实现经典的两列布局,左边内容固定宽度,右边因为是 BFC 所以会进行自适应。
box-sizing 属性可以被用来调整这些表现:
BFC(Block Formatting Context)格式化上下文,是 Web 页面中盒模型布局的 CSS 渲染模式,指一个独立的渲染区域或者说是一个隔离的独立容器。
五种:
BFC 的特性:
CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括: 外边距(margin) 、 边框(border) 、 内边距(padding) 、 实际内容(content) 四个属性。 CSS盒模型: 标准模型 + IE模型
标准盒子模型:宽度=内容的宽度(content)+ border + padding
低版本IE盒子模型:宽度=内容宽度(content+border+padding),如何设置成 IE 盒子模型:
static:无特殊定位,对象遵循正常文档流。top,right,bottom,left等属性不会被应用。 relative:对象遵循正常文档流,但将依据top,right,bottom,left等属性在正常文档流中偏移位置。而其层叠通过z-index属性定义。 absolute:对象脱离正常文档流,使用top,right,bottom,left等属性进行绝对定位。而其层叠通过z-index属性定义。 fixed:对象脱离正常文档流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。而其层叠通过z-index属性定义。 sticky:具体是类似 relative 和 fixed,在 viewport 视口滚动到阈值之前应用 relative,滚动到阈值之后应用 fixed 布局,由 top 决定。
问:为什么会有这种现象?你能解释一下吗
是由块级格式上下文决定的,BFC,元素在 BFC 中会进行上下排列,然后垂直距离由 margin 决定,并且会发生重叠,具体表现为同正取最大的,同负取绝对值最大的,一正一负,相加
BFC 是页面中一个独立的隔离容器,内部的子元素不会影响到外部的元素。
不清楚浮动会发生高度塌陷:浮动元素父元素高度自适应(父元素不写高度时,子元素写了浮动后,父元素会发生高度塌陷)
了解更多加入我们前端学习圈
HTML代码
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>用CSS3动画给一个小球创建跳跃的动画效果</title>
<link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
<section class="main">
<div id="ballWrapper">
<div id="ball"></div>
<div id="ballShadow"></div>
</div>
</section>
</body>
</html>
CSS代码(创建一个与html同目录的index.css文件)
#ballWrapper{position:fixedtop:35%left:50%z-index:100margin-left:-70pxwidth:140pxheight:300pxcursor:pointer-webkit-transition:all 5s linear 0s-moz-transition:all 5s linear 0stransition:all 5s linear 0s-webkit-transform:scale(1)-moz-transform:scale(1)-o-transform:scale(1)transform:scale(1)-ms-transform:scale(1)}#ballWrapper:active{cursor:pointer-webkit-transform:scale(0)-moz-transform:scale(0)-o-transform:scale(0)transform:scale(0)-ms-transform:scale(0)}
#ball{position:absolutetop:0z-index:11width:140pxheight:140pxborder-radius:70pxbackground:#bbbbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(187,187,187,1)), color-stop(99%,rgba(119,119,119,1))) background: -webkit-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -o-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -ms-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) box-shadow: inset 0 -5px 15px rgba(255,255,255,0.4), inset -2px -1px 40px rgba(0,0,0,0.4), 0 0 1px #000 cursor: pointer base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2JiYmJiYiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzc3Nzc3NyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(187,187,187,1) 0,rgba(119,119,119,1) 99%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#bbbbbb', endColorstr='#777777', GradientType=0 )-webkit-animation:jump 1s infinite-moz-animation:jump 1s infinite-o-animation:jump 1s infinite-ms-animation:jump 1s infiniteanimation:jump 1s infinite}
#ball::after{position:absolutetop:10pxleft:30pxz-index:10width:80pxheight:40pxborder-radius:40px/20pxbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(232,232,232,1)), color-stop(1%,rgba(232,232,232,1)), color-stop(100%,rgba(255,255,255,0))) background: -webkit-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -o-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -ms-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) content: "" base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2U4ZThlOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjElIiBzdG9wLWNvbG9yPSIjZThlOGU4IiBzdG9wLW9wYWNpdHk9IjEiLz4KICAgIDxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(232,232,232,1) 0,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#e8e8e8', endColorstr='#00ffffff', GradientType=0 )}
#ballShadow{position:absolutebottom:0left:50%z-index:10margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-transform:scaleY(.3)-moz-transform:scaleY(.3)-o-transform:scaleY(.3)transform:scaleY(.3)-ms-transform:scaleY(.3)-webkit-animation:shrink 1s infinite-moz-animation:shrink 1s infinite-o-animation:shrink 1s infinite-ms-animation:shrink 1s infiniteanimation:shrink 1s infinite}
@-webkit-keyframes jump{0%{top:0-webkit-animation-timing-function:ease-in}
50%{top:140pxheight:140px-webkit-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-webkit-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-webkit-animation-timing-function:ease-out}
95%{top:0-webkit-animation-timing-function:ease-in}
100%{top:0-webkit-animation-timing-function:ease-in}
}
@-moz-keyframes jump{0%{top:0-moz-animation-timing-function:ease-in}
50%{top:140pxheight:140px-moz-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-moz-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-moz-animation-timing-function:ease-out}
95%{top:0-moz-animation-timing-function:ease-in}
100%{top:0-moz-animation-timing-function:ease-in}
}
@-o-keyframes jump{0%{top:0-o-animation-timing-function:ease-in}
50%{top:140pxheight:140px-o-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-o-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-o-animation-timing-function:ease-out}
95%{top:0-o-animation-timing-function:ease-in}
100%{top:0-o-animation-timing-function:ease-in}
}
@-ms-keyframes jump{0%{top:0-ms-animation-timing-function:ease-in}
50%{top:140pxheight:140px-ms-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-ms-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-ms-animation-timing-function:ease-out}
95%{top:0-ms-animation-timing-function:ease-in}
100%{top:0-ms-animation-timing-function:ease-in}
}
@keyframes jump{0%{top:0animation-timing-function:ease-in}
50%{top:140pxheight:140pxanimation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60pxanimation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70pxanimation-timing-function:ease-out}
95%{top:0animation-timing-function:ease-in}
100%{top:0animation-timing-function:ease-in}
}
@-webkit-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-webkit-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}
}
@-moz-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-moz-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}
}
@-o-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-o-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}
}
@-ms-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-ms-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}
}
@keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}
}