輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)拖放商品放置購(gòu)物車(chē)
在本文中,我們將向您展示如何創(chuàng)建一個(gè)啟用用戶(hù)拖動(dòng)和放置用戶(hù)想買(mǎi)的商品的購(gòu)物車(chē)頁(yè)面,購(gòu)物籃中的物品和價(jià)格將更新,分享給大家,具體內(nèi)容如下:
效果圖:
具體代碼如下
顯示頁(yè)面上的商品:
<ul class="products"> <li> <a href="#" class="item"> <img src="images/shirt1.gif"/> <div> <p>Balloon</p> <p>Price:$25</p> </div> </a> </li> <li> <a href="#" class="item"> <img src="images/shirt2.gif"/> <div> <p>Feeling</p> <p>Price:$25</p> </div> </a> </li> <!-- other products --> </ul>
正如您所看到的上面的代碼,我們添加一個(gè)包含一些 <li> 元素的 <ul> 元素來(lái)顯示商品。所有商品都有名字和價(jià)格屬性,它們包含在<p> 元素中。
創(chuàng)建購(gòu)物車(chē):
<div class="cart"> <h1>Shopping Cart</h1> <table id="cartcontent" style="width:300px;height:auto;"> <thead> <tr> <th field="name" width=140>Name</th> <th field="quantity" width=60 align="right">Quantity</th> <th field="price" width=60 align="right">Price</th> </tr> </thead> </table> <p class="total">Total: $0</p> <h2>Drop here to add to cart</h2> </div>
我們使用數(shù)據(jù)網(wǎng)格(datagrid)來(lái)顯示購(gòu)物籃中的物品。
拖動(dòng)克隆的商品:
$('.item').draggable({ revert:true, proxy:'clone', onStartDrag:function(){ $(this).draggable('options').cursor = 'not-allowed'; $(this).draggable('proxy').css('z-index',10); }, onStopDrag:function(){ $(this).draggable('options').cursor='move'; } });
請(qǐng)注意,我們把 draggable 屬性的值從 'proxy' 設(shè)置為 'clone',所以拖動(dòng)元素將由克隆產(chǎn)生。
放置選擇商品到購(gòu)物車(chē)中
$('.cart').droppable({ onDragEnter:function(e,source){ $(source).draggable('options').cursor='auto'; }, onDragLeave:function(e,source){ $(source).draggable('options').cursor='not-allowed'; }, onDrop:function(e,source){ var name = $(source).find('p:eq(0)').html(); var price = $(source).find('p:eq(1)').html(); addProduct(name, parseFloat(price.split('$')[1])); } }); var data = {"total":0,"rows":[]}; var totalCost = 0; function addProduct(name,price){ function add(){ for(var i=0; i<data.total; i++){ var row = data.rows[i]; if (row.name == name){ row.quantity += 1; return; } } data.total += 1; data.rows.push({ name:name, quantity:1, price:price }); } add(); totalCost += price; $('#cartcontent').datagrid('loadData', data); $('div.cart .total').html('Total: $'+totalCost); }
每當(dāng)放置商品的時(shí)候,我們首先得到商品名稱(chēng)和價(jià)格,然后調(diào)用 'addProduct' 函數(shù)來(lái)更新購(gòu)物籃。
EasyUI實(shí)現(xiàn)拖放商品放置購(gòu)物車(chē)的功能就介紹到這,有了本文為大家提供的實(shí)例,相信大家應(yīng)該很輕松的就可以實(shí)現(xiàn)拖放商品放置購(gòu)物車(chē)的模塊設(shè)計(jì),謝謝大家的閱讀。
- Android自定義商品購(gòu)買(mǎi)數(shù)量加減控件
- JavaScript實(shí)現(xiàn)仿淘寶商品購(gòu)買(mǎi)數(shù)量的增減效果
- JQuery實(shí)現(xiàn)的購(gòu)物車(chē)功能(可以減少或者添加商品并自動(dòng)計(jì)算價(jià)格)
- Android仿美團(tuán)下拉菜單(商品選購(gòu))實(shí)例代碼
- jQuery 浮動(dòng)導(dǎo)航菜單適合購(gòu)物商品類(lèi)型的網(wǎng)站
- Android實(shí)現(xiàn)仿淘寶購(gòu)物車(chē)增加和減少商品數(shù)量功能demo示例
- js實(shí)現(xiàn)商品拋物線(xiàn)加入購(gòu)物車(chē)特效
- 基于PHP實(shí)現(xiàn)假裝商品限時(shí)搶購(gòu)繁忙的效果
- 基于JavaScript實(shí)現(xiàn)購(gòu)物網(wǎng)站商品放大鏡效果
- mvc架構(gòu)實(shí)現(xiàn)商品的購(gòu)買(mǎi)(二)
相關(guān)文章
jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法
本文給大家介紹jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法,涉及到uploadify上傳錯(cuò)誤302相關(guān)問(wèn)題,對(duì)本文感興趣的朋友一起看看吧2015-12-12jQuery實(shí)現(xiàn)橫向帶緩沖的水平運(yùn)動(dòng)效果(附demo源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)橫向帶緩沖的水平運(yùn)動(dòng)效果,涉及jQuery中鼠標(biāo)事件及animate方法使用技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-01-01jQuery實(shí)現(xiàn)在下拉列表選擇時(shí)獲取json數(shù)據(jù)的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)在下拉列表選擇時(shí)獲取json數(shù)據(jù)的方法,主要涉及jQuery中g(shù)etJSON方法的使用技巧,需要的朋友可以參考下2015-04-04iframe中使用jquery進(jìn)行查找的方法【案例分析】
這篇文章主要介紹了iframe中使用jquery進(jìn)行查找的方法,結(jié)合實(shí)際案例形式較為詳細(xì)的分析了jQuery結(jié)合iframe查找的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06jquery簡(jiǎn)單的拖動(dòng)效果實(shí)現(xiàn)原理及示例
本文為大家詳細(xì)介紹下jQuery拖曵的簡(jiǎn)單實(shí)例,具體的實(shí)現(xiàn)思路及代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07