The bar magnet on the left attracts the magnet on the right when opposite poles of the two magnets are facing one another.

Rotate the bar magnet by moving the mouse across the screen from left to right. As the poles of the bar magnet alternate, the magnet in the motor on the right moves back and forth as it is alternately attracted and repelled by the changing polarity of the bar magnet.