从零实现Vue的组件库(六)- Hover-Tip 实现
栏目: JavaScript · 发布时间: 7年前
内容简介:代码实例地址:Hover-Tip 实例代码地址:
代码
<!-- 基础用法 -->
<fat-hovertip>
<template slot="hover-part">
<fat-button>组件</fat-button>
</template>
<template slot="tip-part">向下</template>
</fat-hovertip>
复制代码
实例地址:Hover-Tip 实例
代码地址: Github UI-Library
2. 原理
由于需要Hover-tip组件具备良好的扩展性较好,所以采用具名插槽来完成。
将该组件分为两个部分:
- hover-part:鼠标hover区域;
- tip-part:展示提示框的内容,具体如下图。
template 的基本结构为
<div :class="['hover-tip', customClass]">
<div class="hover-part">
<slot name="hover-part">
<!-- slot 的默认节点 -->
<fat-icon name="help" size="18" />
</slot>
</div>
<div :class="[type, 'tip-part']">
<slot name="tip-part"></slot>
<div class="arrow"></div>
<div class="block"></div>
</div>
</div>
复制代码
template 中具有两个具名插槽,其中 <slot name="hover-part"> 为鼠标hover区域,可以为它设定默认值,以减少项目中代码的冗余。 <slot name="tip-part> 代表提示框的内容区域,完全自定义。
相关样式:tip-part的显示、tip-part的相对位置。
鼠标 hover 的时候,显示 tip-part
.hover-tip {
position: relative;
display: inline-flex;
.hover-part {
display: flex;
align-items: center;
}
// 初始的tip-part的display为none
.tip-part {
display: none;
...
}
// 当鼠标hover的时候hover-part区域的时候,显示tip-part
.hover-part:hover + .tip-part {
display: block;
}
}
复制代码
tip-part 的位置,以及上方居中为例 top-center
$base-offset: 8px;
.top-center {
margin-bottom: $base-offset;
position: absolute;
left: 50%;
bottom: 100%;
transform: translateX(-50%);
}
复制代码
tip-part 为绝对定位,其最近的非 static 定位祖先元素为 hover-tip ,相对于它偏移
/* <percentage>s of the width of the containing block */ left:50%; /* <percentage>s of the height of the containing block */ bottom:100% 复制代码
left , bottom 偏移的单位分别为包含块的宽和高,之后 tip-part 还需要向左偏移自己的50%,所以添加 transform: translateX(-50%); ,此时tip-part位于 hover-tip 上方的正中间,如示意图。
同样可以开发 bottom-center , left-center , right-center 。由于 bottom-center , top-center 有公共部分,可进一步聚合
.top-center,
.bottom-center {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.bottom-center {
margin-top: $base-offset;
top: 100%;
}
.top-center {
margin-bottom: $base-offset;
bottom: 100%;
}
复制代码
3. 使用
使用时,主要注意点是Hover-tip组件的两个具名插槽的使用。
<fat-hovertip>
<!-- 对应slot name="hover-part" -->
<template slot="hover-part">
<fat-button>组件</fat-button>
</template>
<!-- 对应slot name="tip-part" -->
<template slot="tip-part">向下</template>
</fat-hovertip>
复制代码
往期文章:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 从零实现Vue的组件库(二)-Slider组件实现
- Vue自定义组件(简单实现一个自定义组件)
- 实现一个沙漏⏳组件
- angular 实现下拉列表组件
- 从零实现Vue的组件库(五)- Breadcrumb 实现
- 从零实现Vue的组件库(十)- Select 实现
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
白话大数据与机器学习
高扬、卫峥、尹会生 / 机械工业出版社 / 2016-6 / 69
本书通俗易懂,有高中数学基础即可看懂,同时结合大量案例与漫画,将高度抽象的数学、算法与应用,与现实生活中的案例和事件一一做了关联,将源自生活的抽象还原出来,帮助读者理解后,又带领大家将这些抽象的规律与算法应用于实践,贴合读者需求。同时,本书不是割裂讲解大数据与机器学习的算法和应用,还讲解了其生态环境与关联内容,让读者更全面地知晓渊源与未来,是系统学习大数据与机器学习的不二之选: ·大数据产业......一起来看看 《白话大数据与机器学习》 这本书的介绍吧!