基于jquery實(shí)現(xiàn)在線選座訂座之影院篇
先給大家展示效果圖(支持源碼下載哦):

我們?cè)诰€購(gòu)票時(shí)(如電影票、車票等)可以自己選座。開發(fā)者會(huì)在頁面上列出座次席位,用戶可以一目了然的看到可以選擇的座位及支付。本文以電影院購(gòu)票為例,為您展示如何選座、處理選座數(shù)據(jù)等。
在這里,我給大家介紹一款基于jQuery的在線選座插件:jQuery Seat Charts,它支持自定義座位類型和價(jià)格,支持自定義樣式,支持設(shè)置不可選的座位,也支持鍵盤控制座位。
HTML
我們假設(shè)進(jìn)入電影《星際穿越》的選座頁面,頁面布局請(qǐng)看上面的大圖,頁面左邊將在#seat-map中顯示影院的座位布局圖,右側(cè)#booking-details顯示影片相關(guān)信息以及選中的座位信息#selected-seats和票價(jià)金額信息,選擇座位后確認(rèn)到支付頁面完成支付。
<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將頁面中的各個(gè)元素美化,尤其是座位列表布局,為座位狀態(tài)(已售出、可選座位、已選座位等)設(shè)置不同的樣式,我們已經(jīng)整理好CSS代碼,當(dāng)然你可以根據(jù)自己項(xiàng)目頁面風(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庫和選座插件: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的計(jì)算文本框字?jǐn)?shù)的代碼
用戶邊輸入計(jì)算同時(shí)進(jìn)行,告訴用戶還剩余多少可輸入的字?jǐn)?shù),當(dāng)超過規(guī)定的字?jǐn)?shù)后,點(diǎn)擊確定,會(huì)讓輸入框閃動(dòng)2012-06-06
基于jquery css3實(shí)現(xiàn)點(diǎn)擊動(dòng)畫彈出表單源碼特效
這篇文章主要介紹了基于jquery css3實(shí)現(xiàn)點(diǎn)擊動(dòng)畫彈出表單,彈出的表單沒有任何遮蓋層,在web前端程序開發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下2015-08-08
基于jQuery實(shí)現(xiàn)美觀且實(shí)用的倒計(jì)時(shí)實(shí)例代碼
倒計(jì)時(shí)效果在我們的日常生活中經(jīng)常會(huì)用到,比如說高考倒計(jì)時(shí)、元旦放假倒計(jì)時(shí)、春節(jié)放假倒計(jì)時(shí)等等,下面通過本篇文章給大家分享基于jQuery實(shí)現(xiàn)美觀且實(shí)用的倒計(jì)時(shí)實(shí)例代碼,對(duì)jquery倒計(jì)時(shí)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
JQ獲取動(dòng)態(tài)加載的圖片大小的正確方法分享
這篇文章介紹了JQ獲取動(dòng)態(tài)加載的圖片大小的正確方法,有需要的朋友可以參考一下2013-11-11
使用jquery判斷一個(gè)元素是否含有一個(gè)指定的類(class)實(shí)例
下面小編就為大家?guī)硪黄褂胘query判斷一個(gè)元素是否含有一個(gè)指定的類(class)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

