Css 画布位于导航栏上方
出于某种原因,我的画布位于导航栏上方。我之前做对了,并且改变了一些事情,现在我的生活无法理解它。我希望页面加载时导航栏位于顶部,画布位于导航栏下方页面的中心。这与CSS或某个错误放置的标记有关吗?谢谢你的帮助Css 画布位于导航栏上方,css,html,canvas,html5-canvas,Css,Html,Canvas,Html5 Canvas,出于某种原因,我的画布位于导航栏上方。我之前做对了,并且改变了一些事情,现在我的生活无法理解它。我希望页面加载时导航栏位于顶部,画布位于导航栏下方页面的中心。这与CSS或某个错误放置的标记有关吗?谢谢你的帮助 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=dev
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Square Up</title>
</head>
<body>
<style>
.navbar-brand {
font-family: arial;
font-size: 35px;
font-color: #00FA9A;
}
body {
background-color: whitesmoke;
}
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
width: 600px;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
<nav class="navbar navbar-expand-sm navbar-dark bg-secondary">
<a class="navbar-brand" href="index.html">Square Up</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="game.html">Play<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="instructions.html">Instructions</a>
</li>
</ul>
</div>
</nav>
<body onload="startGame()">
<script>
var Square;
var Course = [];
var myScore;
function startGame() {
Square = new component(30, 30, "red", 10, 120);
Square.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < Course.length; i += 1) {
if (Square.crashWith(Course[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
Course.push(new component(10, height, "green", x, 0));
Course.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < Course.length; i += 1) {
Course[i].x += -1;
Course[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
Square.newPos();
Square.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
Square.gravity = n;
}
</script>
<br>
<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">Click me to move!</button>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
摆正
.navbar品牌{
字体系列:arial;
字体大小:35px;
字体颜色:#00FA9A;
}
身体{
背景色:白烟;
}
帆布{
边框:1px实心#D3;
背景色:#f1f1;
左侧填充:0;
右边填充:0;
左边距:自动;
右边距:自动;
显示:块;
宽度:600px;
排名:0;
底部:0;
左:0;
右:0;
}
-
-
-
var平方;
var课程=[];
var myScore;
函数startName(){
正方形=新部件(30,30,“红色”,10,120);
平方。重力=0.05;
myScore=新组件(“30px”、“控制台”、“黑色”、280、40、“文本”);
myGameArea.start();
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
这个.frameNo=0;
this.interval=setInterval(updateGameArea,20);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
}
}
功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
这个重力=0;
这个重力速度=0;
this.update=函数(){
ctx=myGameArea.context;
如果(this.type==“text”){
ctx.font=this.width+“”+this.height;
ctx.fillStyle=颜色;
ctx.fillText(this.text,this.x,this.y);
}否则{
ctx.fillStyle=颜色;
ctx.fillRect(this.x,this.y,this.width,this.height);
}
}
this.newPos=函数(){
this.gravitySpeed+=this.gravity;
this.x+=this.speedX;
这个.y+=这个.speedY+这个.gravitySpeed;
这个;
}
this.hitbooth=函数(){
var rockbottom=myGameArea.canvas.height—this.height;
如果(this.y>rockbottom){
y=最低点;
这个重力速度=0;
}
}
this.crashWith=函数(otherobj){
var myleft=this.x;
var myright=this.x+(this.width);
var mytop=this.y;
var mybottom=this.y+(this.height);
var otherleft=otherobj.x;
var otherright=otherobj.x+(otherobj.width);
var othertop=otherobj.y;
var otherbottom=otherobj.y+(otherobj.height);
var-crash=true;
如果((mybottomotherbottom)| |(myrightotherright)){
崩溃=错误;
}
返回碰撞;
}
}
函数updateGameArea(){
变量x,高度,间隙,最小高度,最大高度,最小间隙,最大间隙;
对于(i=0;i
点击我移动!
请参见下面的演示,它应该适合您,您还可以向画布添加一些类,并为其留出空白
.navbar.navbar-fixed{
位置:固定;
排名:0;
左:0;
宽度:100%;
}
帆布{
边缘顶部:100px;
}
摆正
.navbar品牌{
字体系列:arial;
字体大小:35px;
字体颜色:#00FA9A;
}
身体{
背景色:白烟;
}
帆布{
边框:1px实心#D3;
背景色:#f1f1;
左侧填充:0;
右边填充:0;
左边距:自动;
右边距:自动;
显示:块;
宽度:600px;
排名:0;
底部:0;
左:0;
右:0;
}