import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { CSS2DRenderer, CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
let camera, scene, renderer, labelRenderer, controls;
let mesh;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.set(0.5, 0.5, 0.5);
scene = new THREE.Scene();
// 创建一个立方体
let geometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
mesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial());
scene.add(mesh);
// 创建CSS2DRenderer
labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = 0;
document.body.appendChild(labelRenderer.domElement);
// 创建2D标签对象
let label = document.createElement('div');
label.style.color = 'white';
label.innerHTML = 'Hello Three.js!';
let labelObject = new CSS2DObject(label);
labelObject.position.set(0.5, 0.5, 0);
scene.add(labelObject);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 添加轨道控制器
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// 监听窗口大小变化
window.addEventListener('resize', onWindowResize, false);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
controls.update();
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.005;
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.inn
AutoFit.js 是一个用于解决网页在不同屏幕尺寸下的自适应问题的 JavaScript 库。以下是使用 AutoFit.js 实现 PC 和大屏幕自适应的方法:
- 首先,确保在你的 HTML 文件中包含 AutoFit.js。你可以从 AutoFit.js 的 GitHub 仓库或者其他 CDN 获取这个文件。
<script src="path/to/autofit.min.js"></script>
- 接下来,在你的 JavaScript 代码中初始化 AutoFit.js。通常,你可以在
<body>
标签加载完成后做这件事。
document.addEventListener('DOMContentLoaded', function() {
new AutoFit();
});
- 你还可以传递一些配置参数来定制 AutoFit.js 的行为。例如,你可以设置最大和最小的缩放比例。
document.addEventListener('DOMContentLoaded', function() {
new AutoFit({
minZoom: 1,
maxZoom: 1.5
});
});
AutoFit.js 会在页面加载时自动计算并调整页面的缩放比例,以适应不同的屏幕尺寸。这个库提供了一个简单的解决方案,用于处理不同设备上的自适应问题。
OrbitControls 是 Three.js 中用于提供相机绕物体旋转、缩放以及平移的一个控制器。
以下是一些使用 OrbitControls 的方法:
- 基本使用:
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建场景
const scene = new THREE.Scene();
// 创建物体
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建OrbitControls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// 渲染循环
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
controls.update(); // 要更新控件的状态
}
animate();
- 限制旋转:
你可以通过设置 OrbitControls
的 minPolarAngle
和 maxPolarAngle
属性来限制旋转的最小和最大角度。
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minPolarAngle = Math.PI / 3; // 最小角度设置为60度
controls.maxPolarAngle = Math.PI / 2; // 最大角度设置为90度
- 禁用某些操作:
你可以通过设置 OrbitControls
的 enabled
属性为 false
来禁用控件,或者通过设置 enableRotate
、enableZoom
、enablePan
等属性为 false
来分别禁用旋转、缩放和平移。
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enabled = false; // 禁用控件
controls.enableRotate = false; // 禁用旋转
- 添加监听事件:
你可以通过监听 OrbitControls
的事件来响应用户的操作。
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', () => {
// 当用户进行旋转、缩放或平移操作时,会触发这个事件
console.log('Camera position:', camera.position);
});
- 设置动画时间:
你可以通过设置 OrbitControls
的 animateTo
方法来平滑地动画到特定的位置。
const controls = new THREE.OrbitC
JavaScript中的Set是一种新的原生集合类型,类似于数组,但只能有唯一的值。
以下是一些Set的常用方法和操作:
- 创建Set
let s = new Set();
- 添加元素
s.add(1);
s.add("hello");
- 删除元素
s.delete(1);
- 检查元素是否存在
s.has(1); // false
- 清空Set
s.clear();
- 获取Set的长度
s.size;
- 遍历Set
for (let item of s) {
console.log(item);
}
- 使用Array去重
let arr = [1, 2, 3, 2, 4, 1];
let unique = [...new Set(arr)];
- 交集、并集、差集操作
let setA = new Set([1, 2, 3]);
let setB = new Set([2, 3, 4]);
// 并集
let union = new Set([...setA, ...setB]);
// 交集
let intersect = new Set([...setA].filter(x => setB.has(x)));
// 差集
let difference = new Set([...setA].filter(x => !setB.has(x)));
以上是Set的基本操作和使用方法,Set是一种新的数据结构,可以用于解决一些实际问题,例如,数组去重、数据的交集、并集、差集等操作。
ES6(ECMAScript 2015)是JavaScript语言的一个重要版本,它引入了许多新特性,如类、模块、箭头函数、let和const命令等,以下是一些基本的ES6特性的示例代码:
- 类(Class):
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, my name is ${this.name}!`);
}
}
const person = new Person('Alice');
person.greet(); // 输出: Hello, my name is Alice!
- 模块(Module):
// math.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
// main.js
import { add, subtract } from './math.js';
console.log(add(5, 3)); // 输出: 8
console.log(subtract(5, 3)); // 输出: 2
- 箭头函数(Arrow Function):
const sum = (a, b) => a + b;
console.log(sum(3, 4)); // 输出: 7
- let和const命令:
let x = 5;
x = 10; // 正确
console.log(x); // 输出: 10
const y = 5;
// y = 10; // 错误:常量不能重新赋值
console.log(y); // 输出: 5
- 解构赋值(Destructuring Assignment):
let [a, b, c] = [1, 2, 3];
console.log(a); // 输出: 1
console.log(b); // 输出: 2
console.log(c); // 输出: 3
let { x, y } = { x: 1, y: 2 };
console.log(x); // 输出: 1
console.log(y); // 输出: 2
- 扩展运算符(Spread Operator):
const numbers = [1, 2, 3];
const clone = [...numbers];
console.log(clone); // 输出: [1, 2, 3]
const merged = [...numbers, 4, 5];
console.log(merged); // 输出: [1, 2, 3, 4, 5]
这些示例展示了ES6的一些基本特性,实际开发中,你可以根据需要使用更多的ES6特性,如Promise、Set和Map、Iterable、Generator等。
在Vue.js中,组件是构建用户界面的基本单元。组件有三个主要的API:props、events和slots。
Props:
Props是组件外部传递数据给组件内部的一种方式。
Vue.component('my-component', {
props: ['message'],
template: '<div>{{ message }}</div>'
})
使用组件:
<my-component message="Hello!"></my-component>
Events:
Events是子组件向父组件发送消息的一种方式。
子组件:
this.$emit('myEvent', myData)
父组件:
<my-component @myEvent="handleEvent"></my-component>
methods: {
handleEvent(data) {
console.log(data);
}
}
Slots:
Slots是组件内部的插槽,用于插入内容。
父组件:
<my-component>
<p>This is some content</p>
</my-component>
子组件:
Vue.component('my-component', {
template: `<div><slot></slot></div>`
})
以上是三个API的简单示例,它们是Vue.js组件通信的基础。
jQuery是一个快速、简洁的JavaScript库,方便了HTML文档 traversing, event handling, animation 和Ajax interactions等一系列的操作,使得JavaScript的编写更加简便。
以下是一些使用jQuery的基本示例:
- 元素的显示与隐藏:
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
- 动画:
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle();
});
});
- 事件绑定:
$(document).ready(function(){
$("p").click(function(){
$(this).css("background-color", "yellow");
});
});
- AJAX请求:
$(document).ready(function(){
$("#b01").click(function(){
href = $("#test").attr("action");
data = $("#test").serialize();
$.ajax({
type: "POST",
url: href,
data: data,
success: function(data){
$("#div1").html(data);
}
});
});
});
- 链式调用:
$(document).ready(function(){
$("#btn1").click(function(){
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
});
});
- 对所有匹配元素进行迭代:
$(document).ready(function(){
$("button").click(function(){
$("p").each(function(i){
$(this).text("这是第 " + (i + 1) + " 个段落.");
});
});
});
- 事件冒泡:
$(document).ready(function(){
$("#div1").click(function(){
alert("这个事件将在每个子元素的点击事件后触发。");
});
});
- 获取和设置HTML内容:
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").html();
});
$("#btn2").click(function(){
$("#test2").html("Hello World!");
});
});
- 获取和设置文本内容:
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text();
});
$("#btn2").click(function(){
$("#test2").text("Hello World!");
});
});
- 获取和设置属性值:
$(document).ready(function(){
$("#btn1").click(function(){
$("#w3s").attr("href");
});
$("#btn2").click(function(){
$("#w3s").attr("href", "http://www.w3school.com.cn/jquery");
});
在Flutter Web项目中使用JavaScript工具类,你可以通过universal_html
包来访问Web特有的API。首先,确保在你的pubspec.yaml
文件中添加了universal_html
依赖。
dependencies:
flutter:
sdk: flutter
universal_html: ^2.1.0
然后,你可以创建一个Dart类来封装你的JavaScript工具方法。使用universal_html
提供的JsObject
来调用JavaScript函数。
import 'package:universal_html/prefer_universal/html.dart';
class JavaScriptUtils {
static void performJavaScriptAction(String actionName) {
final jsUtil = JsObject(context['JavaScriptUtils'] as JsFunction);
jsUtil.callMethod(actionName);
}
}
确保你的JavaScript文件已经被包含在项目中,并且在你的HTML文件中引入了这个JavaScript文件。
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<!-- ... -->
<script defer src="path_to_your_javascript_file.js"></script>
</head>
<!-- ... -->
</html>
在你的JavaScript文件中,定义JavaScriptUtils
类和相关方法。
// path_to_your_javascript_file.js
function performAction() {
// JavaScript code here
}
// 将函数暴露给Dart
window['JavaScriptUtils'] = {
performAction: performAction
};
现在,你可以在Dart代码中使用JavaScriptUtils.performJavaScriptAction('performAction')
来调用JavaScript中的函数了。
以下是一个简单的HTML5、CSS3和JavaScript代码示例,实现了一个图片九宫格效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片九宫格</title>
<style>
.container {
width: 300px;
height: 300px;
perspective: 1000px;
margin: 50px auto;
position: relative;
}
.container .cube {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: transform 1s;
}
.container .face {
position: absolute;
width: 100%;
height: 100%;
box-shadow: 0 0 5px #000;
line-height: 100px;
text-align: center;
font-size: 3em;
color: #fff;
}
.face1 {
background: url('img/1.jpg');
background-size: cover;
}
.face2 {
background: url('img/2.jpg');
background-size: cover;
transform: rotateY(90deg) translateZ(100px);
}
.face3 {
background: url('img/3.jpg');
background-size: cover;
transform: rotateY(-90deg) translateZ(100px);
}
.face4 {
background: url('img/4.jpg');
background-size: cover;
transform: rotateX(90deg) translateZ(100px);
}
.face5 {
background: url('img/5.jpg');
background-size: cover;
transform: rotateX(-90deg) translateZ(100px);
}
.face6 {
background: url('img/6.jpg');
background-size: cover;
transform: rotateY(180deg) translateZ(100px);
}
.face7 {
background: url('img/7.jpg');
background-size: cover;
transform: translateZ(-100px);
}
.face8 {
background: url('img/8.jpg');
background-size: cover;
transform: rotateY(90deg) translateZ(-100px);
}
.face9 {
background: url('img/9.jpg');
background-size: cover;
transform: rotateY(-90deg) translateZ(-100px);
}
</style>
</head>
<body>
<div class="container">
<div class="cube">
<div class="face face1">1</div>
<div class="face face2">2</div>
<div class="face face3">3</div>
<div class="face face4">4</div>
<div class="face face5">5</div>
<div class="face face6">6</div>
<div class="face face7">7</div>
<div class="face face8">8</div>
<div class="face face9">9</div>
</div>
</div>
<script>
const cube = document.querySel
<template>
<div id="app">
<transition name="fade">
<img v-if="show" key="logo" src="./assets/logo.png">
</transition>
<button @click="show = !show">Toggle Image</button>
</div>
</template>
<script>
export default {
name: 'App',
data() {
return {
show: true
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
opacity: 0;
}
</style>
这个例子展示了如何在Vue.js应用中使用过渡效果。通过点击按钮来切换一个图片的显示与隐藏,并且在这个过程中应用了一个淡入淡出的过渡效果。这个例子简单明了,并且教会了开发者如何在Vue.js中使用过渡。