js中使用单独的一张图片做加载效果
时间: 2023-04-02 20:03:31 浏览: 86
可以使用CSS3的动画效果,将图片设置为背景,然后使用@keyframes定义动画,通过animation属性将动画应用到元素上,从而实现加载效果。具体实现可以参考网上的教程或者使用现成的loading库。
相关问题
js图片加载效果(延迟加载+瀑布流加载)
实现图片延迟加载可以使用以下代码:
1. 先将所有需要加载的图片的src属性设置为一个空白图片或者占位符图片,例如:
```html
<img class="lazy" data-src="图片真实路径" src="占位符路径">
```
2. 使用JavaScript获取所有需要延迟加载的图片元素,并监听页面滚动事件:
```javascript
var lazyLoadImg = document.getElementsByClassName('lazy');
var viewHeight = document.documentElement.clientHeight;
function lazyLoad() {
for(var i = 0, len = lazyLoadImg.length; i < len; i++) {
var offsetTop = lazyLoadImg[i].getBoundingClientRect().top;
if(offsetTop < viewHeight) {
lazyLoadImg[i].src = lazyLoadImg[i].getAttribute('data-src');
lazyLoadImg[i].classList.remove('lazy');
}
}
}
window.addEventListener('scroll', lazyLoad);
```
3. 当滚动到图片位置时,将data-src属性赋值给src属性即可。
实现瀑布流加载可以使用以下代码:
1. 设置图片容器div的列数和图片间距,例如:
```css
.waterfall {
column-count: 4;
column-gap: 20px;
}
```
2. 使用JavaScript获取所有需要加载的图片,并计算每张图片的宽度和高度:
```javascript
var waterfall = document.getElementsByClassName('waterfall')[0];
var waterfallImg = waterfall.getElementsByTagName('img');
var columnNum = 4;
var imgWidth = (waterfall.offsetWidth - (columnNum - 1) * 20) / columnNum;
var imgHeightArr = [];
for(var i = 0, len = waterfallImg.length; i < len; i++) {
var imgHeight = imgWidth * waterfallImg[i].naturalHeight / waterfallImg[i].naturalWidth;
imgHeightArr.push(imgHeight);
waterfallImg[i].style.width = imgWidth + 'px';
}
```
3. 根据每张图片的高度,计算每列的高度,并将图片插入到最短的一列:
```javascript
function minIndex(arr) {
var min = arr[0];
var index = 0;
for(var i = 1, len = arr.length; i < len; i++) {
if(arr[i] < min) {
min = arr[i];
index = i;
}
}
return index;
}
for(var i = 0, len = waterfallImg.length; i < len; i++) {
var minIndex = minIndex(imgHeightArr);
waterfallImg[i].style.top = imgHeightArr[minIndex] + 'px';
waterfallImg[i].style.left = (imgWidth + 20) * minIndex + 'px';
imgHeightArr[minIndex] += imgHeightArr[i];
}
```
4. 当新图片加载完成时,重复步骤2和步骤3即可。
vue3中使用three.js加载fbx模型
在Vue3中使用three.js加载fbx模型,可以按照以下步骤进行操作:
1. 首先,在Vue3项目中安装three.js和fbx-loader:
```
npm install three fbx-loader --save
```
2. 在Vue3中创建一个Three.js组件,可以参考如下代码:
```javascript
<template>
<div ref="container"></div>
</template>
<script>
import * as THREE from 'three'
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'
export default {
name: 'ThreeScene',
mounted() {
// 创建场景
this.scene = new THREE.Scene()
// 创建相机
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
this.camera.position.set(0, 0, 5)
// 创建渲染器
this.renderer = new THREE.WebGLRenderer({ antialias: true })
this.renderer.setSize(window.innerWidth, window.innerHeight)
this.renderer.setPixelRatio(window.devicePixelRatio)
// 将渲染器添加到页面中
this.$refs.container.appendChild(this.renderer.domElement)
this.animate()
// 加载fbx模型
const loader = new FBXLoader()
loader.load('/path/to/model.fbx', (fbx) => {
this.scene.add(fbx)
})
},
methods: {
animate() {
requestAnimationFrame(this.animate)
this.renderer.render(this.scene, this.camera)
}
}
}
</script>
```
在mounted函数中,我们创建了场景、相机和渲染器,并将渲染器添加到Vue组件的DOM元素中。然后,使用FBXLoader加载fbx模型,并将模型添加到场景中。最后,使用requestAnimationFrame函数来循环渲染场景。
3. 在Vue3中使用ThreeScene组件
在Vue3项目中,我们可以使用ThreeScene组件来加载fbx模型。例如:
```html
<template>
<div>
<ThreeScene />
</div>
</template>
<script>
import ThreeScene from './ThreeScene.vue'
export default {
components: {
ThreeScene
}
}
</script>
```
这样就可以在Vue3中加载fbx模型了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.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)