更改随机图像的不透明度(切换)
Change opacity of random image (toggle)
我找了大约 2 个小时的答案,但没有成功:
你如何制作一个动画,以缓慢的淡入随机改变网格中图像的不透明度?
把组件分解一下,应该能搞清楚其中的逻辑:
- 3x3 网格中的 9 个头像。
- 大约。每 3 秒,就有一个新的 "active" 一个
- 一切不活跃的都淡出
因此,我们可以搜索如何在活动元素和非活动元素之间制作 3x3 grid in css, figure out how to rig a 3 second timer with Javascript to change an element's class, and with a class toggle the fade。
如果你能把所有这些放在一起,你就得到了你的组件!
这不是最优雅的代码,因为我只用了大约 5 分钟就完成了,但您可能会理解并改进它:
jQuery
$(function(){
setInterval(function(){
var i = Math.floor(Math.random() * 10) + 1;
$("[data-highlight=1]").attr("data-highlight", "0").animate({
opacity: 0.2
}, 1000);
$("#photo-" + i).attr("data-highlight", "1").animate({
opacity: 1
}, 1000);
}, 3000);
});
CSS
.photo {
width: 150px;
height: 150px;
background-color: #F00;
font-size: 2em;
color: #FFF;
float: left;
opacity: 0.2;
}
HTML
<div class="photo" id="photo-1" data-highlight="0">DIV</div>
<div class="photo" id="photo-2" data-highlight="0">DIV</div>
<div class="photo" id="photo-3" data-highlight="0">DIV</div>
<div class="photo" id="photo-4" data-highlight="0">DIV</div>
<div class="photo" id="photo-5" data-highlight="0">DIV</div>
<div class="photo" id="photo-6" data-highlight="0">DIV</div>
<div class="photo" id="photo-7" data-highlight="0">DIV</div>
<div class="photo" id="photo-8" data-highlight="0">DIV</div>
<div class="photo" id="photo-9" data-highlight="0">DIV</div>
<div class="photo" id="photo-10" data-highlight="0">DIV</div>
您链接的网页中的代码执行以下操作(为简单起见,我对其进行了一些修改):
$(function(){
setInterval(setImageOpacity, 2000); // Every 2 seconds call function setImageOpacity()
});
function setImageOpacity() {
var images = $('#careerImageTable img'); // Gets all images from the grid
var currentIndex = -1;
$.each(images, function (index, item) { // Loops through each image
if ($(item).css('opacity') == 1) { // Checks the opacity of the current image
currentIndex = index; // If opacity == 1 then thats the current index
return false;
}
});
var nextIndex = currentIndex;
while (nextIndex == currentIndex) {
nextIndex = Math.floor(Math.random() * images.length); // Will loop until a different index is found
}
images.eq(currentIndex).fadeTo(1000, 0.3); // The opacity animations
images.eq(nextIndex).fadeTo(1000, 1);
}
我找了大约 2 个小时的答案,但没有成功: 你如何制作一个动画,以缓慢的淡入随机改变网格中图像的不透明度?
把组件分解一下,应该能搞清楚其中的逻辑:
- 3x3 网格中的 9 个头像。
- 大约。每 3 秒,就有一个新的 "active" 一个
- 一切不活跃的都淡出
因此,我们可以搜索如何在活动元素和非活动元素之间制作 3x3 grid in css, figure out how to rig a 3 second timer with Javascript to change an element's class, and with a class toggle the fade。
如果你能把所有这些放在一起,你就得到了你的组件!
这不是最优雅的代码,因为我只用了大约 5 分钟就完成了,但您可能会理解并改进它:
jQuery
$(function(){
setInterval(function(){
var i = Math.floor(Math.random() * 10) + 1;
$("[data-highlight=1]").attr("data-highlight", "0").animate({
opacity: 0.2
}, 1000);
$("#photo-" + i).attr("data-highlight", "1").animate({
opacity: 1
}, 1000);
}, 3000);
});
CSS
.photo {
width: 150px;
height: 150px;
background-color: #F00;
font-size: 2em;
color: #FFF;
float: left;
opacity: 0.2;
}
HTML
<div class="photo" id="photo-1" data-highlight="0">DIV</div>
<div class="photo" id="photo-2" data-highlight="0">DIV</div>
<div class="photo" id="photo-3" data-highlight="0">DIV</div>
<div class="photo" id="photo-4" data-highlight="0">DIV</div>
<div class="photo" id="photo-5" data-highlight="0">DIV</div>
<div class="photo" id="photo-6" data-highlight="0">DIV</div>
<div class="photo" id="photo-7" data-highlight="0">DIV</div>
<div class="photo" id="photo-8" data-highlight="0">DIV</div>
<div class="photo" id="photo-9" data-highlight="0">DIV</div>
<div class="photo" id="photo-10" data-highlight="0">DIV</div>
您链接的网页中的代码执行以下操作(为简单起见,我对其进行了一些修改):
$(function(){
setInterval(setImageOpacity, 2000); // Every 2 seconds call function setImageOpacity()
});
function setImageOpacity() {
var images = $('#careerImageTable img'); // Gets all images from the grid
var currentIndex = -1;
$.each(images, function (index, item) { // Loops through each image
if ($(item).css('opacity') == 1) { // Checks the opacity of the current image
currentIndex = index; // If opacity == 1 then thats the current index
return false;
}
});
var nextIndex = currentIndex;
while (nextIndex == currentIndex) {
nextIndex = Math.floor(Math.random() * images.length); // Will loop until a different index is found
}
images.eq(currentIndex).fadeTo(1000, 0.3); // The opacity animations
images.eq(nextIndex).fadeTo(1000, 1);
}