Skip to content

Commit

Permalink
1.增加搜索关键字高亮2.诗句详情页tab切换增加动画效果
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteMu committed Apr 6, 2019
1 parent c7ce92b commit 354b9f3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 24 deletions.
69 changes: 49 additions & 20 deletions pages/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,40 @@ Page({
searchPoem(key){
let _this = this;
let resultList = [];
let originList = poemList;
let originList = JSON.parse(JSON.stringify(poemList));
for (var i = 0; i < originList.length; i++){
let poem = originList[i];
if(_this.poemMatchKey(poem,key)){
resultList.push(poem);
//处理高亮数据
let data = _this.handleHighlightKeyword(poem, key);
resultList.push(data);
}
}
return resultList;
},
//处理高亮数据
handleHighlightKeyword(data, key){
let _this = this;
let titleArr = data.title.split(key);
let title = data.title.indexOf(key) != -1?_this.handleHighlightArr(titleArr, key): titleArr;
let authorArr = data.author.split(key);
let author = data.author.indexOf(key) != -1 ?_this.handleHighlightArr(authorArr, key): authorArr;
let summaryArr = data.summary.split(key);
let summary = data.summary.indexOf(key) != -1 ?_this.handleHighlightArr(summaryArr, key): summaryArr;
data.title = title;
data.author = author;
data.summary = summary;
return data;
},
handleHighlightArr(list, key){
let arr = [];
list.forEach((item, index) => {
arr.push(item);
if(index != list.length-1)
arr.push(key)
})
return arr;
},
// 判断一首诗是否命中关键词
poemMatchKey(poem,key){
if(poem.title.indexOf(key) != -1) {
Expand All @@ -47,27 +72,31 @@ Page({
_this.goPage(url);
},
getKeyword(e){
let _this = this, poemData;
if (e.detail.value){
poemData = _this.searchPoem(e.detail.value);
_this.setData({
keyword: e.detail.value
})
}else{
poemData = poemList
}
let _this = this, poemData,
keyword = e.detail.value.trim();
_this.setData({
listData: poemData
})
if(_this.data.listData.length == 0){
_this.setData({
noData: true
})
}else{
keyword
},()=>{
if (keyword) {
poemData = _this.searchPoem(keyword);
} else {
poemData = poemList
}
_this.setData({
noData: false
listData: poemData
}, () => {
if (_this.data.listData.length == 0) {
_this.setData({
noData: true
})
} else {
_this.setData({
noData: false
})
}
})
}
})

},
goPage(url){
wx.navigateTo({
Expand Down
16 changes: 15 additions & 1 deletion pages/index/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</view>
<view style="height: 108rpx;"></view>
<view class="content" wx:if="{{!noData}}">
<view class="list" wx:for="{{listData}}" wx:key="index" data-data="{{item}}" bindtap="goDetail">
<view class="list" wx:if="{{!keyword}}" wx:for="{{listData}}" wx:key="index" data-data="{{item}}" bindtap="goDetail">
<view class="left">
<view class="title">{{item.title}}</view>
<view class="sub-title">{{item.summary}}</view>
Expand All @@ -17,6 +17,20 @@
<view class="des">[{{item.dynasty}}]{{item.author}}</view>
</view>
</view>
<view class="list" wx:if="{{keyword}}" wx:for="{{listData}}" wx:key="index" data-data="{{item}}" bindtap="goDetail">
<view class="left">
<view class="title">
<text wx:if="{{value}}" wx:for="{{item.title}}" wx:key="key" wx:for-item="value" class="{{value == keyword? 'active': ''}}">{{value}}</text>
</view>
<view class="sub-title">
<text wx:if="{{value}}" wx:for="{{item.summary}}" wx:key="key" wx:for-item="value" class="{{value == keyword? 'active': ''}}">{{value}}</text>
</view>
</view>
<view class="right">
<view class="des">[{{item.dynasty}}]<text wx:if="{{value}}" wx:for="{{item.author}}" wx:key="key" wx:for-item="value" class="{{value == keyword? 'active': ''}}">{{value}}</text>
</view>
</view>
</view>
<view class="list-tips">已经到底了哟~</view>
</view>
<view class="no-data" wx:else>
Expand Down
3 changes: 3 additions & 0 deletions pages/index/index.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
text-overflow:ellipsis;
white-space: nowrap;
}
.content .list .active{
color: #ff4c00!important;
}
.content .list .left .sub-title{
font-size: 24rpx;
color: #666;
Expand Down
13 changes: 11 additions & 2 deletions pages/poemDetail/poemDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ Page({
*/
selectOption(e){
let _this = this,
optionid = e.currentTarget.dataset.optionid;
optionid = e.currentTarget.dataset.optionid,
left = "calc(33.3% * "+optionid+" + 16.6% - 60rpx)",
animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0,
transformOrigin: "50% 50%"
});
animation.left(left).step();
_this.setData({
optionid
optionid,
animationData: animation.export()
});
},
/**
Expand Down
1 change: 1 addition & 0 deletions pages/poemDetail/poemDetail.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<view class="item-des {{optionid == 2?'active':''}}">赏析</view>
</view>
</view>
<view class="option-line" animation="{{animationData}}"></view>
<view class="appreciation-content" wx:if="{{optionid == 0}}">
<view wx:for="{{annotation}}" wx:key="index">
<text class="sp">{{item.sp}}</text><text>{{item.def}}</text>
Expand Down
10 changes: 9 additions & 1 deletion pages/poemDetail/poemDetail.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
/**注释部分**/
.appreciation {
width: 100%;
position: relative;
}
.appreciation .option-line{
height: 6rpx;
width: 120rpx;
background: #ff4c00;
position: absolute;
top: 122rpx;
left: calc(16.6% - 60rpx);
}
.appreciation .option-bar{
width: 100%;
Expand Down Expand Up @@ -51,7 +60,6 @@
}
.appreciation .option-bar .item .active{
color: #ff4c00;
border-bottom: 6rpx solid #ff4c00;
}
.appreciation .appreciation-content{
font-size: 30rpx;
Expand Down

0 comments on commit 354b9f3

Please sign in to comment.