包含HTML的switch的代码示例
时间: 2024-05-15 12:17:53 浏览: 12
以下是一个包含 HTML 的 switch 的代码示例:
```
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
```
通过使用 CSS 样式,此代码示例可以将一个复选框转换为 switch(开关)控件。在此示例中,“slider round” 类为 switch 滑块添加了样式,而“switch” 类使其正确地对齐。请注意,此示例中的 HTML 代码不是完整的代码,因此您可能需要根据您的实际需求进行修改。
相关问题
html+js贪吃蛇示例代码
以下是一个基于 HTML 和 JavaScript 的贪吃蛇示例代码:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>贪吃蛇</title>
<style>
#canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript:
```javascript
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const tileSize = 10;
const width = canvas.width / tileSize;
const height = canvas.height / tileSize;
let snake = [{ x: 5, y: 5 }];
let apple = { x: 10, y: 10 };
let direction = "right";
let score = 0;
function drawTile(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);
}
function drawSnake() {
snake.forEach(function (tile) {
drawTile(tile.x, tile.y, "green");
});
}
function drawApple() {
drawTile(apple.x, apple.y, "red");
}
function update() {
let snakeHead = { x: snake[0].x, y: snake[0].y };
switch (direction) {
case "up":
snakeHead.y--;
break;
case "down":
snakeHead.y++;
break;
case "left":
snakeHead.x--;
break;
case "right":
snakeHead.x++;
break;
}
// Check if snake hits wall
if (snakeHead.x < 0 || snakeHead.x >= width || snakeHead.y < 0 || snakeHead.y >= height) {
alert("Game Over!");
location.reload();
}
// Check if snake hits itself
for (let i = 1; i < snake.length; i++) {
if (snakeHead.x === snake[i].x && snakeHead.y === snake[i].y) {
alert("Game Over!");
location.reload();
}
}
snake.unshift(snakeHead);
// Check if snake eats apple
if (snakeHead.x === apple.x && snakeHead.y === apple.y) {
score++;
document.getElementById("score").innerHTML = "Score: " + score;
apple.x = Math.floor(Math.random() * width);
apple.y = Math.floor(Math.random() * height);
} else {
snake.pop();
}
}
function gameLoop() {
update();
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnake();
drawApple();
}
setInterval(gameLoop, 100);
document.addEventListener("keydown", function (event) {
switch (event.key) {
case "ArrowUp":
if (direction !== "down") {
direction = "up";
}
break;
case "ArrowDown":
if (direction !== "up") {
direction = "down";
}
break;
case "ArrowLeft":
if (direction !== "right") {
direction = "left";
}
break;
case "ArrowRight":
if (direction !== "left") {
direction = "right";
}
break;
}
});
```
这个示例代码实现了一个基本的贪吃蛇游戏,包括贪吃蛇的移动、撞墙判定、撞自身判定、吃苹果、计分等功能。你可以将代码保存为 `snake.html` 和 `snake.js`,在浏览器中打开 `snake.html` 运行游戏。
html css js登录注册界面代码
### 回答1:
<!DOCTYPE html>
<html>
<head>
<title>Login and Registration Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body{
background-color: #f1f1f1;
font-family: Arial, sans-serif;
}
h2{
text-align: center;
color: #333;
}
.container{
max-width: 500px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
}
form{
display: flex;
flex-direction: column;
align-items: center;
}
input[type="text"], input[type="password"]{
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button{
background-color: #4CAF50;
color: #fff;
padding: 12px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover{
background-color: #45a049;
}
.error{
color: red;
font-size: 12px;
margin-top: 5px;
margin-bottom: 0;
}
.success{
color: green;
font-size: 12px;
margin-top: 5px;
margin-bottom: 0;
}
.switch{
text-align: center;
margin-top: 20px;
}
.switch a{
color: #4CAF50;
text-decoration: none;
}
.switch a:hover{
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h2>Login and Registration Form</h2>
<div id="login">
<form action="#" method="post">
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Enter your username" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Enter your password" required>
<button type="submit">Login</button>
<p class="switch">Don't have an account? <a href="#" onclick="switchForm()">Register here.</a></p>
</form>
</div>
<div id="register" style="display: none;">
<form action="#" method="post">
<label for="fullname">Full Name</label>
<input type="text" id="fullname" name="fullname" placeholder="Enter your full name" required>
<label for="email">Email</label>
<input type="text" id="email" name="email" placeholder="Enter your email" required>
<label for="newusername">Username</label>
<input type="text" id="newusername" name="newusername" placeholder="Enter a username" required>
<label for="newpassword">Password</label>
<input type="password" id="newpassword" name="newpassword" placeholder="Enter a password" required>
<button type="submit">Register</button>
<p class="switch">Already have an account? <a href="#" onclick="switchForm()">Login here.</a></p>
</form>
</div>
<div id="message"></div>
</div>
<script>
function switchForm(){
var loginForm = document.getElementById("login");
var registerForm = document.getElementById("register");
if(loginForm.style.display === "none"){
loginForm.style.display = "block";
registerForm.style.display = "none";
} else{
loginForm.style.display = "none";
registerForm.style.display = "block";
}
document.getElementById("message").innerHTML = "";
}
</script>
</body>
</html>
### 回答2:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>登录注册界面</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="container">
<form id="login-form" class="form" action="login.php" method="post">
<h2>登录</h2>
<input type="text" placeholder="用户名" name="username" required>
<input type="password" placeholder="密码" name="password" required>
<button type="submit">登录</button>
</form>
<form id="register-form" class="form" action="register.php" method="post">
<h2>注册</h2>
<input type="text" placeholder="用户名" name="username" required>
<input type="password" placeholder="密码" name="password" required>
<button type="submit">注册</button>
</form>
</div>
</body>
</html>
```
CSS代码(style.css):
```css
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.form {
width: 300px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
h2 {
text-align: center;
}
input {
width: 100%;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
width: 100%;
padding: 10px;
background-color: #337ab7;
color: #fff;
border: none;
border-radius: 3px;
}
```
JavaScript代码(script.js):
```javascript
// 在此添加任何需要的JavaScript代码,例如表单验证等
```
以上是一个简单的登录注册界面的HTML、CSS和JavaScript代码。HTML部分包含两个表单,用于登录和注册,分别包括用户名和密码的输入框以及提交按钮。CSS部分用于设置界面的样式,包括容器的布局、表单的样式等。JavaScript部分可以用于添加表单验证等额外功能。
请注意,以上代码仅为示例,可能需要根据具体需求进行修改和完善。
### 回答3:
以下是一个简单的HTML、CSS和JavaScript的登录注册界面代码:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<title>登录注册界面</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="login">
<h2>登录</h2>
<form>
<input type="text" placeholder="用户名" required>
<input type="password" placeholder="密码" required>
<button type="submit">登录</button>
</form>
</div>
<div id="register">
<h2>注册</h2>
<form>
<input type="text" placeholder="用户名" required>
<input type="password" placeholder="密码" required>
<input type="password" placeholder="确认密码" required>
<button type="submit">注册</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS部分(style.css):
```css
body {
font-family: Arial, sans-serif;
background-color: #f1f3f5;
}
#login, #register {
background-color: white;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 300px;
margin: 0 auto;
margin-top: 100px;
}
h2 {
margin-bottom: 20px;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
}
button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
```
JavaScript部分(script.js):
```javascript
// 登录表单和注册表单的显示切换
document.getElementById("register").style.display = "none";
document.getElementById("login-link").addEventListener("click", function(){
document.getElementById("login").style.display = "block";
document.getElementById("register").style.display = "none";
});
document.getElementById("register-link").addEventListener("click", function(){
document.getElementById("login").style.display = "none";
document.getElementById("register").style.display = "block";
});
```
以上代码创建了一个简单的登录注册界面。其中包含一个登录的字段和一个注册的字段。使用HTML创建了表单,CSS样式美化页面布局,JavaScript代码实现了登录表单和注册表单的显示切换功能。用户可以在登录表单输入用户名和密码进行登录,也可以在注册表单输入用户名和密码进行注册。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)