I have a section of code which is supposed to move this image ( id = 'background' ), which I have downloaded locally, and is quite large. It is supposed to move when I hover over top of a certain div(s). This then changes the opacity CSS value, which in turn is detected by the js, which then makes the image move. The js code looks like this:
setInterval(function(){ var element = document.getElementById('background'), style = window.getComputedStyle(element), left = style.getPropertyValue('left'); left = left.replace(/px/g, "") left = parseInt(left,10) if(getOpacity('rightbar') == .5){ document.getElementById('background').style.left = left - 8 + 'px' } if(getOpacity('leftbar') == .5){ document.getElementById('background').style.left = left + 8 + 'px' } },10) The getOpacity(idName) function looks like this:
function getOpacity(param){ var element = document.getElementById(param), style = window.getComputedStyle(element), opacity = style.getPropertyValue('opacity'); return opacity } So the problem is that, no matter what movement values or setInteveral time I use, the animation always makes out to be laggy. Is there a way to make this smooth with vanilla js, or better yet, to scrap the opacity detection and do it all with CSS?
It works fine when I put the above code in a fiddle, but when it actually runs full browser (on my personal chrome window), it looks like this.
Note: I am running this full browser window on a 4k monitor, is this just too much for chrome to handle?
requestAnimationFrame()on browsers that support it.