//index.js
//获取应用实例
const app = getApp()
Page({
data: {
navData:[
{
text: '推荐'
},
{
text: '视频'
},
{
text: '热点'
},
{
text: '本地'
},
{
text: '社会'
},
{
text: '娱乐'
},
{
text: '科技'
},
{
text: '汽车'
},
{
text: '体育'
},
{
text: '财经'
},
{
text: '军事'
},
{
text: '国际'
},
{
text: '时尚'
},
{
text: '游戏'
},
{
text: '美文'
}
],
// 窗口的宽度、高度值
winWidth: 0,
winHeight: 0,
currentTab: 0,
navScrollLeft: 0
},
// 打开 Search 搜索页面
showSearch: function () {
wx.navigateTo({
url: '../search/search',
})
},
// 打开 Menu 导航菜单选择页面
showMenu: function () {
wx.navigateTo({
url: '../menu/menu',
})
},
// 打开 My 个人中心页面
showMy: function () {
wx.navigateTo({
url: '../my/my',
})
},
// 事件处理函数
onLoad: function () {
var that = this;
// 窗口高度自适应
wx.getSystemInfo({
success: function (res) {
that.setData({
winWidth: res.windowWidth,
winHeight: res.windowHeight
});
}
});
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
wx.getSystemInfo({
success: (res) => {
this.setData({
pixelRatio: res.pixelRatio,
windowHeight: res.windowHeight,
windowWidth: res.windowWidth
})
}
})
},
switchNav(event){
var cur = event.currentTarget.dataset.current;
//每个tab选项宽度占1/5
var singleNavWidth = this.data.windowWidth / 5;
//tab选项居中
this.setData({
navScrollLeft: (cur - 2) * singleNavWidth
})
if (this.data.currentTab == cur) {
return false;
} else {
this.setData({
currentTab: cur,
})
}
},
switchTab(event){
var cur = event.detail.current;
var singleNavWidth = this.data.windowWidth / 5;
this.setData({
currentTab: cur,
navScrollLeft: (cur - 2) * singleNavWidth,
});
},
// 滑动页面时绑定 tab 菜单同时滑动
bindChange: function (e) {
var that = this;
that.setData({
currentTab: e.detail.current
});
}
})