Query在线选座订座(影院篇)

转自:http://www.helloweba.com/view-blog-278.html

我们在线购票时(如电影票、车票等)可以自己选座。开发者会在页面上列出座次席位,用户可以一目了然的看到可以选择的座位及支付。本文以电影院购票为例,为您展示如何选座、处理选座数据等。


在这里,我给大家介绍一款基于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;background-color: #f0f0f0color: #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:26pxfont-size:16pxcolor:#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:leftwidth:90px;margin-top: 10px;line-height: 2;} 
span.seatCharts-legendDescription {margin-left: 5px;line-height: 30px;} 
.checkout-button {display: block;width:80pxheight:24pxline-height:20px;margin: 10px auto;border:1px solid #999;font-size: 14pxcursor:pointer
#selected-seats {max-height: 150px;overflow-y: auto;overflow-x: none;width: 200px;} 
#selected-seats li{float:leftwidth:72pxheight:26pxline-height:26pxborder:1px solid #d3d3d3background:#f7f7f7margin:6pxfont-size:14pxfont-weight:boldtext-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

接下来,helloweba将为您提供jQuery Seat Charts更为丰富的应用实例,我们可使用该插件应用到飞机客舱选座,列车/汽车选座,会议赛事礼堂选座,餐厅饭馆选座等等,更多精彩请关注本站。

声明:本文为原创文章,helloweba.com和作者拥有版权,如需转载,请注明来源于 helloweba.com并保留原文链接:http://www.helloweba.com/view-blog-278.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值