navbar.js 794 B

123456789101112131415161718192021222324252627
  1. var sliderWidth = 96; // 需要设置slider的宽度,用于计算中间位置
  2. Page({
  3. data: {
  4. tabs: ["选项一", "选项二", "选项三"],
  5. activeIndex: 1,
  6. sliderOffset: 0,
  7. sliderLeft: 0
  8. },
  9. onLoad: function () {
  10. var that = this;
  11. wx.getSystemInfo({
  12. success: function(res) {
  13. that.setData({
  14. sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
  15. sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
  16. });
  17. }
  18. });
  19. },
  20. tabClick: function (e) {
  21. this.setData({
  22. sliderOffset: e.currentTarget.offsetLeft,
  23. activeIndex: e.currentTarget.id
  24. });
  25. }
  26. });