Merge condition and pass viewport

Reduce access to the DOM and merge conditions on one line