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

需要开发类似于CSDN文章浏览右侧显示的工具条,如下所示
《实现侧边栏工具(1)使用背景图片方式》

需要实现的效果如下:
《实现侧边栏工具(1)使用背景图片方式》

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;    
}
  • 内部单个工具条栏目中有类似相同的代码
    《实现侧边栏工具(1)使用背景图片方式》

可以外部新建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属性也需要如此考虑。

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