I made a fade in/out for a simple polygon that fades in and out, because of the variation of the Z coordinate of that polygon.
#version 330 core
in vec4 fragColor;
out vec4 finalColor;
in float zPos;
vec4 color = fragColor;
float partA = -1.0 * zPos;
float partB = partA - 7.0;
color.a = 1.0 - partB;
finalColor = color;
zPos is always negative in my case, so in partA I make it a positive value, then in partB I decrease it by 7. The value in partB is used to get alpha value for the color of the polygon.
The initial value of zPos is -8.0, then it increases to -7.0. This is a fade in. Then the zPos from value -7.0 dereases again to -8.0. This way i get a fade out.
In this video a recorded the game that uses this fragment shader for fading in/out the squares that are in background.
Hope this will help you to write the right shader for your problem. :)
P.S. Sorry for my english, if i made any mistakes.