if offset is undefined, the above offset.attr will throw.
This if is either unnecessary or needs to be moved.