I achieved this using an app called
Tasker (currently £4).
1) New profile: on the Tasker homepage, click the plus icon to create a new profile, and don't bother entering a name.
2) Choose the context: go State > Net > WiFi Connected and click the green tick icon to confirm.
3) Select task: you then have to create a task to run when this context is active. There should be a "Task Selection" popup, and you need to click New Task, and again you don't need to bother with a name. Then you should be on the Edit Task page. Click the plus icon to add an action > select Net > AutoSync > Select On and accept. Finally, finish the task by clicking the tick icon.
4) Repeat steps 1-3 for the AutoSync Off task i.e. set the context to be WiFi Connected, but then check Invert on the next page, and set the Task to be AutoSync Off.
That's it - your AutoSync should turn on when you connect to a WiFi network, and off again when you disconnect.
Alternative Apps
-
Locale (with a plugin) - expensive at over £6
-
AutoSync Account Activator - FREE, and looks good (though I haven't tested it), but too fine-grained controls (i.e. must set individual sync rules for each sync account rather than just overall enable/disable Auto-Sync, ALSO must select specific WiFi networks as triggers, rather than just overall "WiFi connected")
-
Toggle Settings - FREE, but not very intuitive interface - I gave up quite quickly.
If anyone can figure out how to achieve this behaviour using a free app, then please let me know in the comments!