jQuery線上選座訂座(影院篇)

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   使用   

我們線上購票時(如電影票、車票等)可以自己選座。開發人員會在頁面上列出座次席位,使用者可以一目瞭然的看到可以選擇的座位及支付。本文以電影院購票為例,為您展示如何選座、處理選座資料等。

在這裡,我給大家介紹一款基於jQuery的線上選座外掛程式:jQuery Seat Charts,它支援自訂座位類型和價格,支援自訂樣式,支援設定不可選的座位,也支援鍵盤控制座位。

點擊這裡查看示範

HTML

我們假設進入電影《星際穿越》的選座頁面,頁面配置請看上面的大圖,頁面左邊將在#seat-map中顯示影院的座位布局圖,右側#booking-details顯示影片相關資訊以及選中的座位資訊#selected-seats和票價金額資訊,選擇座位後確認到支付頁面完成支付。

 
<div class="demo">        <div id="seat-map">         <div class="front">螢幕</div>                         </div>     <div class="booking-details">         <p>影片:<span>星際穿越3D</span></p>         <p>時間:<span>11月14日 21:00</span></p>         <p>座位:</p>         <ul id="selected-seats"></ul>         <p>票數:<span id="counter">0</span></p>         <p>總計:<b>¥<span id="total">0</span></b></p>                              <button class="checkout-button">確定購買</button>                              <div id="legend"></div>     </div> </div> 

  

 
CSS

使用CSS將頁面中的各個元素美化,尤其是座位列表布局,為座位狀態(已售出、可選座位、已選座位等)設定不同的樣式,我們已經整理好CSS代碼,當然你可以根據自己項目頁面風格自己修改任意CSS代碼。

 
.front{width: 300px;margin: 5px 32px 45px 32px;#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 {#B9DEA0;} div.seatCharts-seat.focused {#76B474;border: none;} div.seatCharts-seat.selected {#E6CAC4;} div.seatCharts-seat.unavailable {#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

本執行個體基於jQuery,所以別忘了要先載入jquery庫和選座外掛程式:jQuery Seat Charts。

 
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.seat-charts.min.js"></script> 

  

接下來我們定義好諸如票價,座位區,票數,總計金額之類的元素,然後調用外掛程式:$(‘#seat-map‘).seatCharts()。

我們先設定好座位元影像,一個放映廳的座位是固定好的。在本例中,第三排是過道,以及三四排的右側空位是出口,最後一排我們設定了情侶座,那麼放映廳的布局是這樣的:

aaaaaaaaaa
aaaaaaaaaa
__________
aaaaaaaa__
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aa__aa__aa

我們用字母a表示座位,用符號_表示空的,即沒有座位,當然你也可以用a,b,c等代表不同等級的座位。

然後定義圖例樣式,關鍵是偵探點擊事件click():使用者點擊座位時,如果座位狀態為可選(available),那麼點擊座位後,將座位資訊(幾排幾座)加入到右側的已選座列表中,並計算總票數和總金額;如果座位狀態為已選中(selected),那麼再次點擊座位後,則會將已選中的座位資訊從右側的座位列表中刪除,並將狀態設定可選;如果座位狀態為已售出(unavailable),則不可點擊座位。最後使用get()方法將已售出的座位號狀態設定為已售出。以下是詳細代碼:

 
var price = 80; //票價 $(document).ready(function() {     var $cart = $(‘#selected-seats‘), //座位區     $counter = $(‘#counter‘), //票數     $total = $(‘#total‘); //總計金額          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 () { //點擊事件             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‘) { //已選中                 //更新數量                 $counter.text(sc.find(‘selected‘).length-1);                 //更新總計                 $total.text(recalculateTotal(sc)-price);                                          //刪除已預訂座位                 $(‘#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‘);          }); //計算總金額 function recalculateTotal(sc) {     var total = 0;     sc.find(‘selected‘).each(function () {         total += price;     });                  return total; } 

  

說明

jQuery Seat Charts外掛程式提供了多個選項設定和方法調用,具體可參照項目官網:https://github.com/mateuszmarkowski/jQuery-Seat-Charts。

 

 

jQuery線上選座訂座(影院篇)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.