My jQuery function is changing the background-position
property of three elements when the user moves their mouse, but it's causing some performance problems.
It's worth mentioning that the background images of these elements are SVGs.
Here's an example of the code:
$(window).on('mousemove', function(event) {
window.requestAnimationFrame(function() {
$banner.find('.pattern').each(function(key) {
var modifier = 20 * (key + 1);
$(this).css({
'background-position': (event.pageX / modifier)+'px '+(event.pageY / modifier)+'px'
});
});
});
});
You can view my working code here: https://codepen.io/thelevicole/project/full/DarVMY/
I am using window.requestAnimationFrame()
and I have added the css attribute
will-change: background-position;
to each element.
However, this effect is causing lagging issues, especially on larger window sizes.
I suspect that the problem lies in using SVGs for the background images instead of PNGs. The reason behind using SVGs is the high pixel density screens.
If anyone has suggestions on how to improve the frames per second without resorting to PNGs, I would greatly appreciate it. Thank you.