I have an image needs to be larger than the area set for viewing that image. I’m trying to come up with a reliable way to view different portions of that image via a smooth transition. Currently, I have something that works nicely in Firefox, but is very choppy in Chrome.
The div that displays the image has the id of “navViewer” and starts out with the class=“topPosition”. When the button is clicked, Javascript is used to toggle between assigning “topPosition” and “botPosition” to that div. These two classes each have a setting for the position of the background image and set a transition time.
Is there something that can be done so that the transition works smoothly in Chrome?
One thing I tried that had no effect: adding the following line to #navViewer:
transform: translateZ(0);
There are mentions, in other questions on this issue, of forcing the Chrome browser to use hardware acceleration, and the above was said to be a hack to cause this to happen. But either the suggestion is obsolete, or my simply adding it to #navViewer was incorrect/naive.
The goal is to be able to view (with nice transitions) different areas of an image, where the image is much larger than the container that displays the image. I’d like to do this without scrollbars, just a click to a button. If there is a different scheme to use besides using a background image and changing its position via assigning different classes, I’d love to hear the suggestion.
Here is the code.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TransitionQuestion</title>
<style>
#navViewer {
width: 900px;
height: 500px;
border: 1px solid;
background-size: 900px;
background-image: url('http://adonax.com/main/images/Cheryl.jpg');
}
.topPosition {
background-position: 0px 0px;
transition: background-position 1s;
}
.botPosition {
background-position: 0px -625px;
transition: background-position 1s;
}
</style>
</head>
<body>
<button id="goNext">next View</button>
<div id="navViewer" class="topPosition"> </div>
<script>
const viewer = document.querySelector("#navViewer");
const nextButton = document.querySelector("#goNext");
nextButton.addEventListener("click", goNext, false);
var positionIsAtTop = true;
function goNext() {
if (positionIsAtTop) {
viewer.className = "botPosition";
} else {
viewer.className = "topPosition"
}
positionIsAtTop = !positionIsAtTop;
}
</script>
</body>
</html>