基于jquery實(shí)現(xiàn)在線選座訂座之影院篇
先給大家展示效果圖(支持源碼下載哦):
我們?cè)诰€購(gòu)票時(shí)(如電影票、車票等)可以自己選座。開發(fā)者會(huì)在頁(yè)面上列出座次席位,用戶可以一目了然的看到可以選擇的座位及支付。本文以電影院購(gòu)票為例,為您展示如何選座、處理選座數(shù)據(jù)等。
在這里,我給大家介紹一款基于jQuery的在線選座插件:jQuery Seat Charts,它支持自定義座位類型和價(jià)格,支持自定義樣式,支持設(shè)置不可選的座位,也支持鍵盤控制座位。
HTML
我們假設(shè)進(jìn)入電影《星際穿越》的選座頁(yè)面,頁(yè)面布局請(qǐng)看上面的大圖,頁(yè)面左邊將在#seat-map中顯示影院的座位布局圖,右側(cè)#booking-details顯示影片相關(guān)信息以及選中的座位信息#selected-seats和票價(jià)金額信息,選擇座位后確認(rèn)到支付頁(yè)面完成支付。
<div class="demo"> <div id="seat-map"> <div class="front">屏幕</div> </div> <div class="booking-details"> <p>影片:<span>星際穿越3D</span></p> <p>時(shí)間:<span>11月14日 21:00</span></p> <p>座位:</p> <ul id="selected-seats"></ul> <p>票數(shù):<span id="counter">0</span></p> <p>總計(jì):<b>¥<span id="total">0</span></b></p> <button class="checkout-button">確定購(gòu)買</button> <div id="legend"></div> </div> </div>
CSS
使用CSS將頁(yè)面中的各個(gè)元素美化,尤其是座位列表布局,為座位狀態(tài)(已售出、可選座位、已選座位等)設(shè)置不同的樣式,我們已經(jīng)整理好CSS代碼,當(dāng)然你可以根據(jù)自己項(xiàng)目頁(yè)面風(fēng)格自己修改任意CSS代碼。
.front{width: 300px;margin: 5px 32px 45px 32px;background-color: #f0f0f0; color: #666;text-align: center;padding: 3px;border-radius: 5px;} .booking-details {float: right;position: relative;width:200px;height: 450px; } .booking-details h3 {margin: 5px 5px 0 0;font-size: 16px;} .booking-details p{line-height:26px; font-size:16px; color:#999} .booking-details p span{color:#666} div.seatCharts-cell {color: #182C4E;height: 25px;width: 25px;line-height: 25px;margin: 3px;float: left;text-align: center;outline: none;font-size: 13px;} div.seatCharts-seat {color: #fff;cursor: pointer;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius: 5px;} div.seatCharts-row {height: 35px;} div.seatCharts-seat.available {background-color: #B9DEA0;} div.seatCharts-seat.focused {background-color: #76B474;border: none;} div.seatCharts-seat.selected {background-color: #E6CAC4;} div.seatCharts-seat.unavailable {background-color: #472B34;cursor: not-allowed;} div.seatCharts-container {border-right: 1px dotted #adadad;width: 400px;padding: 20px;float: left;} div.seatCharts-legend {padding-left: 0px;position: absolute;bottom: 16px;} ul.seatCharts-legendList {padding-left: 0px;} .seatCharts-legendItem{float:left; width:90px;margin-top: 10px;line-height: 2;} span.seatCharts-legendDescription {margin-left: 5px;line-height: 30px;} .checkout-button {display: block;width:80px; height:24px; line-height:20px;margin: 10px auto;border:1px solid #999;font-size: 14px; cursor:pointer} #selected-seats {max-height: 150px;overflow-y: auto;overflow-x: none;width: 200px;} #selected-seats li{float:left; width:72px; height:26px; line-height:26px; border:1px solid #d3d3d3; background:#f7f7f7; margin:6px; font-size:14px; font-weight:bold; text-align:center}
jQuery
本實(shí)例基于jQuery,所以別忘了要先加載jquery庫(kù)和選座插件:jQuery Seat Charts。
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.seat-charts.min.js"></script>
接下來我們定義好諸如票價(jià),座位區(qū),票數(shù),總計(jì)金額之類的元素,然后調(diào)用插件:$('#seat-map').seatCharts()。
我們先設(shè)置好座位圖,一個(gè)放映廳的座位是固定好的。在本例中,第三排是過道,以及三四排的右側(cè)空位是出口,最后一排我們?cè)O(shè)置了情侶座,那么放映廳的布局是這樣的:
aaaaaaaaaa
aaaaaaaaaa
__________
aaaaaaaa__
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aa__aa__aa
我們用字母a表示座位,用符號(hào)_表示空的,即沒有座位,當(dāng)然你也可以用a,b,c等代表不同等級(jí)的座位。
然后定義圖例樣式,關(guān)鍵是偵探點(diǎn)擊事件click():用戶點(diǎn)擊座位時(shí),如果座位狀態(tài)為可選(available),那么點(diǎn)擊座位后,將座位信息(幾排幾座)加入到右側(cè)的已選座列表中,并計(jì)算總票數(shù)和總金額;如果座位狀態(tài)為已選中(selected),那么再次點(diǎn)擊座位后,則會(huì)將已選中的座位信息從右側(cè)的座位列表中刪除,并將狀態(tài)設(shè)置可選;如果座位狀態(tài)為已售出(unavailable),則不可點(diǎn)擊座位。最后使用get()方法將已售出的座位號(hào)狀態(tài)設(shè)置為已售出。
以下是詳細(xì)代碼:
var price = 80; //票價(jià) $(document).ready(function() { var $cart = $('#selected-seats'), //座位區(qū) $counter = $('#counter'), //票數(shù) $total = $('#total'); //總計(jì)金額 var sc = $('#seat-map').seatCharts({ map: [ //座位圖 'aaaaaaaaaa', 'aaaaaaaaaa', '__________', 'aaaaaaaa__', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aa__aa__aa' ], legend : { //定義圖例 node : $('#legend'), items : [ [ 'a', 'available', '可選座' ], [ 'a', 'unavailable', '已售出'] ] }, click: function () { //點(diǎn)擊事件 if (this.status() == 'available') { //可選座 $('<li>'+(this.settings.row+1)+'排'+this.settings.label+'座</li>') .attr('id', 'cart-item-'+this.settings.id) .data('seatId', this.settings.id) .appendTo($cart); $counter.text(sc.find('selected').length+1); $total.text(recalculateTotal(sc)+price); return 'selected'; } else if (this.status() == 'selected') { //已選中 //更新數(shù)量 $counter.text(sc.find('selected').length-1); //更新總計(jì) $total.text(recalculateTotal(sc)-price); //刪除已預(yù)訂座位 $('#cart-item-'+this.settings.id).remove(); //可選座 return 'available'; } else if (this.status() == 'unavailable') { //已售出 return 'unavailable'; } else { return this.style(); } } }); //已售出的座位 sc.get(['1_2', '4_4','4_5','6_6','6_7','8_5','8_6','8_7','8_8', '10_1', '10_2']).status('unavailable'); }); //計(jì)算總金額 function recalculateTotal(sc) { var total = 0; sc.find('selected').each(function () { total += price; }); return total; }
說明
jQuery Seat Charts插件提供了多個(gè)選項(xiàng)設(shè)置和方法調(diào)用,具體可參照項(xiàng)目官網(wǎng):
https://github.com/mateuszmarkowski/jQuery-Seat-Charts。
以上代碼使用jquery實(shí)現(xiàn)在線選座訂座之影院篇,希望大家喜歡。
相關(guān)文章
JQuery實(shí)現(xiàn)的圖文自動(dòng)輪播效果插件
這篇文章主要介紹了JQuery實(shí)現(xiàn)的圖文自動(dòng)輪播效果插件,涉及jQuery處理鼠標(biāo)事件及動(dòng)畫效果的方法,需要的朋友可以參考下2015-06-06jQuery動(dòng)態(tài)添加及刪除表單上傳元素的方法(附demo源碼下載)
這篇文章主要介紹了jQuery動(dòng)態(tài)添加及刪除表單上傳元素的方法,涉及jQuery的事件動(dòng)態(tài)綁定及頁(yè)面元素動(dòng)態(tài)操作技巧,并附帶了demo源碼供讀者下載參考,代碼非常簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2016-01-01jQuery ''行 4954 錯(cuò)誤: 不支持該屬性或方法'' 的問題解決方法
這個(gè)問題只在IE下出現(xiàn)。詭異的是,對(duì)于出現(xiàn)這個(gè)問題的頁(yè)面,重新刷新一下就又好了,Ajax 工作一切正常。順便說一下,我的 jQuery 版本是 1.4.2。2011-01-01內(nèi)容滑動(dòng)切換效果jquery.hwSlide.js插件封裝
這篇文章主要為大家詳細(xì)介紹了jQuery開發(fā)之內(nèi)容滑動(dòng)切換效果的相關(guān)資料,jquery.hwSlide.js插件進(jìn)行封裝具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07jQuery mobile類庫(kù)使用時(shí)加載導(dǎo)航歷史的方法簡(jiǎn)介
這篇文章主要介紹了jQuery mobile開發(fā)中加載導(dǎo)航歷史的方法,jQuery mobile是jQuery針對(duì)移動(dòng)設(shè)備開發(fā)的JavaScript庫(kù),需要的朋友可以參考下2015-12-12