I was always curious, why do chameleons have this strange gait?
The movement you observed serves two functions:
- Imitation of leaves to protect against predators
- Improved stereoscopic vision while scanning for their own prey
Imitation of leaves (mimesis)
Chameleons in the wild live in trees and are surrounded by leaves. In order to protect themselves from predators they move forth and back to blend with leaves moving in the wind.
Also note that their torso shape resembles a leaf:
Why do they move like this even when there is no wind and they are not sitting on a tree? I suppose they just follow their instincts. Evolution "did not prepare" them for sitting in a cage. The lack of ability to know when to move and when not was not a evolutionary disadvantage so there was not pressure to get rid of it.
Improved stereoscopic vision
Chameleons are predators that use their tongue to catch prey e.g. grasshoppers. While they are looking for their next target, they scan their surroundings with both eyes independently, i.e. one scans the left hemisphere, the other eye scans the right hemisphere, thus during this time they have no stereoscopic vision.
Moving forth and back helps alleviating this. They can estimate the distance to their distance better through the use of motion parallax (closer objects seem to move more than object further away when we move sideways). Once they found a target they turn in its direction and then use both eyes to focus on it, preparing to shoot out their tongue.
(Disclaimer: I am not a biologist. This is from my own reasoning and the German Wikipedia article about Chameleons)
Predators are very good at spotting moving targets, as movements will easily stand out from inanimate objects and vegetation. Most prey will prefer to hide or just run if they are spotted by predators. But the chameleons prefer to blend in with the vegetation instead. This gives them the advantage that they may move in plain sight, and still not be recognized as prey.
They go back and forth for the same reason that stick insects and caterpillars do. to imitate wind and foliage, leaf trembling, to ward of their predators. A possible additional reason is to gain added perspective of their environment.
Here is example of same principle in stick insects:
They don't have the physiology or habitat where they can escape or hide from predators hence the open air hiding tactic.
They don't have running muscles and skeleton. They couldn't run if they wanted to (slower than a snake or bird) and they couldn't hide because trees also don't offer crevices like cliffs and walls do. Their behavior is good for catching more prey and for staying hidden.
They are related to Iguanadons, which are adapted to moving in the sea (cold). Chameleons often live in the shadows, 130 of 171 species are forest dwellers, and most of the others live in trees of savannas and steppes, only a few live on the floor.
They don't need to be warm to hunt, their tongue is still one of the fastest things in vertebrates, so they can hunt when they are cold.
Their prey is not very intelligent: birds, snakes and more rarely mammals. because they are too slow to run away, they don't have hiding places except for the branches, they have a very cautious walk which is disguised, with slow head movements, very active eyes, and ability to keep head still in between steps. hunters will tell you that animals are very oblivious when their head is moving, they generally need to stop to see you, and chameleons stop constantly. there was research to understand their slow muscles, the muscles have slow fibers, they have a heart which is not as capable as fast lizards. http://onlinelibrary.wiley.com/doi/10.1002/jez.1402630102/abstract