完成侧边栏东西(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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞