[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

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