javascript - document.createElement() style images does not work
问题描述
Check the code below. In the function called createElement
I am taking random image from images
array but the problem is I am getting single image animated.
My main goal is to make animation with random images from those 4 images, not single image like now. How can I fix that?
var maxElements = 20;
var duration = 4000;
var toAnimate = [];
var radius = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;
var distance = radius / 4 <= 250 ? 250 : radius / 4;
//var images = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];
var images = ['https://preview.ibb.co/gZfv09/img2.jpg', 'https://image.ibb.co/bDCBOU/img5.jpg', 'https://preview.ibb.co/gZfv09/img2.jpg', 'https://image.ibb.co/bDCBOU/img5.jpg'];
console.log(images);
var createElements = (function() {
var fragment = document.createDocumentFragment();
for (var i = 0; i < maxElements; i++) {
var el = document.createElement('div');
el.classList.add('particule');
el.style.images = images[anime.random(0, 4)];
toAnimate.push(el);
fragment.appendChild(el);
}
document.body.appendChild(fragment);
})();
var animate = function(el, i) {
var angle = Math.random() * Math.PI * 3;
anime({
targets: el,
translateX: [0, Math.cos(angle) * distance],
translateY: [0, Math.sin(angle) * distance],
scale: [{
value: [0, .1],
duration: 4000,
easing: 'easeOutBack'
},
{
value: 0,
duration: 4000,
delay: duration - 8000,
easing: 'easeInBack'
}
],
offset: (duration / maxElements) * i,
duration: duration,
easing: 'easeOutSine',
loop: true
});
}
toAnimate.forEach(animate);
.particule {
position: absolute;
top: 5%;
left: 10%;
width: 70rem;
height: 70rem;
margin: -.5rem 0 0 -.5rem;
border: 1px solid currentColor;
transform: scale(0);
background-image: url('https://preview.ibb.co/gZfv09/img2.jpg');
background-size: cover;
background-repeat: no-repeat;
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.js"></script>
<div class="particule">
<img id="img-1" src="">
<img id="img-2" src="">
</div>
解决方案
You are very close, you just need to set the backgroundImage
property of styles
.
I've updated the random selection part to avoid attempting to access indices of the images
index which are not present.
var maxElements = 20;
var duration = 4000;
var toAnimate = [];
var radius = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;
var distance = radius / 4 <= 250 ? 250 : radius / 4;
//var images = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];
var images = ['https://preview.ibb.co/gZfv09/img2.jpg', 'https://image.ibb.co/bDCBOU/img5.jpg', 'https://preview.ibb.co/gZfv09/img2.jpg', 'https://image.ibb.co/bDCBOU/img5.jpg'];
console.log(images);
var createElements = (function() {
var fragment = document.createDocumentFragment();
for (var i = 0; i < maxElements; i++) {
var el = document.createElement('div');
el.classList.add('particule');
var selectedImage = Math.floor(Math.random() * images.length);
el.style.backgroundImage = `url('${images[selectedImage]}')`; // template literal
toAnimate.push(el);
fragment.appendChild(el);
}
document.body.appendChild(fragment);
})();
var animate = function(el, i) {
var angle = Math.random() * Math.PI * 3;
anime({
targets: el,
translateX: [0, Math.cos(angle) * distance],
translateY: [0, Math.sin(angle) * distance],
scale: [{
value: [0, .1],
duration: 4000,
easing: 'easeOutBack'
},
{
value: 0,
duration: 4000,
delay: duration - 8000,
easing: 'easeInBack'
}
],
offset: (duration / maxElements) * i,
duration: duration,
easing: 'easeOutSine',
loop: true
});
}
toAnimate.forEach(animate);
.particule {
position: absolute;
top: 5%;
left: 10%;
width: 70rem;
height: 70rem;
margin: -.5rem 0 0 -.5rem;
border: 1px solid currentColor;
transform: scale(0);
background-image: url('https://preview.ibb.co/gZfv09/img2.jpg');
background-size: cover;
background-repeat: no-repeat;
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.js"></script>
<div class="particule">
<img id="img-1" src="">
<img id="img-2" src="">
</div>
推荐阅读
- javascript - '无法读取未定义事件的属性' Vue.JS
- mips - 我想把一个大写字母的字符串变成小写字母
- javascript - JS:如果播放器嵌入到 iframe 中,则使用 mediaSession 的动作处理程序
- c# - 如何在 .NET 6.0 中构建 WebApplication 构建器之前访问配置变量?
- kubernetes - 部署/xyz 是应用程序 xyz-abc-integ 和 xyz-integ 的一部分
- selenium-chromedriver - 无法停止进程;它目前没有运行
- dbt - 在 dbt 中测试单个 CTE
- laravel - Vue.js 属性或方法“车辆”未在实例上定义,但在渲染期间引用
- python - 如何从python中的其他文件设置类的变量
- c++ - 为什么 std::fstream 在 Windows 上比 WriteFile 快