I have got a problem with the background fading script. The function causes high CPU usage (30-40%, tested on Intel i7-4810MQ). The problem is present only on Chrome and Opera. With Firefox everything work fine. It's quite a big problem for me, because when my website is open the laptop starts heating and the fan is getting louder. Here is the jsfiddle code: http://jsfiddle.net/jwdu8mkq/4/ JS:
$(document).ready(function() {
var background = {};
background.num = 3;
background.min = 1;
background.max = 6;
background.firstShow = true;
background.swap = function() {
var swapFirst = false;
var swapSecond = false;
if($('.background.img1').attr('image-number') == this.num) {
$('.background.img1').fadeOut(2000);
swapSecond = true;
} else if($('.background.img2').attr('image-number') == this.num) {
$('.background.img2').fadeOut(2000);
swapFirst = true;
} else {
swapFirst = true;
}
this.num++;
if(this.num < this.min) {
this.num = this.min;
} else if(this.num > this.max) {
this.num = this.min;
}
if(swapFirst) {
$('.background.img1').css('background-image', 'url(\'http://lokeshdhakar.com/projects/lightbox2/images/image-' + this.num + '.jpg\')');
$('.background.img1').attr('image-number', this.num);
$('.background.img1').fadeIn(this.firstShow ? 0 : 2000);
this.firstShow = false;
} else if(swapSecond) {
$('.background.img2').css('background-image', 'url(\'http://lokeshdhakar.com/projects/lightbox2/images/image-' + this.num + '.jpg\')');
$('.background.img2').attr('image-number', this.num);
$('.background.img2').fadeIn(this.firstShow ? 0 : 2000);
this.firstShow = false;
}
}
setInterval(function() { background.swap() }, 6000);
background.swap();
});
CSS:
.background {
filter: blur(10px);
-webkit-filter: blur(10px);
-moz-filter: blur(10px);
-o-filter: blur(10px);
-ms-filter: blur(10px);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: -1000;
display: none;
}
HTML:
<div class="background img1"></div>
<div class="background img2"></div>
Is there any chance to optimize this code? Or maybe can I replace it with something similar that has the same effect?
Sorry if the question is obvious, I'm definitely not a good web designer.
Well, a different approach might be to use a canvas then. This is a bit more complicated but conceptually mirrors your initial attempt. We will load up an array of images then cycle them by calling crossfadeTo(). There is a bunch of stuff you can do to improve on this, but it might get you going again. As an example, the first image crossfading to itself is a little wonky.
This solution uses almost no CPU under FF, Chrome and Edge.
You might try using CSS transitions to do most of the lifting for you. If you define 6 classes each with a background then you can swap the classes and simplify things quite a lot.
This strategy allows you to swap classes rather than divs with a cross fade. The fade effect provided by a CSS transition.
For full effect you need to view a cycle so that the images are cached. For your proper implementation, you will want to pre-load the images.
This is the CPU use I see on my system in chrome. Your code for the left half and mine for the right.