สอนสร้าง button navigation bar และเขียน script ให้เรียกหน้าอื่นๆ มาแสดง

 



    สวัสดีครับ ในบทความนี้เราจะมาสอนสร้าง Button navigation bar  ครับเพื่อน และเมื่อมีผู้ใช้คลิกที่ปุ่มจะให้เปิดหน้าขึ้นมาแสดงโดยไม่ต้องใส่ href link ครับ 

 และสามารถดูโค้ดตัวอย่างด้านล่างนี้เลยนะครับ


<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Button navigation bar</title>
  <link rel="stylesheet" href="/button/style.css">
</head>

<body>
  <section class="home">
    <div class="section-home">
      <h2>เปิดหน้าหลักแล้ว</h2>
    </div>
  </section>
  
  <section class="shop">
    <div class="section-shop">
    <h2>เปิดหน้าช้อปแล้ว</h2>
    </div>
  </section>
  
  <section class="wallet">
    <div class="section-wallet">
    <h2>เปิดหน้ากระเป๋าแล้ว</h2>
    </div>
  </section>
  
  <section class="user">
    <div class="section-user">
    <h2>เปิดหน้าโปรไฟล์แล้ว</h2>
    </div>
  </section>
  
  <nav class="navbar" id="myDIV">
    <ul class="nav-content">
      <li class="nav-list">
        <a href="#home" class="link-item home-link">
          <img src="/app/home.png" alt="">
          <span class="link-text">หน้าหลัก</span>
        </a>
      </li>
      <li class="nav-list">
        <a href="#shop" class="link-item shop-link">
          <img class="link-icon" src="/app/shop.png" alt="">
          <span class="link-text">ช้อป</span>
        </a>
      </li>
      <li class="nav-list">
        <a href="#upload" class="link-item actives">
          <img class="link-icon" src="/app/plus.png">
          <span class="link-text"></span>
        </a>
      </li>
      <li class="nav-list">
        <a href="#wallet" class="link-item wallet-link">
          <img src="/app/wallet.png" alt="">
          <span class="link-text">กระเป๋า</span>
        </a>
      </li>
      <li class="nav-list">
        <a href="#user" class="link-item  user-link">
          <img class="link-icon" src="/app/hi.png" alt="">
          <span class="link-text">โปรไฟล์</span>
        </a>
      </li>
  
      <span class="indicator"></span>
    </ul>
  </nav>
  
  
  <script src="/button/index.js"></script>
</body>

</html>


ในส่วนโค้ด HTML นี้จะสร้างโครงขึ้นมาและกำลังหนด section ต่างๆ ในแต่ล่ะ section ก็จะมี class เพื่อให้เราสามารถเขียนในส่วน JavaScript ให้สามารถเปิดหน้าต่างได้ ในโค้ดนี้ผมได้กำหนดให้ section มี 4 หน้านะครับ 

โค้ด CSS


 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
67
68
69
70
71
72
73
74
75
76
77
@import url('https://fonts.googleapis.com/css2?family=Kanit&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Kanit', sans-serif;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
}

li {
  margin: 3px;
  padding: 10px;
}

li a {
  color: black;
  text-decoration: none;
  display: block;
  padding: 0;
}

.navbar {
  border-radius: 5px 5px 0 0;
  z-index: fixed;
  position: absolute;
  bottom: 0px;
  background: #FFF;
  width: 100%;
  padding: 0 10px;
}

.link-item {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 50px;
  width: 100%;
  text-decoration: none;
  transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.link-item.active{
    transform: translateY(-10px);
}
.link-icon{
    font-size: 20px;
    color: #999;
    transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.navbar img {
  height: 20px;
  width: 20px;
  font-size: 20px;
}

.section-home, .section-shop, .section-wallet, .section-user {
  height: 95vh;
  background-color: #ddd;
  position: relative;
}

.shop, .wallet, .user {
  display: none;
}

h2 {
  text-align: center;
}

 ในส่วนบรรทัดที่ 71 นั้น จะซ่อนหน้า shop wallet user ได้ทีการกำหนดเป็น none และเหลือหน้า home ที่ยังไม่ได้กำหนดเพราะหน้าแรกจะเป็นในส่วนของหน้าหลักของเว็บไซต์เรา 

 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
const homeLink = document.querySelector('.home-link');
const shopLink = document.querySelector('.shop-link');
const walletLink = document.querySelector('.wallet-link');
const userLink = document.querySelector('.user-link');


const homeSection = document.querySelector('.home');
const shopSection = document.querySelector('.shop');
const walletSection = document.querySelector('.wallet');
const userSection = document.querySelector('.user');

homeLink.addEventListener('click', () => {
  homeSection.style.display = 'block';
  shopSection.style.display = 'none';
  walletSection.style.display = 'none';
  userSection.style.display = 'none';
});

shopLink.addEventListener('click', () => {
  homeSection.style.display = 'none';
  shopSection.style.display = 'block';
  walletSection.style.display = 'none';
  userSection.style.display = 'none';
});

walletLink.addEventListener('click', () => {
  homeSection.style.display = 'none';
  shopSection.style.display = 'none';
  walletSection.style.display = 'block';
  userSection.style.display = 'none';
});

userLink.addEventListener('click', () => {
  homeSection.style.display = 'none';
  shopSection.style.display = 'none';
  walletSection.style.display = 'none';
  userSection.style.display = 'block';
});

 และโค้ด JavaScript นี้จะมีการกำหนดหน้าต่างๆ เมื่อปุ่ม home ถูกคลิกจะต้องซ่อนหน้าอื่นๆ และให้แสดงหน้าหลักแทน

1 ความคิดเห็น

สวัสดีนี่คือฟอร์มความคิดเห็นชุมชน

แสดงความคิดเห็น
ใหม่กว่า เก่ากว่า