Note that the electroscope here is always neutral, even at the beginning. If you bring the insulating charged rod closer to the electroscope, you'll be able to see that the electrons (in blue) and the positive charges (red) on the electroscope cancel one another out.
When you bring the rod closer to the electroscope, the electroscope acts like it is charged even though it isn't. The electroscope becomes polarized. If the rod has a positive charge, electrons in the electroscope are attracted toward the rod and therefore the electrons move toward the top plate of the electroscope, leaving a net positive charge on and near the needle, which deflects. If, instead, the rod has a negative charge, electrons in the top plate of the electroscope are repelled by the electrons on the rod. The electrons on the electroscope move toward the needle, which then deflects.
Moving the rod away reverses the movement of the electrons, so the needle deflects less.
Simulation written by Andrew Duffy, and first posted on 12-27-2018.
The counter has been running on this page since 12-27-2018. The number of people accessing the page since then is: