本篇內(nèi)容主要講解“怎么用jQuery實(shí)現(xiàn)電影院在線選座訂座功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用jQuery實(shí)現(xiàn)電影院在線選座訂座功能”吧!
代碼如下:
我們假設(shè)進(jìn)入電影《星際穿越》的選座頁(yè)面,頁(yè)面布局請(qǐng)看上面的大圖,頁(yè)面左邊將在#seat-map中顯示影院的座位布局圖,右側(cè)#booking-details顯示影片相關(guān)信息以及選中的座位信息#selected-seats和票價(jià)金額信息,選擇座位后確認(rèn)到支付頁(yè)面完成支付。
<p class="demo">
<p id="seat-map">
<p class="front">屏幕</p>
</p>
<p 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>
<p id="legend"></p>
</p>
</p>
使用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 h4 {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}
p.seatCharts-cell {color: #182C4E;height: 25px;width: 25px;line-height: 25px;margin: 3px;float: left;text-align: center;outline: none;font-size: 13px;}
p.seatCharts-seat {color: #fff;cursor: pointer;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius: 5px;}
p.seatCharts-row {height: 35px;}
p.seatCharts-seat.available {background-color: #B9DEA0;}
p.seatCharts-seat.focused {background-color: #76B474;border: none;}
p.seatCharts-seat.selected {background-color: #E6CAC4;}
p.seatCharts-seat.unavailable {background-color: #472B34;cursor: not-allowed;}
p.seatCharts-container {border-right: 1px dotted #adadad;width: 400px;padding: 20px;float: left;}
p.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}
本實(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;
}
到此,相信大家對(duì)“怎么用jQuery實(shí)現(xiàn)電影院在線選座訂座功能”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文標(biāo)題:怎么用jQuery實(shí)現(xiàn)電影院在線選座訂座功能-創(chuàng)新互聯(lián)
本文路徑:http://jinyejixie.com/article28/dsedcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、定制網(wǎng)站、全網(wǎng)營(yíng)銷推廣、小程序開發(fā)、App設(shè)計(jì)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容