Modern loitering laws in small-town America, post-Civil-War, were typically originally intended to enslave the entire population of black men remaining in town, or otherwise drive them out. https://www.youtube.com/watch?v=j4kI2h3iotA It took most of a century for the economy to really convert everything over to an hourly-pay model. This is not the only reason, but it is the dominant, proximate one that enshrined the practice.
Places with less black-white racial animus were comfortable adopting animus for other minorities, or for the poor in general. Post-Civil-Rights, loitering laws (and a panoply of other practices ranging from swimming pools to mortgage approvals to cul-de-sacs) were exploited not to enslave, but principally to simply eject categories of people.
For a take on the origins of the Anglo cultural tradition of persecuting the poor in general, this goes a lot further back - https://www.youtube.com/watch?v=2ec9Al5ezYs