2024-08-23

以下是一个简单的下雪效果实现,使用HTML5和JavaScript。

HTML部分(index.html):




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下雪效果</title>
<style>
  body, html {
    margin: 0;
    padding: 0;
    height: 100%;
  }
  canvas {
    display: block;
  }
</style>
</head>
<body>
<canvas id="snow-canvas"></canvas>
 
<script src="snow.js"></script>
</body>
</html>

JavaScript部分(snow.js):




const canvas = document.getElementById('snow-canvas');
const ctx = canvas.getContext('2d');
 
const snowflakes = [];
const flakeCount = 200;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
 
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
 
function init() {
  for (let i = 0; i < flakeCount; i++) {
    snowflakes.push({
      x: Math.random() * canvasWidth,
      y: Math.random() * canvasHeight,
      size: Math.random() * 10,
      speed: Math.random() * 2 + 1,
    });
  }
}
 
function animateSnow() {
  ctx.clearRect(0, 0, canvasWidth, canvasHeight);
 
  snowflakes.forEach((flake) => {
    flake.y += flake.speed;
    flake.x *= 0.98;
    ctx.fillStyle = 'white';
    ctx.beginPath();
    ctx.arc(flake.x, flake.y, flake.size, 0, Math.PI * 2);
    ctx.fill();
 
    if (flake.y > canvasHeight) {
      flake.y = 0;
      flake.x = Math.random() * canvasWidth;
    }
  });
}
 
function loop() {
  requestAnimationFrame(loop);
  animateSnow();
}
 
init();
loop();

这段代码会在页面上创建一个<canvas>元素,并在其中绘制下雪的效果。每个雪花是一个圆形,它们随机生成位置和大小,并以随机速度下降。如果雪花移出底部,它们会重新生成在顶部。这个简单的下雪效果可以通过调整flakeCount来控制雪花的数量,以及通过修改Math.random()的范围来调整雪花的大小和速度。

2024-08-23

以下是一个简化的HTML和JavaScript代码示例,用于实现上述登录窗口的功能:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Window</title>
<style>
  body, html {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #2e4053;
  }
  .login-container {
    width: 300px;
    padding: 40px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
  }
  .login-container h2 {
    text-align: center;
  }
  .form-group {
    margin-bottom: 15px;
  }
  .form-group input[type="text"], .form-group input[type="password"] {
    width: 100%;
    padding: 15px;
    margin-top: 30px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box;
  }
  .form-group input[type="submit"] {
    width: 100%;
    padding: 15px;
    border: none;
    border-radius: 4px;
    background: #58a9ff;
    color: #fff;
    cursor: pointer;
  }
  .form-group input[type="submit"]:hover {
    background: #4e8ab3;
  }
</style>
</head>
<body>
 
<div class="login-container">
  <h2>Login to your Account</h2>
  <form action="">
    <div class="form-group">
      <input type="text" required autocomplete="off" placeholder="Username" name="username" />
    </div>
    <div class="form-group">
      <input type="password" required placeholder="Password" name="password" />
    </div>
    <div class="form-group">
      <input type="submit" value="Login" />
    </div>
  </form>
</div>
 
<script>
  // JavaScript code to handle the login form submission
  document.querySelector('form').addEventListener('submit', function(event) {
    event.preventDefault(); // Prevent the form from submitting
    const username = document.querySelector('input[name="username"]').value;
    const password = document.querySelector('input[name="password"]').value;
 
    // Here you would handle the login logic, e.g., by sending a request to the server
    // For now, we'll just log the username and password to the console
    console.log('Login attempt with username: ' + username + ' and passwor
2024-08-23

在HTML中实现点击按钮切换整张屏幕和局部屏幕,可以使用JavaScript来控制元素的显示和隐藏。以下是一个简单的示例代码:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Toggle Screen</title>
<script>
function toggleFullScreen() {
    var elem = document.body; // 整个屏幕
    if (elem.requestFullscreen) {
        elem.requestFullscreen();
    } else if (elem.mozRequestFullScreen) { /* Firefox */
        elem.mozRequestFullScreen();
    } else if (elem.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
        elem.webkitRequestFullscreen();
    } else if (elem.msRequestFullscreen) { /* IE/Edge */
        elem.msRequestFullscreen();
    }
}
 
function togglePartialScreen() {
    var elem = document.getElementById('partialScreen'); // 局部屏幕元素
    if (elem.style.display === 'none') {
        elem.style.display = 'block';
    } else {
        elem.style.display = 'none';
    }
}
</script>
</head>
<body>
 
<button onclick="toggleFullScreen()">切换整张屏幕</button>
<button onclick="togglePartialScreen()">切换局部屏幕</button>
 
<div id="partialScreen" style="display:none;">
    <!-- 局部屏幕内容 -->
    局部屏幕内容
</div>
 
</body>
</html>

在这个示例中,toggleFullScreen 函数会将浏览器当前页面全屏显示,而 togglePartialScreen 函数会切换一个ID为 partialScreen 的元素的显示状态。这个元素在HTML中被设置为隐藏(display: none),当用户点击切换局部屏幕的按钮时,它会显示出来。

2024-08-23

HTML5 <progress> 标签用于表示一个进度条,通常用于显示JavaScript中的任务进度,如文件下载进度、图片加载进度等。

属性:

  • max:定义进度条的最大值。
  • value:定义进度条的当前值。

示例代码:




<progress value="30" max="100"> 30%</progress>

这个例子创建了一个进度条,最大值为100,当前值为30。进度条显示30%,随着value属性的变化,进度条内部填充色也会随之变化,直至达到max定义的值。

2024-08-23



// Base64编码
function base64Encode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}
 
// Base64解码
function base64Decode(str) {
    return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}
 
// 使用示例
var encodedStr = base64Encode('Hello, World!');
console.log(encodedStr); // 输出编码后的字符串
 
var decodedStr = base64Decode(encodedStr);
console.log(decodedStr); // 输出解码后的字符串

这段代码提供了Base64的编码和解码功能。base64Encode函数接收一个字符串,将其进行URI编码,然后将编码后的字符串中的百分号编码转换为对应的字符,并使用btoa进行Base64编码。base64Decode函数则执行相反的操作,将Base64字符串解码并转换回原始字符串。

2024-08-23

在uniapp中使用Leaflet加载天地图,可以通过集成Leaflet插件和天地图的方式实现。以下是一个简单的示例代码:

  1. 首先,确保你的项目中已经安装了Leaflet。如果没有安装,可以通过npm安装:



npm install leaflet
  1. pages.json中配置"web-view",允许在App端使用网页组件:



{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        "enableWebview": true
      }
    }
  ]
}
  1. 在页面的.vue文件中,引入Leaflet和天地图,并初始化地图:



<template>
  <view class="content">
    <web-view src="/hybrid/leaflet-map.html"></web-view>
  </view>
</template>



<script>
export default {
  data() {
    return {};
  },
  onReady() {
    // 初始化Leaflet地图
    this.initMap();
  },
  methods: {
    initMap() {
      const map = L.map('map').setView([39.916527, 116.397128], 12); // 设置中心点和缩放级别
 
      L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      }).addTo(map);
 
      // 加载天地图
      const tdtLayer = L.tileLayer('https://{s}.tianditu.gov.cn/vec_c/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=c&TileMatrix={z}&TileRow={y}&TileCol={x}&style=default&format=tiles&tk=你的天地图key').addTo(map);
 
      // 设置最大和最小缩放级别
      tdtLayer.setMaxZoom(18);
      tdtLayer.setMinZoom(1);
    }
  }
};
</script>
  1. static目录下创建leaflet-map.html文件,并在其中初始化Leaflet:



<!DOCTYPE html>
<html>
<head>
  <title>Leaflet Map</title>
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
  <style>
    #map {
      height: 100%;
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
  <script>
    var map = L.map('map').setView([39.916527, 116.397128], 12);
 
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
 
    // 加载天地图
    var tdtLayer = L.tileLayer('https://{s}.tianditu.gov.cn/vec_c/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec
2024-08-23

HTML5.1中的<textarea><label>标签用于创建多行文本输入区域和定义标签关联表单元素。以下是如何使用这两个标签的示例代码:




<!DOCTYPE html>
<html>
<head>
    <title>Textarea and Label Example</title>
</head>
<body>
    <form action="#">
        <label for="message">Message:</label>
        <textarea id="message" name="message" rows="4" cols="50">
请输入您的信息...
        </textarea>
        <input type="submit">
    </form>
</body>
</html>

在这个例子中,<label>标签通过for属性指定与<textarea>id值相同,以此来建立两者之间的关联。用户点击<label>时,与之关联的表单元素(在这个例子中是<textarea>)会获得焦点。<textarea>允许用户输入多行文本,其中rowscols属性分别指定了文本区域的行数和列数。当用户输入信息后,可以通过提交表单将信息发送到服务器。

2024-08-23



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller
public class MyController {
 
    @GetMapping("/greeting")
    public String greeting(Model model) {
        model.addAttribute("name", "World");
        return "greeting";
    }
}

这段代码定义了一个控制器MyController,它处理"/greeting"的GET请求。它将一个属性"name"添加到模型中,值为"World",并返回视图名称"greeting"。这个视图应该是一个Thymeleaf模板,它可以使用${name}来访问模型中的"name"属性。

2024-08-23

HTML5 引入了多媒体元素,包括 videoaudio 标签,以简化多媒体内容的嵌入。

视频 (video) 标签

HTML5 的 video 标签可以用来嵌入视频内容。




<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
  • widthheight 属性用来设置视频的尺寸。
  • controls 属性添加视频播放的默认控制界面(播放、暂停、音量控制等)。
  • <source> 标签指定视频文件的路径和类型。
  • 如果浏览器不支持 video 标签,会显示 <video> 标签中的文本内容。

音频 (audio) 标签

HTML5 的 audio 标签可以用来嵌入音频内容。




<audio controls>
  <source src="song.mp3" type="audio/mpeg">
  <source src="song.ogg" type="audio/ogg">
  Your browser does not support the audio tag.
</audio>
  • controls 属性添加音频播放的默认控制界面(播放、暂停、音量控制等)。
  • <source> 标签指定音频文件的路径和类型。
  • 如果浏览器不支持 audio 标签,会显示 <audio> 标签中的文本内容。

注意事项

  • 为了兼容不同浏览器,应当为 <source> 标签提供多种格式的文件源。
  • 对于不支持 HTML5 多媒体元素的旧版浏览器,可以使用 Flash 或其他回退方案。
2024-08-23

HTML5 的 <dialog> 标签用于创建对话框或者提示框,它可以用CSS来定制样式。

基本用法:




<dialog open>
  这是一个打开的对话框。
</dialog>

<dialog> 标签默认是不可见的,需要设置 open 属性来显示它。

你可以通过 JavaScript 来控制 <dialog> 的打开和关闭:




<dialog id="myDialog">
  这是一个通过 JavaScript 控制的对话框。
</dialog>
 
<button onclick="openDialog()">打开对话框</button>
<button onclick="closeDialog()">关闭对话框</button>
 
<script>
  function openDialog() {
    document.getElementById('myDialog').show();
  }
 
  function closeDialog() {
    document.getElementById('myDialog').close();
  }
</script>

<dialog> 元素还支持通过 CSS 进行样式定制,比如隐藏关闭按钮:




dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
}
 
dialog::close-button {
  display: none;
}

请注意,<dialog> 标签的兼容性在不同的浏览器中可能会有所不同,请在实际应用前检查浏览器的兼容性。