须要开发相似于CSDN文章浏览右侧显示的工具条,以下所示javascript
须要实现的效果以下:css
主要原理利用a标签和图片的sprite来实现
建立一个toolbat类的工具条div
点击a标签不产生任何效果java
<div class="toolbar"> a[href="javascript:;"].toolbar-item.toolbat-item-app*4 </div>
完整代码,公共类toolbar-item用来设置相同的宽高,第一个app在鼠标hover时弹出二维码下载,在a标签中包含一个span标签,用来放置二维码css3
<div class="toolbar"> <a href="javascript:;" class="toolbar-item toolbar-item-app"> <span class="toolbar-layer"></span> </a> <a href="javascript:;" class="toolbar-item toolbar-item-feedback"></a> <a href="javascript:;" class="toolbar-item toolbar-item-other"> <span class="toolbar-layer"></span> </a> <a href="javascript:;" class="toolbar-item toolbar-item-top"></a> </div>
这里使用sass样式预编译工具来编写css样式,sass的具体使用另外一篇文章中已经说过,参考 Sass和Compass学习笔记(1)
如下介绍将都在scss文件中编写
公共变量设置,工具条尺寸web
/* 参数变量设置 */ $toolbar-width: 90px; $toolbar-height: 28px;
外部统一工具条toolbar类设置浏览器
.toolbar{ position: fixed;//工具条固定定位 right: 10%; top: 50%; }
a标签公共样式toolbar-item设置sass
/* 公共样式设置 */ .toolbar-item{ position: relative; display: block; width: $toolbar-width; height: $toolbar-height; background-image: url(../img/com-toolbar.png); background-repeat: no-repeat; margin-top: 10px; z-index: 1000; transition: background-position 1s; /*鼠标hover时a标签下面的toolbar-layer二维码显示,透明度为1,兼容ie6,缩放大小为1*/ &:hover{ .toolbar-layer{ opacity: 1; filter: alpha(opacity=100); transform: scale(1); } } }
内部标签订义样式性能优化
.toolbar-item-app{ background-position: 0 0; &:hover{ background-position: -100px 0; } .toolbar-layer{ height: 112px; background-position: 0 -198px; } } .toolbar-item-feedback{ background-position: 0 -33px; &:hover{ background-position: -100px -33px; } } .toolbar-item-other{ background-position: 0 -66px; &:hover{ background-position: -100px -66px; } .toolbar-layer{ height: 112px; background-position: 0 -198px; } } .toolbar-item-top{ background-position: 0 -165px; &:hover{ background-position: -100px -165px; } }
二维码初始样式设置app
.toolbar-layer{ cursor: pointer; position: absolute;//相对于工具条绝对定位 right: $toolbar-width; bottom:-1px; width: 90px; background-image: url(../img/com-toolbar.png); background-repeat: no-repeat; opacity: 0;//开始透明度为0 filter: alpha(opacity=0); transform: scale(0.01);//初始大小为0.01,不可见 z-index: 1000; transform-origin: right bottom;//从底部和右侧开始缩放 transition: all 1s; }
能够将toolbar-item和toolbar-layer类相同的部分单独提出函数
.toolbar-item, .toolbar-layer{ background-image: url(../img/com-toolbar.png); background-repeat: no-repeat; }
内部单个工具条栏目中有相似相同的代码
能够外部新建mixin统一模块
@mixin toolbar-item($x, $y, $hoverx, $hovery){ background-position: $x $y; &:hover{ background-position: $hoverx $hovery; } }
函数调用
@include toolbar-item(0, 0, -100px, 0);
transition属于css3属性,须要考虑不一样浏览器的兼容性,一样对transition进行封装。
新建mixin
@mixin transition($transition){ -webkit-transition: $transition; -moz-transition: $transition; -ms-transition: $transition; -o-transition: $transition; transition: $transition; }
.toolbar-item中函数调用
@include transition(background-position 1s);
一样其余的CSS3属性也须要如此考虑。