ตัวแสดงจำนวนสินค้าในตะกร้าสินค้า (Shopping cart Counter)

โดย SONGCHAI SAETERN

Cart counter

การใช้งานไอคอนแสดงตัวนับจำนวนสินค้าในตะกร้าสินค้านี้ จะต้องใช้ Bootstrap Framework เข้ามาช่วยในการจัดรูปแบบด้วย


Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web.


ในส่วนของไอคอนแสดงตัวเลขนั้นจะมีโค้ดอยู่ 2 ส่วนด้วยกัน 


ส่วนที่ 1 CSS

<style>
.cart-box{
position: fixed;
bottom: 40px;
left: 30px;
width: 48px;
height: 48px;
z-index: 2147483000;
cursor: pointer;
background-position: 50%;
}
.btn-circle {
width: 30px;
height: 30px;
text-align: center;
padding: 6px 0;
font-size: 12px;
line-height: 1.428571429;
border-radius: 15px;
}
.btn-circle.btn-lg {
width: 50px;
height: 50px;
padding: 10px 16px;
font-size: 18px;
line-height: 1.33;
border-radius: 25px;
}
.btn-circle.btn-xl {
width: 70px;
height: 70px;
padding: 10px 16px;
font-size: 24px;
line-height: 1.33;
border-radius: 35px;
}
.cart-items-count{
position:relative;
display:flex;
text-align:center;
justify-content: center;
top:-55px;
}
.notification-counter { 
position: absolute;
left: 8px;
background-color: rgba(212, 19, 13, 1);
color: #fff;
border-radius: 3px;
padding: 1px 3px;
font: 8px Verdana;
}
</style>

ส่วนที่ 2 HTML

<div class="cart-box" id="Normal">
<ul class="nav navbar-nav">
<li class="dropdown">
  <button href="#" class="draggable dropdown-toggle btn btn-primary btn-circle btn-xl" data-toggle="dropdown" role="button" aria-expanded="false"> <span class="glyphicon glyphicon-shopping-cart"></span></button>
   <span  class="cart-items-count"><span class=" notification-counter">243</span></span>
</li>
  </ul>
</div>

เมื่อเปิดหน้าเว็บขึ้นมาดูผลลัพธ์จะได้ดังนี้




หากต้องการเปลี่ยนตำแหน่ง ให้กำหนดค่าที่ CSS .cart_box{  ใหม่



ตัวนับจำนวนจะย้ายไปยังตำแหน่งใหม่ที่ต้องการ





โค้ดฉบับเต็มสำหรับทดลอง


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Shopping Cart COUNTER</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<link href="https://getbootstrap.com/docs/3.3/examples/non-responsive/non-responsive.css" rel="stylesheet">
<style>
.cart-box{
position: fixed;
  bottom: 40px;
  left: 30px;
  width: 48px;
  height: 48px;
  z-index: 2147483000;
  cursor: pointer;
  background-position: 50%;
}
.btn-circle {
  width: 30px;
  height: 30px;
  text-align: center;
  padding: 6px 0;
  font-size: 12px;
  line-height: 1.428571429;
  border-radius: 15px;
}
.btn-circle.btn-lg {
  width: 50px;
  height: 50px;
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.33;
  border-radius: 25px;
}
.btn-circle.btn-xl {
  width: 70px;
  height: 70px;
  padding: 10px 16px;
  font-size: 24px;
  line-height: 1.33;
  border-radius: 35px;
}
.cart-items-count{
position:relative;
display:flex;
text-align:center;
justify-content: center;
top:-55px;
}
.notification-counter { 
position: absolute;
left: 8px;
background-color: rgba(212, 19, 13, 1);
color: #fff;
border-radius: 3px;
padding: 1px 3px;
font: 8px Verdana;
}
</style>
    <!-- Custom styles for this template -->
 
</head>
<body>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <!-- The mobile navbar-toggle button can be safely removed since you do not need it in a non-responsive implementation -->
          <a class="navbar-brand" href="#">Shopping Cart</a>
        </div>
        <!-- Note that the .navbar-collapse and .collapse classes have been removed from the #navbar -->
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">Profile</a></li>
            <li><a href="#contact">Order</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown"
  role="button" aria-haspopup="true" aria-expanded="false">Sale <span class="caret"></span></a>
            </li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Search</button>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
 
    <div class="container">
<h1>แสดงตัวนับจำนวนรายการในตะกร้าสินค้า</h1>
<div class="cart-box" id="Normal">
<ul class="nav navbar-nav">
<li class="dropdown">
  <button href="#" class="draggable dropdown-toggle btn btn-primary btn-circle btn-xl" data-toggle="dropdown" role="button" aria-expanded="false"> <span class="glyphicon glyphicon-shopping-cart"></span></button>
   <span  class="cart-items-count"><span class=" notification-counter">243</span></span>
</li>
  </ul>
</div>
    </div> <!-- /container -->
</body>
</html>


ที่มา : https://bootsnipp.com/snippets/ae8qE

PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/