实现侧边栏工具(1)使用背景图片方式

需要开发类似于CSDN文章浏览右侧显示的工具条,如下所示
这里写图片描述

需要实现的效果如下:
这里写图片描述

1 HTML格式

主要原理利用a标签和图片的sprite来实现
创建一个toolbat类的工具条div
点击a标签不产生任何效果

<div class="toolbar">
    a[href="javascript:;"].toolbar-item.toolbat-item-app*4
</div>

完整代码,公共类toolbar-item用来设置相同的宽高,第一个app在鼠标hover时弹出二维码下载,在a标签中包含一个span标签,用来放置二维码

<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>

2 CSS样式

这里使用sass样式预编译工具来编写css样式,sass的具体使用另一篇文章中已经说过,参考 Sass和Compass学习笔记(1)
以下介绍将都在scss文件中编写
公共变量设置,工具条尺寸

/* 参数变量设置 */
$toolbar-width: 90px;
$toolbar-height: 28px; 

外部统一工具条toolbar类设置

.toolbar{
    position: fixed;//工具条固定定位
    right: 10%;
    top: 50%;
}

a标签公共样式toolbar-item设置

/* 公共样式设置 */
.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;
    }
}

二维码初始样式设置

.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;
}

3 性能优化

  • 可以将toolbar-itemtoolbar-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属性也需要如此考虑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值