[new feature] IndexBar: add highlight color (#3406)

This commit is contained in:
dongj0316 2019-05-30 15:54:22 +08:00 committed by neverland
parent 5f5de381e8
commit 37ed9bd9ba
3 changed files with 28 additions and 2 deletions

View File

@ -65,6 +65,7 @@ export default {
| index-list | Index List | `Array` | `A-Z` | | index-list | Index List | `Array` | `A-Z` |
| z-index | z-index | `Number` | `1` | | z-index | z-index | `Number` | `1` |
| sticky | Whether to enable anchor sticky top | `Boolean` | `true` | | sticky | Whether to enable anchor sticky top | `Boolean` | `true` |
| highlight-color | Index character highlight color | `String` | - | - |
### IndexAnchor Props ### IndexAnchor Props

View File

@ -18,6 +18,7 @@ export default sfc({
type: Number, type: Number,
default: 1 default: 1
}, },
highlightColor: String,
indexList: { indexList: {
type: Array, type: Array,
default() { default() {
@ -33,6 +34,24 @@ export default sfc({
} }
}, },
data() {
return {
activeAnchorIndex: -1
};
},
computed: {
highlightStyle() {
const { highlightColor } = this;
if (highlightColor) {
/* istanbul ignore else */
return {
color: highlightColor
};
}
}
},
mounted() { mounted() {
this.scroller = getScrollEventTarget(this.$el); this.scroller = getScrollEventTarget(this.$el);
this.handler(true); this.handler(true);
@ -72,6 +91,7 @@ export default sfc({
const active = this.getActiveAnchorIndex(scrollTop, rects); const active = this.getActiveAnchorIndex(scrollTop, rects);
this.activeAnchorIndex = active;
this.children.forEach((item, index) => { this.children.forEach((item, index) => {
if (index === active) { if (index === active) {
item.active = true; item.active = true;
@ -150,8 +170,12 @@ export default sfc({
onTouchend={this.onTouchEnd} onTouchend={this.onTouchEnd}
onTouchcancel={this.onTouchEnd} onTouchcancel={this.onTouchEnd}
> >
{this.indexList.map(index => ( {this.indexList.map((index, idx) => (
<span class={bem('index')} data-index={index}> <span
class={bem('index')}
style={idx === this.activeAnchorIndex ? this.highlightStyle : null}
data-index={index}
>
{index} {index}
</span> </span>
))} ))}

View File

@ -69,6 +69,7 @@ export default {
| index-list | 索引字符列表 | `Array` | `A-Z` | - | | index-list | 索引字符列表 | `Array` | `A-Z` | - |
| z-index | z-index 层级 | `Number` | `1` | - | | z-index | z-index 层级 | `Number` | `1` | - |
| sticky | 是否开启锚点自动吸顶 | `Boolean` | `true` | - | | sticky | 是否开启锚点自动吸顶 | `Boolean` | `true` | - |
| highlight-color | 索引字符高亮颜色 | `String` | - | - |
### IndexAnchor Props ### IndexAnchor Props