เทคนิค สร้าง effect add to cart ด้วย jQuery อย่างง่าย
24 February 2010ตัวอย่างต่อไปนี้เป็นแนวทางสำหรับนำไปปรับใช้ ในการทำระบบ add to cart ด้วย ajax
ตัวอย่าง
HTML Code
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top"><div class="wapProductBox">
<table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" align="center" bgcolor="#CCCC66"> </td>
</tr>
</table>
<input type="button" name="button" id="button" class="toCart"value="Add to Cart" />
</div>
<div class="wapProductBox">
<table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" align="center" bgcolor="#FF9999"> </td>
</tr>
</table>
<input type="button" name="button" id="button" class="toCart"value="Add to Cart" />
</div>
<div class="wapProductBox">
<table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" align="center" bgcolor="#00CCFF"> </td>
</tr>
</tr>
</table>
<input type="button" name="button" id="button" class="toCart"value="Add to Cart" />
</div>
<div class="wapProductBox">
<table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" align="center" bgcolor="#FFCC66"> </td>
</tr>
</tr>
</table>
<input type="button" name="button" id="button" class="toCart"value="Add to Cart" />
</div>
</td>
<td width="200" align="left" valign="top" bgcolor="#F2F2F2">
<form id="form_checkout" name="form_checkout" method="post" action="">
<div id="boxOfProduct">
</div>
</form>
</td>
</tr>
</table>
jQuery Code
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(function(){
$("input.toCart").click(function(){
var nowOffsetX=$(this).parents("div.wapProductBox").offset().left; // หาตำแหน่งสินค้า แกน x
var nowOffsetY=$(this).parents("div.wapProductBox").offset().top; // หาตำแหน่งสินค้า แกน y
var moveOffsetX=$("div#boxOfProduct").offset().left; // หาตำแหน่งตะกร้าสินค้า แกน x
var moveOffsetY=$("div#boxOfProduct").offset().top; // หาตำแหน่งตะกร้าสินค้า แกน y
var nowObjPhoduct=$(this).parents("div.wapProductBox").clone(); // สร้าง object สินค้าใหม่ โดย copy จากตัวเดิม
$(nowObjPhoduct).find("input.toCart").remove(); // เอาปุ่มเพิ่มรายการออก
// จัดตำแปน่งไว้ที่เดิม แต่ซ้อนอยู่ด้านบน
nowObjPhoduct.css({
position:"absolute",
left:nowOffsetX+"px",
top:nowOffsetY+"px",
"z-index":"900"
});
$(document.body).prepend(nowObjPhoduct); // แทรกลงไปในเว็บ
// ย้ายตำแหน่ง ไปยังตะกร้าสินค้า
nowObjPhoduct.animate({
left:moveOffsetX+"px",
top:moveOffsetY+"px",
opacity:0
},500,function(){
// ฟังก์ชันเมื่อย้ายตำแหน่ง เสร็จต้องการให้ทำอะไร
// สามารถนำไปประยุกต์ เพิ่มสินค้าในตะกร้าสินค้า ด้วย ajax
var obj_remove="<button type=button class=removeCart>Remove</button>"; // เพิ่มปุ่มลบรายการ
$("div#boxOfProduct").prepend('<div class="wapProductBox">'+nowObjPhoduct.html()+obj_remove+'</div>');
});
return false;
});
$("button.removeCart").live("click",function(){ // เมื่อคลิกที่ปุ่มลบรายการ
$(this).parents("div.wapProductBox").fadeOut(); // ลบรายการที่เลือก
});
});
</script>
บทความคนเข้าอ่านวันนี้
16 Oct 08 การแสดงเฉพาะปุ่มสำหรับ upload อ่าน 3201 21 Apr 10 สร้างเส้นทาง เพื่อ หาระยะทาง ใน google map ด้วย jQuery อ่าน 7366 08 Jun 09 วิธีการสร้าง preloading images โหลดรูปภาพ ด้วย javascript อย่างง่าย อ่าน 6347 02 Jun 09 แก้ปัญหา การดึงข้อมูล จากฐานข้อมูล เมื่อเปลี่ยน charset ใน แท็ก meta อ่าน 3360 28 Sep 10 สร้างคำสั่ง สำหรับปุ่มควบคุม กำหนดเอง ใน google map อ่าน 1507 06 Nov 10 แนะนำ jQuery UI Effect แบบ ไม่ต้องใช้งานร่วมกับ event show hide และ toggle อ่าน 2549 28 Jun 10 การเลือก หรือ ไม่เลือก checkbox ทั้งหมด และ hilight ด้วย jQuery อย่างง่าย อ่าน 2719 27 Sep 10 วิธีใช้ css จัดรูปแบบ เนื้อหา contents ที่ต้องการแสดง บน google map อ่าน 2706 18 Oct 08 การประยุกต์ใช้ฟังก์ขัน after() กับ text input อ่าน 4549 25 Sep 08 การสร้างวันเวลาปัจจุบัน ด้วยฟังก์ชัน date() อ่าน 5122 13 Sep 10 ประยุกต์ สร้างเครื่องมือ ค้นหา พิกัด จากชื่อสถานที่ จำนวนมาก ใน google map อ่าน 1816 29 Sep 10 แนวทาง การสร้าง ระบบตรวจสอบ การล็อกอิน อย่างง่าย ด้วย ajax ใน jQuery และ php อ่าน 3794 25 Sep 08 การแปลงค่าตัวแปรเป็นตัวแปร string ด้วยฟังก์ชัน toString() อ่าน 3405 30 Oct 08 กำหนดเวลารอดำเนินการ ด้วย javascript อย่างง่าย อ่าน 2719 04 Oct 08 การติดตั้ง MySQL บน Windows อ่าน 2944 10 Nov 08 ฟังก์ชัน PHP ตัดข้อความยาวด้วย wordwrap อ่าน 3591 05 Nov 09 เช็ค บราวเซอร์ Browser ด้วย php อย่างง่าย อ่าน 2114 24 Feb 10 เทคนิค สร้าง effect add to cart ด้วย jQuery อย่างง่าย อ่าน 4467 03 Apr 11 จัดข้อความยาว ให้อยู่ในกรอบ ด้วย css break word อ่าน 3228 08 Nov 09 เทคนิค CSS แสดงข้อมูลแบ่งเป็น คอลัมน์ แทนการใช้ตาราง Table อ่าน 7452
