Writing A Excellently Useful Navigation Bar(1)

Maybe there are a large number of people have written navigation bar, which is an important part of a website. However, have you written a navigation for single page application? without Bootstrap of another style framework ? this time , I will try to write an excellently useful navigation with React and Sass for you .

First of all,

Navigation.js

renderNav() {
    return this.props.path.map((item, idx) =>
      <Tab
        key={idx}
        active={location.pathname === item.path}
        label={item.label}
        path={item.path}
        updatePointer={this.updatePointer}
      />

    );
  }

render() {
    return (
      <header>
        <div>
          <nav className="navigation" ref="navigation">
            {this.renderNav()}
          </nav>
          <span className="pointer" style={this.state.pointer} />
        </div>
      </header>
    );
  }

it likes that .
I posted the rendering part just now, you known the pointer is also the important part of a navigation, sometimes if the tab is active will the pointer be more lightful or some other signs of it . This time, the pointer is material style, a rectangle under the tab.

Don't fear , it's not difficult like you now envisioning. When actually begin to write something , you will find that easier than you have ever considered. Of course, I mean that when to begin to do it you will touch the real uncomfortable difficulties , and you will try to consider it with your brain your intelligence instead of anxiety.

You must find that we need states to maintain the pointer' s styles .

constructor() {
    super();
    this.state = {
      index: -1,
      pointer: {},
    };
  }

This the initial state, index stands for which tab is active , -1 means nothing active. the object naming pointer is for the style of the pointer.
After writing states within the constructor function , try to think how to know the pointer is in the right place and right styles.

We need a function to get its place and update its styles.

updatePointers(idx) {
    if (idx < 0) {
      this.setState({
        pointer: {
          top: `${this.refs.navigation.getBoundingClientRect().height}px`,
          left: '1500px',
          width: '0px',
        },
      });
      return;
    }
    const label = this.refs.navigation.children[idx].getBoundingClientRect();
    this.setState({
      pointer: {
        top: `${this.refs.navigation.getBoundingClientRect().height}px`,
        left: `${label.left}px`,
        width: `${label.width}px`,
      },
    });
  }

For those codes , we make the pointer go away when the state.index is -1 and use the index to choose the right active tab, then setState will update its style.

It's worth to mention that the getBoundingClientRect(), which function is to grapple the pointer 's position and width for update the pointer style.
Don 't forget to bind the function to its class for geting its context.

that 's all .Now look the Sass file.

.navigation {
  display: flex;
  flex-direction: row;
}

.pointer {
  position: absolute;
  width: 0px;
  height: 3px;
  margin-top: -3px;
  background-color: $button;
  transition-timing-function: $animation-curve-default;
  transition-duration: $animation-duration;
  transition-property: left, width;
}

OK, I will give you more informations next article.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末帽芽,一起剝皮案震驚了整個濱河市逗概,隨后出現(xiàn)的幾起案子裤翩,更是在濱河造成了極大的恐慌享怀,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恃慧,死亡現(xiàn)場離奇詭異园蝠,居然都是意外死亡,警方通過查閱死者的電腦和手機痢士,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門彪薛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人怠蹂,你說我怎么就攤上這事善延。” “怎么了城侧?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵易遣,是天一觀的道長。 經常有香客問我嫌佑,道長豆茫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任屋摇,我火速辦了婚禮揩魂,結果婚禮上,老公的妹妹穿的比我還像新娘炮温。我一直安慰自己火脉,他們只是感情好,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布柒啤。 她就那樣靜靜地躺著忘分,像睡著了一般。 火紅的嫁衣襯著肌膚如雪白修。 梳的紋絲不亂的頭發(fā)上妒峦,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音兵睛,去河邊找鬼肯骇。 笑死,一個胖子當著我的面吹牛祖很,可吹牛的內容都是我干的笛丙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼假颇,長吁一口氣:“原來是場噩夢啊……” “哼胚鸯!你這毒婦竟也來了?” 一聲冷哼從身側響起笨鸡,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤坦冠,失蹤者是張志新(化名)和其女友劉穎哥桥,沒想到半個月后判呕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侠草,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡愧哟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了肥矢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片甘改。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出庆冕,到底是詐尸還是另有隱情访递,我是刑警寧澤惭载,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布抵乓,位于F島的核電站灾炭,受9級特大地震影響蜈出,放射性物質發(fā)生泄漏。R本人自食惡果不足惜偷厦,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦本橙、人聲如沸甚亭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狂鞋。三九已至,卻和暖如春潜的,著一層夾襖步出監(jiān)牢的瞬間骚揍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留信不,地道東北人嘲叔。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像抽活,于是被迫代替她去往敵國和親硫戈。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容