England reached 324 for nine - a second innings lead of 278 - when bad light stopped play for the day at Kandy.
Foakes was batting on 51 alongside James Anderson on four.
Spinner Akila Danajanya, whose action is under investigation by the International Cricket Council, claimed six wickets on the turning pitch.
He trapped Root lbw and then bowled Sam Curran for nought with his next ball. Adil Rashid thwarted the hat-trick but soon fell to Dananjaya's guile for two.
It was his third five wicket haul in just his fifth Test for Dananjaya, who must rush off to Australia after the game for an ICC examination of his bowling.
He however could not stop Root and Foakes swinging the game.
Root reached his 15th Test ton soon after tea, making the sweep and reverse sweep valuable weapons, as he hit 10 fours and two sixes in his 146-ball knock.
Root raised his bat to a standing ovation from travelling English fans who also lauded Foakes.
Root made an 82-run seventh wicket stand with Foakes, who reached his fifty with a six off Dilruwan Perera. The hit turned out to be the last ball of the day with dark clouds gathering and thunder heard in the distance.
As he did in his sparkling century on his Test debut in the opening game of the series, Foakes mixed caution and aggression to push up the England score.
Every one of England's top seven batsmen were out attempting a sweep of some description.
Jos Buttler dragged one of Dananjaya's deliveries onto his stumps while trying to reverse sweep on 34. Moeen Ali was trapped lbw for 10 after failing to connect with an attempted sweep.
Earlier, left-hander Rory Burns registered his maiden Test half-century in just his second match. He was trapped lbw off Malinda Pushpakumara for 59.
Perera wasted little time in sending nightwatchman Jack Leach back to the pavilion for one in the second over of the day.
Burns then put together 73 runs for the second wicket with Keaton Jennings, who made 26, to steady the innings and help England overcome their 46-run deficit.
First Published: November 16, 2018, 5:46 PM IST