Crossover trials are widely used to assess the effect of a reversible exposure on an outcome of interest. To gain further insight into the underlying mechanisms of this effect, researchers may be interested in exploring whether or not it runs through a specific intermediate variable: the mediator. Mediation analysis in crossover designs has received scant attention so far and is mostly confined to the traditional Baron and Kenny approach. We aim to tackle mediation analysis within the counterfactual framework and elucidate the assumptions under which the direct and indirect effects can be identified in AB/BA crossover studies. Notably, we show that both effects are identifiable in certain statistical models, even in the presence of unmeasured time-independent (or upper-level) confounding of the mediator-outcome relation. Employing the mediation formula, we derive expressions for the direct and indirect effects in within-subject designs for continuous outcomes that lend themselves to linear modelling, under a large variety of settings. We discuss an estimation approach based on regressing differences in outcomes on differences in mediators and show how to allow for period effects as well as different types of moderation. The performance of this approach is compared to other existing methods through simulations and is illustrated with data from a neurobehavioural study. Lastly, we demonstrate how a sensitivity analysis can be performed that is able to assess the robustness of both the direct and indirect effect against violation of the "no unmeasured lowerlevel mediator-outcome confounding" assumption.