Driven by big data and artificial intelligence, online teaching tools like virtual labs are emerging, yet they often suffer from low resource utilisation, limited load capacity, and high costs. To overcome these challenges, this study develops a cloud-based infrastructure leveraging the on-demand and elastic features of cloud computing. An enhanced load-balancing algorithm integrating particle swarm optimisation and cuckoo search is proposed, along with a virtual-machine migration strategy, to build a virtual teaching lab. Results show that the algorithm achieves 12.35% CPU utilisation, 17.23% memory utilisation, 33.02% bandwidth utilisation, and a maximum response delay of 210.11 ms. The migration strategy also proves more cost-effective and efficient than alternatives. In practice, 98% of students improved their experimental skills using the virtual lab, and 52.72% scored above 90, outperforming students in traditional lab teaching. The virtual lab demonstrates stable loading, high efficiency, low cost, and supports educational digital transformation.