我们想要展示如何利用CSS3 clip属性制作一种简单而整洁的扩展效果,当点击一个box元素时实现平稳过渡。这个想法是为了实现某种叠加效果,好像它实际上在各个元素的下面。点击其中一个元素将创建一个切断图片的效果,展示将被宽展开的另一层。
 
 怎样做?
 
 首先,我们要创建一个项目列表,它将看起来像是装满不同城市名称的盒子:
 
 每 一个箱子将包含一个元素(覆盖层),该元素的位置将被固定。实际上这个元素会铺满整个页面,但我们不会看到它,因为透明度opacity将被设置为0。当 我们点击一个盒子,我们将使用clip:rect()修剪各自的内部固定元素。然后我们将动态展示所有叠加的宽度和高度,做为整个视窗:
 
 单击关闭按钮将反转这个效果,并且使列表项的大小最小化并消失。
 
 那么,让我们从HTML开始吧!
 
 标记:
 
 我 们将为这些盒子使用一个无序列表。每个列表项将会有一个图标类和一个可选的“span”类,将控制盒子的宽度。在里面将添加一些文本和覆盖的层。叠加的部 分将包含一个列布局的结构。因为我们选择了一个虚拟的天气应用程序作为主题,所以我们将显示未来七天的天气预报。每个“day”列有一些span,用做工 作日、天气图标和温度雅思答案
 
 <ul id=“rb-grid” class=“rb-grid clearfix”>
 
 <li class=“icon-clima-1 rb-span-2”>
 
 <h3>Lisbon</h3>
 
 <span class=“rb-temp”>21°C</span>
 
 <div class=“rb-overlay”>
 
 <span class=“rb-close”>close</span>
 
 <div class=“rb-week”>
 
 <div><span class=“rb-city”>Lisbon</span><span class=“icon-clima-1”></span><span>21°C</span></div>
 
 <div><span>Mon</span><spanclass=“icon-clima-1”></span><span>19°C</span></div>
 
 <div><span>Tue</span><spanclass=“icon-clima-2”></span><span>19°C</span></div>
 
 <div><span>Wed</span><spanclass=“icon-clima-2”></span><span>18°C</span></div>
 
 <div><span>Thu</span><spanclass=“icon-clima-2”></span><span>17°C</span></div>
 
 <div><span>Fri</span><spanclass=“icon-clima-1”></span><span>19°C</span></div>
 
 <div><span>Sat</span><spanclass=“icon-clima-1”></span><span>22°C</span></div>
 
 <div><span>Sun</span><spanclass=“icon-clima-1”></span><span>18°C</span></div>
 
 </div>
 
 </div>
 
 </li>
 
 <li class=“icon-clima-2”>
 
 <h3>Paris</h3><span class=“rb-temp”>11°C</span>
 
 <div class=“rb-overlay”>
 
 <!-- ... -->
 
 </div>
 
 </li>
 
 <li><!-- ... --></li>
 
 <!-- ... -->
 
 </ul>
 
 让我们看看CSS:
 
 一个无序列表将会集中在父结点,我们将会去掉列表样式:
 
 .rb-grid {
 
 list-style: none;
 
 text-align: center;
 
 margin: 0 auto;
 
 }
 
 列表项将有一个不固定的宽度,我们将给他们一个15em的高度。它将浮在左边:
 
 .rb-grid li {
 
 width: 24%;
 
 height: 15em;
 
 margin: 0.5%;
 
 background: #8CC7DF;
 
 color: #fff;
 
 display: block;
 
 float: left;
 
 padding: 1.6em;
 
 cursor: pointer;
 
 position: relative;
 
 }
 
 网格内的项目有三种不同的宽度,“默认”的那个是24%,下面的其他两个是:
 
 .rb-grid li.rb-span-2 {
 
 width: 49%;
 
 }
 
 .rb-grid li.rb-span-4 {
 
 width: 99%;
 
 }
 
 让我们给城市标题加些料:
 
 .rb-grid li h3 {
 
 font-size: 2.6em;
 
 font-weight: 100;
 
 }
 
 为 图标字体添加一个CSS文件。它是Climacons字体,由Adam Whitcroft 创建。你可以查看climacons.css看看我们包括了哪个图标。基本上,我们使用图标类来添加带伪元素的图标。在网格里,我们希望他们是被绝对定位 在右下角,有点像被剥夺了权利雅思改分
 
 .rb-grid li[class^=“icon-”]:before,
 
 .rb-grid li[class*=“ icon-”]:before {
 
 font-size: 10em;
 
 position: absolute;
 
 display: block;
 
 width: 100%;
 
 height: 100%;
 
 top: 0;
 
 left: 0;
 
 line-height: 3;
 
 opacity: 0.4;
 
 text-align: right;
 
 pointer-events: none;
 
 }
 
 温度显示效果是半透明的,我们为它的opacity添加一个过渡:
 
 .rb-temp {
 
 display: block;
 
 font-size: 2em;
 
 opacity: 0.5;
 
 transition: all 0.3s ease-in-out;
 
 }
 
 当悬浮在一个列表项上,我们简单增加:
 
 .rb-grid li:hover .rb-temp {
 
 opacity: 1;
 
 }
 
 现在,让我们看一看重要的重叠部分。最后的视图,我们是想要一个全屏范围的重叠效果,所以我们将把它的宽度和高度设置到100%。我 们不希望它是相对于任何东西,而是希望它在顶部,所以要给它固定了位置。因为这将让它出现在所有东西的顶部,会有重叠,很多重叠!这时需要在初始时设置 z-index为-1。这将把它们统统放在内容页面的后面。设置opacity为0会使他们不可见
 
 .rb-overlay {
 
 opacity: 0;
 
 position: fixed;
 
 top: 0;
 
 left: 0;
 
 width: 100%;
 
 height: 100%;
 
 transition: all 0.4s ease;
 
 z-index: -1;
 
 pointer-events: none;
 
 cursor: default;
 
 }
 
 这就是初始状态的叠加。一旦我们点击列表项,将为clip属性设置正确的rect()值,动态扩展。
 
 让我们看看其余的样式。
 
 每个叠加层有点接近“按钮”,将被定位在右上角:
 
 .rb-close {
 
 position: absolute;
 
 top: 0.4em;
 
 right: 0.4em;
 
 width: 2em;
 
 height: 2em;
 
 text-indent: -9000px;
 
 cursor: pointer;
 
 z-index: 1000;
 
 }
 
 .rb-close:before {
 
 content: ‘x’;
 
 font-weight: 100;
 
 position: absolute;
 
 top: 0;
 
 left: 0;
 
 width: 100%;
 
 height: 100%;
 
 font-size: 3em;
 
 line-height: 0.6;
 
 text-align: center;
 
 text-indent: 0px;
 
 }
 
 列的包装有rb-week类(虽然我们也包括当前天气列在内)。需要将它设置为100%的宽度和高度以便我们可以准确的定义它的孩子的高度和宽度:
 
 .rb-week {
 
 width: 100%;
 
 height: 100%;
 
 }
 
 “列”会有一个10%的宽度(除了第一个有30%的宽度),他们将浮动在左边:
 
 .rb-week > div {
 
 width: 10%;
 
 height: 100%;
 
 float: left;
 
 position: relative;
 
 padding: 3% 0;
 
 }
 
 .rb-week > div:first-child {
 
 width: 30%;
 
 }
 
 总共有八个列,10% 乘以7结果是70%,加上我们有30%的左边第一列。
 
 每个span有一个30%的高度和间距:
 
 .rb-week span {
 
 padding: 5% 0;
 
 font-size: 2em;
 
 font-weight: 100;
 
 display: block;
 
 margin: auto 0;
 
 height: 30%;
 
 width: 100%;
 
 line-height: 0.8;
 
 }
 
 城市名的span有一个特殊的风格,更小的字体权重:
 
 .rb-week span.rb-city {
 
 font-weight: 700;
 
 padding: 1% 10%;
 
 font-size: 1em;
 
 line-height: 1.2;
 
 }
 
 图标将有一个增加的字体大小,我们需要重置字体权重,因为我们已经改变了它的规则:
 
 .rb-week [class^=“icon-”]:before {
 
 font-size: 2.5em;
 
 font-weight: normal;
 
 }
 
 图标在“当前天气列”几乎是透明的:
 
 .rb-week > div:first-child [class^=“icon-”] {
 
 opacity: 0.1;
 
 }
 
 现在,让我们来为每个列表项定义不同的背景颜色。
 
 我们有11个列表项:
 
 /* Colors */
 
 /* Grid */
 
 .rb-grid li:nth-child(1) { background: #3399CC; }
 
 .rb-grid li:nth-child(2) { background: #33CCCC; }
 
 .rb-grid li:nth-child(3) { background: #996699; }
 
 .rb-grid li:nth-child(4) { background: #C24747; }
 
 .rb-grid li:nth-child(5) { background: #e2674a; }
 
 .rb-grid li:nth-child(6) { background: #FFCC66; }
 
 .rb-grid li:nth-child(7) { background: #99CC99; }
 
 .rb-grid li:nth-child(8) { background: #669999; }
 
 .rb-grid li:nth-child(9) { background: #CC6699; }
 
 .rb-grid li:nth-child(10) { background: #339966; }
 
 .rb-grid li:nth-child(11) { background: #666699; }
 
 每个覆盖层我们有八个列:
 
 /* Overlay Columns */
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(1) { background: #3399CC; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(2) { background: #2D87B4; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(3) { background: #297AA3; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(4) { background: #256E93; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(5) { background: #216283; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(6) { background: #1D5672; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(7) { background: #184962; }
 
 .rb-grid li:nth-child(1) .rb-week > div:nth-child(8) { background: #143D52; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(1) { background: #33CCCC; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(2) { background: #2DB4B4; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(3) { background: #29A3A3; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(4) { background: #259393; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(5) { background: #218383; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(6) { background: #1D7272; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(7) { background: #186262; }
 
 .rb-grid li:nth-child(2) .rb-week > div:nth-child(8) { background: #145252; }
 
 最后但并非最不重要,让我们用小屏幕来检验。当空间有限,我们就不希望在网格中显示盒子了:
 
 @media screen and (max-width: 63.125em) {
 
 .rb-grid li,
 
 .rb-grid li.rb-span-2,
 
 .rb-grid li.rb-span-4 {
 
 width: 100%;
 
 height: 10em;
 
 text-align: left;
 
 }
 
 .rb-grid li[class^=“icon-”]:before,
 
 .rb-grid li[class*=“ icon-”]:before {
 
 font-size: 6em;
 
 left: auto;
 
 right: 0;
 
 line-height: 2.5;
 
 }
 
 .rb-grid li > div {
 
 text-align: center;
 
 }
 
 }
 
 JAVASCRIPT:
 
 var $items = $( ‘#rb-grid > li’ ),
 
 transEndEventNames = {
 
 ‘WebkitTransition’ : ‘webkitTransitionEnd’,
 
 ‘MozTransition’ : ‘transitionend’,
 
 ‘OTransition’ : ‘oTransitionEnd’,
 
 ‘msTransition’ : ‘MSTransitionEnd’,
 
 ‘transition’ : ‘transitionend’
 
 },
 
 // transition end event name
 
 transEndEventName = transEndEventNames[ Modernizr.prefixed( ‘transition’ ) ],
 
 // window and body elements
 
 $window = $( window ),
 
 $body = $( ‘BODY’ ),
 
 // transitions support
 
 supportTransitions = Modernizr.csstransitions,
 
 // current item‘s index
 
 current = -1,
 
 // window width and height
 
 winsize = getWindowSize();
 
 当点击每个条目时,如果不支持过渡效果,就会跳过第一个状态并且叠加效果会立即扩展。
 
 initEvents() {
 
 $items.each( function() {
 
 var $item = $( this ),
 
 $close = $item.find( ‘span.rb-close’ ),
 
 $overlay = $item.children( ‘div.rb-overlay’ );
 
 $item.on( ‘click’, function() {
 
 if( $item.data( ‘isExpanded’ ) ) {
 
 return false;
 
 }
 
 $item.data( ‘isExpanded’, true );
 
 // save current index of the item
 
 current = $item.index();
 
 var layoutProp = getItemLayoutProp( $item ),
 
 clipPropFirst = ‘rect(’ + layoutProp.top + ‘px ’ + ( layoutProp.left + layoutProp.width ) + ‘px ’ + ( layoutProp.top + layoutProp.height ) + ‘px ’ + layoutProp.left + ‘px)’,
 
 clipPropLast = ‘rect(0px ’ + winsize.width + ‘px ’ + winsize.height + ‘px 0px)’;
 
 $overlay.css( {
 
 clip : supportTransitions ? clipPropFirst : clipPropLast,
 
 opacity : 1,
 
 zIndex: 9999,
 
 pointerEvents : ‘auto’
 
 } );
 
 if( supportTransitions ) {
 
 $overlay.on( transEndEventName, function() {
 
 $overlay.off( transEndEventName );
 
 setTimeout( function() {
 
 $overlay.css( ‘clip’, clipPropLast ).on( transEndEventName, function() {
 
 $overlay.off( transEndEventName );
 
 $body.css( ‘overflow-y’, ‘hidden’ );
 
 } );
 
 }, 25 );
 
 } );
 
 }
 
 else {
 
 $body.css( ‘overflow-y’, ‘hidden’ );
 
 }
 
 } );
 
 。。.
 
 } );
 
 。。.
 
 }
 
 function getItemLayoutProp( $item ) {
 
 var scrollT = $window.scrollTop(),
 
 scrollL = $window.scrollLeft(),
 
 itemOffset = $item.offset();
 
 return {
 
 left : itemOffset.left - scrollL,
 
 top : itemOffset.top - scrollT,
 
 width : $item.outerWidth(),
 
 height : $item.outerHeight()
 
 };
 
 }

将CSS CLIP属性应用在:扩展覆盖效果的更多相关文章

  1. CSS Clip属性

    Clip属性在W3C官网是这样进行描述的:“通过对元素进行剪切来控制元素的可显示区域,默认情况下,元素是不进行任何剪切的,但是也有可能剪切区域也显示的设置了clip属性”. .selector { c ...

  2. 理解CSS Clip属性及用法

    应用Clip属性实现的一个简单效果图: 样式写法: .my-element { position: absolute; clip: rect(10px  350px  170px  0); /* IE ...

  3. [ javascript css clip ] javascript css clip 的奇思妙想之文字拼接效果

      语法: clip : auto | rect ( number number number number ) 参数: auto : 对象无剪切 rect ( number number numbe ...

  4. CSS Clip剪切元素动画实例

    1.CSS .fixed { position: fixed; width: 90px; height: 90px; background: red; border: 0px solid blue; ...

  5. CSS元素:clip属性作用说明

    clip属性是一个比较有用的属性,但往往在实际应用中,并不多见,介绍的也很少.应用clip属性需要注意的两点: 一.clip属性必须和定位属性postion一起使用才能生效. 二.clip裁切的计算坐 ...

  6. CSS常用属性-xy

    一.文本Text CSS text-align 属性 文本对齐方式 CSS text-decoration 属性 text-decoration 属性规定添加到文本的修饰 CSS line-heigh ...

  7. css之属性部分

    这篇写的是今天的学习到的属性,一共20个. 属性再多,但也要会使用,会在使用时可以记起它,才能起到它为我们所需要的作用. 样式属性 1.border CSS边框属性允许你指定一个元素边框的样式和颜色. ...

  8. CSS背景属性Background详解

    [转] 本文详解了CSS的背景属性Background,包括CSS3中新增的背景属性.如果你是个CSS初学者,还可以查看之前介绍的CSS浮动属性和CSS透明属性详解. CSS2 中有5个主要的背景(b ...

  9. CSS clip:rect矩形剪裁功能及一些应用介绍

    CSS clip:rect矩形剪裁功能及一些应用介绍 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.co ...

随机推荐

  1. The Same Game(模拟)

    http://poj.org/problem?id=1027 题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有 ...

  2. BZOJ 1083 [SCOI2005]繁忙的都市

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1664  Solved: 1080[Submit][Sta ...

  3. Java编译原理

    http://wenku.baidu.com/view/f9b1734b87c24028915fc3a3.html Java编译原理 1. 关于动态加载机制 学习Java比C++更容易理解OOP的思想 ...

  4. gif动画问题

    iOS没有自带支持显示gif动画的功能,  用UIImageView的animationImage虽然可以实现图片动画, 当毕竟不方便. http://blog.stijnspijker.nl/200 ...

  5. arcgis数据文件使用

    dem数据打开,保存,使用 打开

  6. HDOJ(HDU) 2304 Electrical Outlets(求和、、)

    Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is ...

  7. legoblock秀上限

    很久没有做题了,前天做了一道题结果弱的一逼...搜了解题报告不说...还尼玛秀了上限 题意: 给出宽和高为n和m的一堵墙,手上有长为1,2,3,4高均为1的砖,问形成一个坚固的墙有多少种做法. 坚固的 ...

  8. GCD中有哪几种Queue?你自己建立过串行Queue吗?背后的线程模型是什么样的

    一共有五种,看图 Paste_Image.png 主线程也就是那个main,一般后台处理数据就就用default那个.创建过一个queue,处理NSMutableArray的时候都在在这一个queue ...

  9. CFBundleVersion与CFBundleShortVersionString,版本上架注意事项

    CFBundleVersion,标识(发布或未发布)的内部版本号.这是一个单调增加的字符串,包括一个或多个时期分隔的整数. CFBundleShortVersionString  标识应用程序的发布版 ...

  10. POJ 3384 Feng Shui 半平面交

    题目大意:一个人很信"Feng Shui",他要在房间里放两个圆形的地毯. 这两个地毯之间可以重叠,可是不能折叠,也不能伸到房间的外面.求这两个地毯可以覆盖的最大范围.并输出这两个 ...