html - 电影院的座位如何布置

标签 html css

我想设计如下所示的东西。我应该如何在 html/css 中布局它。我应该使用 table 还是 flex 会更好还是其他?

Picture hall

最佳答案

虽然这种风格的问题往往会产生基于意见的答案,因此会令人不悦,但我认为在开始投入工作之前询问专家关于如何解决特定问题的意见是合法的。

现在,我绝不是 html 专家。但是,虽然问题中没有提到,但我认为这可能是在 Web 应用程序而不是静态 HTML 文档中显示状态信息。也许在需要的上下文中,有一个 websocket 或一些基于计时器的页面更新。座位分配数据可能来自服务器。

在我上面描述的场景中,一种无需表格即可实现的方法如下所示。使用 Canvas (如果投入足够的时间,可以进行漂亮的渲染)和 JavaScript 函数,使用从某处进入脚本的数据。

这种方法是否优于基于 html 表的解决方案,可能归结为:

  • 图片应该有多漂亮?
  • 什么是不那么烦人的工作?用脚本填充html表格还是用脚本渲染canvas图片?

这里有一个小原型(prototype)展示了这种方法:

<!DOCTYPE html>
<html>
    <head>
        <title>Demo of a canvas used to render seat plan in a Cinema</title>
        <script>
            var EMPTY = 0; // Still available for reservation and purchase.
            var RESERVED = 1; // reserved but not yet paid for.
            var BOUGHT = 2; // bought and paid for.

            function Point(x,y) {
                return { X: x, Y: y }
            }
            function Size(w,h) {
                return {Width: w, Height: h}
            }
            function Rectangle(left,top,width,height) {
                return {TopLeft: Point(left,top), Size: Size(width,height)}
            }
            function seatColorFromSeatStatus(seatStatus) {
                switch(seatStatus) {
                    case EMPTY: return "white";
                    case RESERVED: return "green";
                    case BOUGHT: return "red";
                    default: return "black"; // Invalid value...
                }
            }
            function mapSeatStatusToSeatColor(seats)
            {
                var result = {};
                for(seat in seats) {
                    result[seat] = seatColorFromSeatStatus(seats[seat])
                }
                return result;
            }
            function seatKeyFromPosition(row,col) {
                return JSON.stringify([row,col]);
            }
            function seatRowFromKey(key) {
                return (JSON.parse(key))[0];
            }
            function seatColFromKey(key) {
                return (JSON.parse(key)[1]);
            }
            function getSeatInfo(nrows,ncolumns) {
                var result = { NRows: nrows, NColumns: ncolumns, Seats : {} };
                for(row = 0; row < nrows; row++) {
                    for( col = 0; col < ncolumns; col++ ) {
                        result.Seats[seatKeyFromPosition(row,col)] = EMPTY;
                    }
                }
                result.Seats[seatKeyFromPosition(0,0)] = RESERVED;
                result.Seats[seatKeyFromPosition(1,3)] = BOUGHT;
                return result;
            }
            function renderSeat(ctx,r,fillColor) {
                var backup = ctx.fillStyle;
                ctx.strokeStyle = "blue";
                ctx.rect(r.TopLeft.X+2,r.TopLeft.Y+2,r.Size.Width-4,r.Size.Height-4);
                ctx.stroke();
                ctx.fillStyle = fillColor;
                ctx.fillRect(r.TopLeft.X+3,r.TopLeft.Y+3,r.Size.Width-5,r.Size.Height-5);
                ctx.fillStyle = backup;
            }
            function renderSeatplan(seatInfo) {
                var nrows = seatInfo.NRows;
                var ncolumns = seatInfo.NColumns;
                var seatColors = mapSeatStatusToSeatColor(seatInfo.Seats)
                var canvas = document.getElementById("seatplan");
                var ctx = canvas.getContext("2d");

                var borderWidth = 10;
                var rcContent = Rectangle(
                    borderWidth
                    , borderWidth
                    , canvas.width - 2 * borderWidth
                    , canvas.height - 2 * borderWidth
                );
                var szCell = Size(
                    Math.floor(rcContent.Size.Width / (ncolumns + 1))
                    , Math.floor(rcContent.Size.Height / (nrows + 1))
                );
                ctx.font = "30px Arial";

                for(row = -1; row < nrows; row++) {
                    for(col = -1; col < ncolumns; col++ ) {
                        var r = Rectangle(
                            rcContent.TopLeft.X + szCell.Width * (col+1)
                            ,rcContent.TopLeft.Y + szCell.Height * (row+1)
                            ,szCell.Width
                            ,szCell.Height
                            );
                        var center = Point(szCell.Width / 2, szCell.Height / 2);
                        if (row == -1 && col == -1) {
                            // nothing to render.
                        }
                        else if(row == -1){
                            // render column headers as numbers...
                            ctx.fillStyle = "black";
                            ctx.textAlign = "center";
                            ctx.fillText(col.toString(),r.TopLeft.X+center.X,r.TopLeft.Y+center.Y+6);
                        }
                        else if(col == -1){
                            // render row header
                            ctx.fillStyle = "black";
                            ctx.textAlign = "center";
                            ctx.fillText(String.fromCharCode(65 + row),r.TopLeft.X+center.X+4,r.TopLeft.Y+center.Y+6);
                        }
                        else
                        {
                            // render seat
                            renderSeat(ctx,r,seatColors[seatKeyFromPosition(row,col)]);
                        }
                    }
                }
            }
        </script>
    </head>
    <body onload="renderSeatplan(getSeatInfo(10,16));">
        <h1>Seatplan</h1>
        <canvas id="seatplan" width="640" height="480"></canvas>
    </body>
</html>

由于这是一种简单的方法,因此不应忽视,有一些 Web 框架(例如 Angular、Node.js 等)可以用更少的代码行解决相同的问题。

最后,我向所有真正的 html 专家并发现我的编码风格很糟糕的人表示歉意;)

关于html - 电影院的座位如何布置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011399/

相关文章:

html - 响应式地将元素绝对定位在背景之上

javascript - 显示数据后合并边框

php - 如何通过php从数据库中获取特定字段

css - 同一行的标签和文本框+整个宽度的文本框

javascript - Firefox 显示没有来源的图像

javascript - 根据它们所在的背景显示文本颜色

html - z-index 不适用于云缩放插件

javascript - 如何创建垂直导航菜单?

javascript - 使用 javascript 清除所有 HTML 字段

javascript - for循环创建多个元素?