【基础系列】CSS专题

1 CSS属性

1.1 滤镜

1.1.1 blur属性

1.1.1.1 代码示例

CSS代码:

.blur {  

    filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */

    -webkit-filter: blur(10px); /* Chrome, Opera */

       -moz-filter: blur(10px);

        -ms-filter: blur(10px);   

            filter: blur(10px);

    filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=10,

MakeShadow=false); /* IE6~IE9 */

}

HTML代码:

《【基础系列】CSS专题》

效果:

《【基础系列】CSS专题》

1.1.1.2 兼容性

        绿色即是可支持版本

《【基础系列】CSS专题》

1.2 变形(transform)

        在CSS3transform主要包括以下几种:旋转rotate、扭曲skew、缩放scale和移动translate以及矩阵变形matrix。

1.2.1 语法

1.2.1.1 语法格式

  transform:none | [ ]*

       也就是:

 transform: rotate | scale | skew | translate|matrix;

        none:表示不进么变换;表示一个或多个变换函数,以空格分开;换句话说就是我们同时对一个元素进行transform的多种属性操作,例如rotate、scale、translate三种,但这里需要提醒大家的,以往我们叠加效果都是用逗号(“,”)隔开,但transform中使用多个属性时却需要有空格隔开。大家记住了是空格隔开。

    取值:

        transform属性实现了一些可用SVG实现的同样的功能。它可用于内联(inline)元素和块级(block)元素。它允许我们旋转、缩放和移动元素 ,他有几个属性值参数:rotate;translate;scale;skew;matrix。

1.2.1.2 transform在不同浏览器内核下的书写规则

  //Mozilla内核浏览器:firefox3.5+

  -moz-transform:rotate | scale | skew | translate ;

 //Webkit内核浏览器:Safariand Chrome

 -webkit-transform: rotate | scale | skew | translate ;

 //Opera

  -o-transform:rotate | scale | skew | translate ;

 //IE9

  -ms-transform:rotate | scale | skew | translate ;

 //W3C标准

  transform: rotate| scale | skew | translate ;

1.2.1.3 浏览器支持版本

《【基础系列】CSS专题》

1.2.2 旋转rotate

        rotate():通过指定的角度参数对原元素指定一个2D rotation(2D旋转),需先有transform-origin属性的定义。transform-origin定义的是旋转的基点,其中angle是指旋转角度,如果设置的值为正数表示顺时针旋转,如果设置的值为负数,则表示逆时针旋转。如:transform:rotate(30deg):

《【基础系列】CSS专题》

1.2.3 移动translate

        移动translate我们分为三种情况:translate(x,y)水平方向和垂直方向同时移动(也就是X轴和Y轴同时移动);translateX(x)仅水平方向移动(X轴移动);translateY(Y)仅垂直方向移动(Y轴移动),具体使用方法如下:

        1、translate([,]):通过矢量[tx, ty]指定一个2D translation,tx是第一个过渡值参数,ty是第二个过渡值参数选项。如果 未被提供,则ty以0作为其值。也就是translate(x,y),它表示对象进行平移,按照设定的x,y参数值,当值为负数时,反方向移动物体,其基点默认为元素 中心点,也可以根据transform-origin进行改变基点。如transform:translate(100px,20px):

《【基础系列】CSS专题》

        2、translateX(): 通过给定一个X方向上的数目指定一个translation。只向x轴进行移动元素,同样其基点是元素中心点,也可以根据transform-origin改变基点位置。如:transform:translateX(100px):

《【基础系列】CSS专题》

3、translateY():通过给定Y方向的数目指定一个translation。只向Y轴进行移动,基点在元素心点,可以通过transform-origin改变基点位置。如:transform:translateY(20px):

《【基础系列】CSS专题》

1.2.4 缩放scale

        缩放scale和移动translate是极其相似,他也具有三种情况:scale(x,y)使元素水平方向和垂直方向同时缩放(也就是X轴和Y轴同时缩放);scaleX(x)元素仅水平方向缩放(X轴缩放);scaleY(y)元素仅垂直方向缩放(Y轴缩放),但它们具有相同的缩放中心点和基数,其中心点就是元素的中心位置,缩放基数为1,如果其值大于1元素就放大,反之其值小于1,元素缩小。下面我们具体来看看这三种情况具体使用方法:

        1、scale([,]):提供执行[sx,sy]缩放矢量的两个参数指定一个2D scale(2D缩放)。如果第二个参数未提供,则取与第一个参数一样的值。scale(X,Y)是用于对元素进行缩放,可以通过transform-origin对元素的基点进行设置,同样基点在元素中心位置;基中X表示水平方向缩放的倍数,Y表示垂直方向的缩放倍数,而Y是一个可选参数,如果没有设置Y值,则表示X,Y两个方向的缩放倍数是一样的。并以X为准。如:transform:scale(2,1.5):

《【基础系列】CSS专题》

        2、scaleX(): 使用[sx,1]缩放矢量执行缩放操作,sx为所需参数。scaleX表示元素只在X轴(水平方向)缩放元素,他的默认值是(1,1),其基点一样是在元素的中心位置,我们同样是通过transform-origin来改变元素的基点。如:transform:scaleX(2):

《【基础系列】CSS专题》

        3、scaleY(): 使用[1,sy]缩放矢量执行缩放操作,sy为所需参数。scaleY表示元素只在Y轴(垂直方向)缩放元素,其基点同样是在元素中心位置,可以通过transform-origin来改变元素的基点。如transform:scaleY(2):

《【基础系列】CSS专题》

1.2.5 扭曲skew

        扭曲skew和translate、scale一样同样具有三种情况:skew(x,y)使元素在水平和垂直方向同时扭曲(X轴和Y轴同时按一定的角度值进行扭曲变形);skewX(x)仅使元素在水平方向扭曲变形(X轴扭曲变形);skewY(y)仅使元素在垂直方向扭曲变形(Y轴扭曲变形),具体使用如下:

        1、skew( [, ]):X轴Y轴上的skew transformation(斜切变换)。第一个参数对应X轴,第二个参数对应Y轴。如果第二个参数未提供,则值为0,也就是Y轴方向上无斜切。skew是用来对元素进行扭曲变行,第一个参数是水平方向扭曲角度,第二个参数是垂直方向扭曲角度。其中第二个参数是可选参数,如果没有设置第二个参数,那么Y轴为0deg。同样是以元素中心为基点,我们也可以通过transform-origin来改变元素的基点位置。如:transform:skew(30deg,10deg):

《【基础系列】CSS专题》

        2、skewX(): 按给定的角度沿X轴指定一个skew transformation(斜切变换)。skewX是使元素以其中心为基点,并在水平方向(X轴)进行扭曲变行,同样可以通过transform-origin来改变元素的基点。如:transform:skewX(30deg)

《【基础系列】CSS专题》

        3、skewY(): 按给定的角度沿Y轴指定一个skew transformation(斜切变换)。skewY是用来设置元素以其中心为基点并按给定的角度在垂直方向(Y轴)扭曲变形。同样我们可以通过transform-origin来改变元素的基点。如:transform:skewY(10deg)

《【基础系列】CSS专题》

1.2.6 矩阵matrix

        matrix(, ,, , , ): 以一个含六值的(a,b,c,d,e,f)变换矩阵的形式指定一个2D变换,相当于直接应用一个[a b c d e f]变换矩阵。就是基于水平方向(X轴)和垂直方向(Y轴)重新定位元素。

1.2.7 改变元素基点transform-origin

        要作用就是让我们在进行transform动作之前可以改变元素的基点位置,因为我们元素默认基点就是其中心位置,换句话说我们没有使用transform-origin改变元素基点位置的情况下,transform进行的rotate,translate,scale,skew,matrix等操作都是以元素自己中心位置进行变化的。

        transform-origin(X,Y):用来设置元素的运动的基点(参照点)。默认点是元素的中心点。其中X和Y的值可以是百分值,em,px,其中X也可以是字符参数值left,center,right;Y和X一样除了百分值外还可以设置字符值top,center,bottom,这个看上去有点像我们background-position设置一样;下面我列出他们相对应的写法:

    1、top left |left top等价于0 0 | 0% 0%

    2、top | top center | center top等价于50% 0

    3、right top |top right等价于100% 0

    4、left | left center | center left等价于0 50% | 0% 50%

    5、center |center center等价于50% 50%(默认值)

    6、right |right center | center right等价于100% 50%

    7、bottom left| left bottom等价于0 100% | 0% 100%

    8、bottom |bottom center | center bottom等价于50% 100%

    9、bottom right| right bottom等价于100% 100%

        其中left, center right是水平方向取值,对应的百分值为left=0%; center=50%; right=100%而top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%;如果只取一个值,表示垂直方向值不变,我们分别来看看以下几个实例

(1)transform-origin: (left, top):

《【基础系列】CSS专题》

(2)transform-origin: right

《【基础系列】CSS专题》

(3)transform-origin(25%,75%)

        更多的改变中心基点办法,大家可以在本地多测试一下,多体会一下,这里还要提醒大家一点的是,transform-origin并不是transform中的属性值,他具有自己的语法,前面我也说过了,说简单一点就是类似于我们的background-position的用法,但又有其不一样,因为我们background-position不需要区别浏览器内核不同的写法,但transform-origin跟其他的css3属性一样,我们需要在不同的浏览内核中加上相应的前缀,下面列出各种浏览器内核下的语法规则:

  //Mozilla内核浏览器:firefox3.5+

  -moz-transform-origin: x y;

  //Webkit内核浏览器:Safari and Chrome

  -webkit-transform-origin: x y;

  //Opera

  -o-transform-origin: x y ;

  //IE9

  -ms-transform-origin: x y;

  //W3C标准

  transform-origin: x y ;

1.2.8 示例

1.2.8.1 transform: translateX(x)示例

HTML代码:

《【基础系列】CSS专题》

CSS代码:

 .menu ul li.translate-x a: hover {

    -moz-transform: translateX(-10px);

    -webkit-transform: translateX(-10px);

    -o-transform: translateX(-10px);

    -ms-transform: translateX(-10px);

    transform: translateX(-10px);         

  }

1.3 转换(transition)

1.4 动画(animation)

1.4.1 CSS3 @keyframes规则

        如需在CSS3中创建动画,您需要学习@keyframes规则。@keyframes 规则用于创建动画。在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果。

1.4.1.1 浏览器支持

        Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 规则和 animation 属性。

Chrome 和 Safari 需要前缀 -webkit-。

1.4.1.2 实例

@keyframes myfirst

{

    from {background: red;}

    to {background: yellow;}

}

@-moz-keyframes myfirst/* Firefox */

{

    from {background: red;}

    to {background: yellow;}

}

@-webkit-keyframes myfirst/* Safari 和 Chrome */

{

    from {background: red;}

    to {background: yellow;}

}

@-o-keyframes myfirst/* Opera */

{

    from {background: red;}

    to {background: yellow;}

}

1.4.1.3 CSS3动画使用规则

        当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。

通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器:

    • 规定动画的名称;

    • 规定动画的时长;

实例

        把 “myfirst” 动画捆绑到 div 元素,时长:5 秒:

div

{

    animation: myfirst 5s;

    -moz-animation: myfirst 5s; /* Firefox */

    -webkit-animation: myfirst 5s;   /* Safari 和 Chrome */

    -o-animation: myfirst 5s;   /* Opera */

}

        注释:您必须定义动画的名称和时长。如果忽略时长,则动画不会允许,因为默认值是0。

1.4.2 用百分比定义动画关键帧变化时间点

        请用百分比来规定变化发生的时间,或用关键词”from” 和 “to”,等同于 0% 和 100%。0%是动画的开始,100%是动画的完成。

        为了得到最佳的浏览器支持,您应该始终定义0% 和100% 选择器。

 

实例

改变背景色和位置:

@keyframes myfirst

{

    0%   {background: red; left:0px;top:0px;}

    25%  {background: yellow;left:200px; top:0px;}

    50%  {background: blue; left:200px; top:200px;}

    75%  {background: green; left:0px;top:200px;}

    100% {background: red; left:0px; top:0px;}

}

@-moz-keyframes myfirst/* Firefox */

{

    0%   {background: red; left:0px;top:0px;}

    25%  {background: yellow;left:200px; top:0px;}

    50%  {background: blue; left:200px;top:200px;}

    75%  {background: green; left:0px;top:200px;}

    100% {background: red; left:0px; top:0px;}    

}

@-webkit-keyframes myfirst/* Safari 和 Chrome */

{

    0%   {background: red; left:0px;top:0px;}

    25%  {background: yellow;left:200px; top:0px;}

    50%  {background: blue; left:200px;top:200px;}

    75%  {background: green; left:0px;top:200px;}

    100% {background: red; left:0px; top:0px;}

}

@-o-keyframes myfirst/* Opera */

{

    0%   {background: red; left:0px;top:0px;}

    25%  {background: yellow; left:200px;top:0px;}

    50%  {background: blue; left:200px;top:200px;}

    75%  {background: green; left:0px;top:200px;}

    100% {background: red; left:0px; top:0px;}

}

1.4.3 CSS3动画属性

        下面的表格列出了 @keyframes 规则和所有动画属性:

《【基础系列】CSS专题》

        下面的两个例子设置了所有动画属性:

实例

        运行名为 myfirst 的动画,其中设置了所有动画属性:

div

{

    animation-name: myfirst;

    animation-duration: 5s;

    animation-timing-function: linear;

    animation-delay: 2s;

    animation-iteration-count: infinite;

    animation-direction: alternate;

    animation-play-state: running;

    /* Firefox: */

    -moz-animation-name: myfirst;

    -moz-animation-duration: 5s;

    -moz-animation-timing-function: linear;

    -moz-animation-delay: 2s;

    -moz-animation-iteration-count: infinite;

    -moz-animation-direction: alternate;

    -moz-animation-play-state: running;

    /* Safari 和 Chrome: */

    -webkit-animation-name: myfirst;

    -webkit-animation-duration: 5s;

    -webkit-animation-timing-function: linear;

    -webkit-animation-delay: 2s;

    -webkit-animation-iteration-count: infinite;

    -webkit-animation-direction: alternate;

    -webkit-animation-play-state: running;

    /* Opera: */

    -o-animation-name: myfirst;

    -o-animation-duration: 5s;

    -o-animation-timing-function: linear;

    -o-animation-delay: 2s;

    -o-animation-iteration-count: infinite;

    -o-animation-direction: alternate;

    -o-animation-play-state: running;

}

实例

        与上面的动画相同,但是使用了简写的动画 animation 属性:

div

{

    animation: myfirst 5s linear 2s infinitealternate;

    /* Firefox: */

    -moz-animation: myfirst 5s linear 2s infinitealternate;

    /* Safari 和 Chrome: */

    -webkit-animation: myfirst 5s linear 2s infinitealternate;

    /* Opera: */

    -o-animation: myfirst 5s linear 2s infinitealternate;

}

1.4.4 CSS3 animation-timing-function属性

    实例

        从开头到结尾以相同的速度来播放动画:

div

{

    animation-timing-function:2s;

    -webkit-animation-timing-function:2s;/* Safari 和 Chrome */

}

1.4.4.1 定义和用法

        animation-timing-function 规定动画的速度曲线。

        速度曲线定义动画从一套 CSS 样式变为另一套所用的时间。速度曲线用于使变化更为平滑。

默认值:ease

继承性:no

版本:CSS3

JavaScript 语法:object.style.animationTimingFunction=”linear”

1.4.4.2 语法

animation-timing-function:value;

        animation-timing-function 使用名为三次贝塞尔(Cubic Bezier)函数的数学函数,来生成速度曲线。您能够在该函数中使用自己的值,也可以预定义的值:

《【基础系列】CSS专题》

        提示:请试着在下面的“亲自试一试”功能中使用不同的值。

2 通用样式

2.1 第三方样式库

微信UI样式库

https://github.com/weui/weui

微信网页开发样式库

https://mp.weixin.qq.com/wiki/2/ae9782fb42e47ad79eb7b361c2149d16.html

2.2  常用样式范例

2.2.1 音波扩散动画

css3波浪形loading动画

http://www.cnblogs.com/djdliu/p/6120929.html

.loading {

  width: 100%;

  height: 100%;

  background: #014a9c;

  position: relative;

}

.show {

  width: 50px;

  height: 50px;

  border-radius: 100%;

  position: absolute;

  left: 50%;

  top: 50%;

  margin-top: -25px;

  margin-left: -25px;

  animation: sploosh4s ease-in-out both infinite;

  -webkit-animation:sploosh 4s ease-in-out both infinite;

  background: rgba(66,166, 223, 0.7);

}

.show_02 {

 -webkit-animation-delay: -0.8s;

  animation-delay:-0.8s;

}

.show_03 {

 -webkit-animation-delay: -1.6s;

  animation-delay:-1.6s;

}

.show_04 {

 -webkit-animation-delay: -2.4s;

  animation-delay:-2.4s;

}

.show_05 {

 -webkit-animation-delay: -3.2s;

  animation-delay:-3.2s;

}

@-webkit-keyframes sploosh {

  0% {

    box-shadow: 0 0 00 rgba(66, 166, 223, 0.7);

    background:rgba(66, 166, 223, 0.7);

  }

  100% {

    box-shadow: 0 0 0250px rgba(66, 166, 223, 0);

    background:rgba(66, 166, 223, 0);

  }

}

@keyframes sploosh {

  0% {

    box-shadow: 0 0 00 rgba(66, 166, 223, 0.7);

    background:rgba(66, 166, 223, 0.7);

  }

  100% {

    box-shadow: 0 0 0250px rgba(66, 166, 223, 0);

    background:rgba(66, 166, 223, 0);

  }

}

2.2.2 波浪动画

http://www.zaole.net/demo/201605061144.html

3 参考链接

小tip:使用CSS将图片转换成模糊(毛玻璃)效果

http://www.zhangxinxu.com/wordpress/2013/11/小tip-使用css将图片转换成模糊毛玻璃效果/

CSS3 Transform

http://www.w3cplus.com/content/css3-transform/

CSS Grid布局指南

http://www.w3cplus.com/css3/a-complete-guide-css-grid-layout.html?utm_source=tuicool&utm_medium=referral

-webkit-overflow-scrolling: touch导致Native滚动时出现黑色背景

http://www.jianshu.com/p/171b8b7761cb

Webkit overflow scrolling touch CSS bug on iPad

http://stackoverflow.com/questions/9084118/webkit-overflow-scrolling-touch-css-bug-on-ipad/12477554

-webkit-overflow-scrolling带来的BUG

http://www.tuicool.com/articles/U73Erq

iOS8 Safari -webkit-overflow-scrolling: touch; issue

http://stackoverflow.com/questions/26738764/ios8-safari-webkit-overflow-scrolling-touch-issue

总结移动端页面开发时需要注意的一些问题

http://blog.csdn.net/lz305263/article/details/52211850

运用webkit绘制渲染页面原理解决iscroll4闪动的问题

http://www.cnblogs.com/aaronjs/p/3147461.html

解决iOS webkit使用CSS动画时闪烁的问题

http://blog.meathill.com/tech/js/phonegap-js/fix-page-flash-in-ios-webkit.html

CSS3中-webkit-overflow-scrolling:touch的使用方法详解

http://www.111cn.net/cssdiv/css/108397.htm

How to fix flicker when using Webkit transforms &transitions

http://stackoverflow.com/questions/2975217/how-to-fix-flicker-when-using-webkit-transforms-transitions

iOS8 webkit-overflow-scrolling:touch with overlay

http://stackoverflow.com/questions/27934845/ios8-webkit-overflow-scrollingtouch-with-overlay

iOS 9 `-webkit-overflow-scrolling:touch` and `overflow:scroll` breaks scrolling capability

http://stackoverflow.com/questions/32897960/ios-9-webkit-overflow-scrollingtouch-and-overflow-scroll-breaks-scrolling?rq=1

Webkit渲染引擎导致页面闪动

http://diyitui.com/content-1393921248.2615296.html

iscroll在iphone浏览器上闪动的BUG

http://www.jslover.com/code/413.html#

用scrollTop制作一个自动滚动公告栏

http://www.cnblogs.com/ooooevan/p/5551104.html

iOS5 Images disappear when scrolling withwebkit-overflow-scrolling: touch

http://stackoverflow.com/questions/8110580/ios5-images-disappear-when-scrolling-with-webkit-overflow-scrolling-touch/8275972#8275972

CSS3 property webkit-overflow-scrolling:touch ERROR

http://stackoverflow.com/questions/7808110/css3-property-webkit-overflow-scrollingtouch-error

100% screen height css

http://stackoverflow.com/questions/7357818/100-screen-height-css

-webkit-overflow-scrolling: touch导致Native滚动时出现黑色背景

http://www.jianshu.com/p/171b8b7761cb

-webkit-overflow-scrolling: touch; breaks in Apple’s iOS8

http://stackoverflow.com/questions/26176288/webkit-overflow-scrolling-touch-breaks-in-apples-ios8?noredirect=1&lq=1

`-webkit-overflow-scrolling: touch` broken for initiallyoffscreen elements in iOS7

http://stackoverflow.com/questions/18736297/webkit-overflow-scrolling-touch-broken-for-initially-offscreen-elements-in-i?rq=1

iOS8 Safari -webkit-overflow-scrolling: touch; issue

http://stackoverflow.com/questions/26738764/ios8-safari-webkit-overflow-scrolling-touch-issue

How we fixed the -webkit-overflow-scrolling: touch; bug oniOS

http://patrickmuff.ch/blog/2014/10/01/how-we-fixed-the-webkit-overflow-scrolling-touch-bug-on-ios/

-webkit-overflow-scrolling带来的BUG

http://www.tuicool.com/articles/U73Erq

CSS教程

http://www.w3school.com.cn/css/index.asp

微信UI样式库

https://github.com/weui/weui

CSS3阴影box-shadow的使用和技巧总结

http://blog.csdn.net/freshlover/article/details/7610269

    原文作者:Kevin_Junbaozi
    原文地址: https://www.jianshu.com/p/7f821eef1c81
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞