index.js 2.76 KB
Newer Older
xuzhenghua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
import React, { Component } from 'react';
import './index.scss';

class Banner extends Component {
  navTop = 183
  prevY = 0
  state = {
    navs: [
      {
        text: '积福气',
        id: 'lucky-value'
      },
      {
        text: '幸运大抽奖',
        id: 'lucky-draw'
      },
      {
        text: '预付定金',
        id: 'deposit'
      },
      {
        text: '精品特惠',
        id: 'best-courses'
      },
      {
        text: 'AI测试',
        id: 'ai-test'
      },
      {
        text: '大咖直播',
        id: 'live'
      },
    ],
    index: 0,
  }

  // componentDidMount() {
  //   window.addEventListener('scroll', throttle(this.calcNavActive, 100))
  // }

  // componentWillUnmount() {
  //   window.removeEventListener('scroll', throttle(this.calcNavActive, 100))
  // }


  // calcNavActive = () => {
  //   const {navs, index} = this.state
  //   const y = window.scrollY
  //   let swipeDirection = y > this.prevY ? 'up' : 'down'
  //   let _index
  //   if (swipeDirection === 'up') {
  //     _index = (index + 1) >= navs.length ? index : index + 1

  //   } else {
  //     _index = (index - 1) <= 0 ? 0 : index - 1
  //   }
  //   let el = document.querySelector(`#${navs[_index].id}`)
  //   let nav = document.querySelector('#main-nav')
  //   if(el) {
  //     let top = el.offsetTop

  //     if (y <= this.navTop) {
  //       nav.classList.remove('fixed')
  //     } else {
  //       !nav.classList.contains('fixed') && nav.classList.add('fixed')
  //     }
xuzhenghua committed
67

xuzhenghua committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  //     if (swipeDirection === 'up') {
  //       if (y + 30 + 30 >= top) {
  //         this.setState({
  //           index: _index
  //         })
  //       }
  //     } else {
  //       if (y + 30 + 20 <= top) {
  //         this.setState({
  //           index: _index
  //         })
  //       }
  //     }
  //     this.prevY = y
  //   }
xuzhenghua committed
83

xuzhenghua committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  // }

  // toSection = (i, e) => {
  //   e.preventDefault()
  //   let top = document.querySelector(`#${this.state.navs[i].id}`).offsetTop
  //   this.setState({
  //     index: i
  //   })
  //   window.scrollTo({
  //     top: top - 60,
  //     left: 0
  //   })
  // }


  render() {
xuzhenghua committed
100
    const { toSection, navs, index, isFormal, onlyShow } = this.props;
xuzhenghua committed
101 102
    return (
      <div id={'main-banner'}>
xuzhenghua committed
103
        <div className={`banner ${isFormal ? 'formal_banner' : ''} ${onlyShow ? 'fc_banner' : ''}`}></div>
xuzhenghua committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        <nav id={'main-nav'}>
          <ul>
            {
              navs.map((item, i) => {
                return (
                  <li key={i} className={index === i ? 'active' : ''}>
                    <a href={`#${item.id}`} onClick={(e) => toSection(i, e)}>{item.text}</a>
                  </li>
                )
              })
            }
          </ul>
        </nav>
      </div>
    )
  }
}

export default Banner