Veins like almost everything else in the body have nerves in them, and so they register pain. If you slap them they react the same way you and I do to pain, they move away from it and contract, making them smaller (OK not exactly the way you and I react, you and I might react by punching someone in the face, but veins lack the hands for this and so they contract.)
So why do some nurses and (especially) doctors still slap? Simple, it’s a learnt behaviour. Not during their training, but afterwards. During training nurses are taught to palpate a vein (more on this in a moment), but shortly after they start working on a ward someone will tell them they should ‘slap’ a vein, and so they do. Don’t think too badly of them for falling into this behaviour, peer education isn’t always a good thing. Just think of the amount of people who still lick needles because the person who showed them how to inject licked his. Nurses and doctors are just like the rest of us, only human.