项目工程结构图
JQuery插件库
网址
jQuery在线选座位插件seat-charts特效代码:
这是一款基于JQuery实现的在线选座位插件seat-charts源码,是一款适合机票,电影票,客车票选座的jquery.seat-charts插件。点击左侧的座位即可在右侧即时显示座位信息,并且可以有计算累加的功能。
代码模块分析
- html主体
<div class="container">
<div class="demo clearfix">
<!---左边座位列表----->
<div id="seat_area">
<div class="front">屏幕</div>
</div>
<!---右边选座信息----->
<div class="booking_area">
<p>电影:<span>我和我的祖国</span></p>
<p>时间:<span>10月1日 14:00</span></p>
<p>座位:</p>
<ul id="seats_chose"></ul>
<p>票数:<span id="tickects_num">0</span></p>
<p>总价:<b>¥<span id="total_price">0</span></b></p>
<input type="button" class="btn" value="确定购买"/>
<div id="legend"></div> <!-- 显示可选的座位和已售出的座位标识 -->
</div>
</div>
</div>
网页效果图:
分析:
首先先确定好基本的HTML主体页面。先写一个容器,在容器中再写一个清除浮动的A块儿,在A块中又分为左右两个块(B块和C块);左边放置座位图显示,右边放置影片和购票等信息;同时给有需要增加样式的标签添加选择器。
- 添加Css样式
<style type="text/css">
.front{width: 300px;margin: 5px 32px 45px 32px;background-color: #f0f0f0; color: #666;text-align: center;padding: 3px;border-radius: 5px;}
.booking_area {float: right;position: relative;width:200px;height: 450px; }
.booking_area h3 {margin: 5px 5px 0 0;font-size: 16px;}
.booking_area p{line-height:26px; font-size:16px; color:#999}
.booking_area 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}
#seats_chose {max-height: 150px;overflow-y: auto;overflow-x: none;width: 200px;}
#seats_chose 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}
</style>
网页效果图:
- 添加JQuery插件库
<link rel="stylesheet" type="text/css" href="css/jq22.css" />
网页效果图:
- 添加Jquery夹包,jQuery在线选座位插件seat-charts,JavaScript语句
<script src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="js/jquery.seat-charts.min.js"></script>
<script type="text/javascript">
var price = 100; //电影票价
$(document).ready(function() {
var $cart = $('#seats_chose'); //座位区
var $tickects_num = $('#tickects_num'); //票数
var $total_price = $('#total_price'); //票价总额
var sc = $('#seat_area').seatCharts({
map:
[
//座位结构图 a代表座位 _代表过道
'aaaaaaaaaa',
'aaaaaaaaaa',
'__________',
'aaaaaaaaa_',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aa__aa__aa'
],
naming:
{
//设置行列等信息
top: false, //不显示顶部横坐标(行)
getLabel: function(character, row, column)
{
return column; //返回座位信息
}
},
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);
$tickects_num.text(sc.find('selected').length + 1); //统计选票数量
$total_price.text(getTotalPrice(sc) + price);//计算票价总金额
return 'selected';
}
else if (this.status() == 'selected') //选中状态
{
$tickects_num.text(sc.find('selected').length - 1);//更新票数量
$total_price.text(getTotalPrice(sc) - price);//更新票价总金额
$('#cart-item-' + this.settings.id).remove();//删除已预订座位
return 'available';
}
else if (this.status() == 'unavailable') //已售出状态
{
return 'unavailable';
}
else{
return this.style();
}
}
});
//设置已售出的座位
sc.get(['1_3', '1_4', '4_4', '4_5', '4_6', '4_7', '4_8']).status('unavailable');
});
function getTotalPrice(sc) //计算票价总额
{
var total = 0;
sc.find('selected').each(function() {
total += price;
});
return total;
}
</script>
网页效果图:
分析:
目的可知:左边屏幕下方需要显示座位坐标图;右边需要显示选择的座位,票数的变化,总价的变化,座位是否可选的标识。
在Jquery动态初始化页面中,获取需要变化的标签,同时将其转化为Jquery对象(Jquery座位区对象、Jquery票数对象、Jquery票价总额对象、Jquery座位分布图对象)。
设置Jquery单击事件(可选,已选,删除,不可选)。
- 项目源文件(index.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>film seats choice</title>
<link rel="stylesheet" type="text/css" href="css/jq22.css" />
<style type="text/css">
.front{width: 300px;margin: 5px 10px 40px 0px;background-color: #f0f0f0; color: #666;text-align: center;padding: 3px;border-radius: 5px;}
.booking_area {float: right;position: relative;width:200px;height: 450px; }
.booking_area h3 {margin: 5px 5px 0 0;font-size: 16px;}
.booking_area p{line-height:26px; font-size:16px; color:#999}
.booking_area 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}
#seats_chose {max-height: 150px;overflow-y: auto;overflow-x: none;width: 200px;}
#seats_chose 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}
</style>
</head>
<body>
<div class="container">
<div class="demo clearfix">
<!---左边座位列表----->
<div id="seat_area">
<div class="front">屏幕</div>
</div>
<!---右边选座信息----->
<div class="booking_area">
<p>电影:<span>我和我的祖国</span></p>
<p>时间:<span>10月1日 14:00</span></p>
<p>座位:</p>
<ul id="seats_chose"></ul>
<p>票数:<span id="tickects_num">0</span></p>
<p>总价:<b>¥<span id="total_price">0</span></b></p>
<input type="button" class="btn" value="确定购买"/>
<div id="legend"></div> <!-- 显示可选的座位和已售出的座位标识 -->
</div>
</div>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="js/jquery.seat-charts.min.js"></script>
<script type="text/javascript">
var price = 100; //电影票价
$(document).ready(function() {
var $cart = $('#seats_chose'); //座位区
var $tickects_num = $('#tickects_num'); //票数
var $total_price = $('#total_price'); //票价总额
var sc = $('#seat_area').seatCharts({
map:
[
//座位结构图 a代表座位 _代表过道
'aaaaaaaaaa',
'aaaaaaaaaa',
'__________',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aaaaaaaaaa',
'aa__aa__aa'
],
naming:
{
//设置行列等信息
top: false, //不显示顶部横坐标(行)
left:false, //不显示左边纵坐标(列)
getLabel: function(character, row, column)
{
return column; //返回座位信息 column
}
},
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);
$tickects_num.text(sc.find('selected').length + 1); //统计选票数量
$total_price.text(getTotalPrice(sc) + price);//计算票价总金额
return 'selected';
}
else if (this.status() == 'selected') //选中状态
{
$tickects_num.text(sc.find('selected').length - 1);//更新票数量
$total_price.text(getTotalPrice(sc) - price);//更新票价总金额
$('#cart-item-' + this.settings.id).remove();//删除已预订座位
return 'available';
}
else if (this.status() == 'unavailable') //已售出状态
{
return 'unavailable';
}
else{
return this.style();
}
}
});
//设置已售出的座位
sc.get(['1_3', '1_4', '4_4', '4_5', '4_6', '4_7', '4_8']).status('unavailable');
});
function getTotalPrice(sc) //计算票价总额
{
var total = 0;
sc.find('selected').each(function() {
total += price;
});
return total;
}
</script>
</body>
</html>