I have first encountered this result in the book Van Rooij, Schikhof: A Second Course on Real Functions.
I will copy here the text of Exercise 9.P.
Let $0<\alpha<1$. Let $f\colon[0,1]\to\mathbb R$, $f(0)=f(1)$.
(i) Show that if $\alpha$ is one of the numbers $\frac12,\frac13,\frac14,\dots$ and if $f$ is continuous, then the graph of $f$ has a horizontal chord of length $\alpha$, i.e., there exists $s,t\in[0,1]$ with $f(s)=f(t)$ and $|s-t|=\alpha$.
(ii) The proof you gave probably relies on Darboux continuity. Prove, however, that the given continuity condition on $f$ may not be weakened to Darboux continuity. (Take $\alpha:=\frac12$ and start with a function on $(0,\frac12]$ that maps every subinterval of $(0,\frac12]$ onto $\mathbb R$.)
(iii) Now let $\alpha\notin\{\frac12,\frac13,\dots\}$. Define a continuous function on $[0,1]$ with $f(0)=f(1)$ whose graph has no horizontal chord of length $\alpha$. (Choose $f$ such that $f(x+\alpha)=f(x)+1$ for $x\in[0,1-\alpha]$.)
For this questions only the first and the third part are relevant. And the first part has been already solved in other answers.
Let us spell out in details construction following the hint from the third part. (Although the hint given there already gives quite a good idea how to proceed.) This is slightly different from the examples given in other answers.
We want to have $f(x+\alpha)=f(x)+1$. Notice that this also implies $f(x+k\alpha)=f(x)+k$.
If we define the function on the interval $[0,\alpha]$, then the above condition determines the function $f$ uniquely on the rest of the interval $[0,1]$.
We want to have $f(0)=0$ and $f(\alpha)=f(1)$.
Let us denote $n=\left\lfloor\frac1\alpha\right\rfloor$, i.e., $n$ is the largest integer such that $n\alpha<1$.
Then we have $1-n\alpha\in(0,\alpha)$. We need to choose $f(1-n\alpha)=-n$ in order to get $f(1)=0$.
(Notice that this cannot be done if $n\alpha=1$. It is possible only if $0<1-n\alpha<\alpha$, since the values $f(0)$ and $f(1)$ are already prescribed.)
Then arbitrary function defined as above (i.e., with the prescribed values in the points $0$, $1-n\alpha$, $\alpha$ and extended from $[0,\alpha]$ to the whole interval using $f(x+\alpha)=f(x)+1$) satisfies the required conditions.
Such function for a specific choice of $\alpha$ is illustrated in this picture:

For comparison, here is plot of Lévy's function mentioned in Aryabhata's answer for the same value of $\alpha$ can be checked on WolframAlpha.