Keeping the viewport in the same place when showing answers in Cloze completions

Right now when I review cloze with a lot text (let’s call them long, vertical clozes), if the cloze to be completed is at the bottom of the flashcard, then I need to scroll there before I know what I need to complete. When I hit “Show answer”, the viewport goes back to the top, so I need to scroll to the bottom again to see if my guess was correct. It would be handy if the viewport stayed where it was when I hit “Show answer” (it would also be nice to have an option for scrolling automatically so that the missing cloze is in the viewport when the card is presented, but that is way less critical).

2 Likes

Just to give you a better idea of what I mean:
This is what happens now:

before

This is what I would like to happen instead:

after

https://ankiweb.net/shared/info/1990296174

That’s cool, but it’s not going to help me on the Android app right? That’s where I struggle the most with this, on PC it’s mostly fine, the screen is large and it already doesn’t scroll automatically to the top after revealing the answer

Custom note types are supported everywhere.

Alternatively, instead of the ready-made note type, you can ask for help in modifying the current one.

1 Like

So, if I’m understanding you correctly, you’re telling me to instead modify the Cloze note type so that it does what I want, I imagine with some custom Javascript taken from that extension you shared? That doesn’t sound too bad, but then the changes would affect both desktop and the app. And I wonder if that’s going to lead to some undesired effects, or if I can even implement different behaviours if I’m on mobile vs desktop from the JS code

Here is an example of how to do it with JavaScript:

Back template:

<div id=c>{{cloze:Text}}</div>
{{Back Extra}}


<script>
var e = document.getElementById("c");

e.innerHTML = e.innerHTML.replaceAll(`<span class="cloze"`, `<span id=answer class="cloze"`);
</script>
2 Likes

Yes, you can. For example, if you want some piece of code to only be executed on mobile, but not on desktop, you can put it inside the following condition:

if (document.documentElement.classList.contains("mobile")) {
  ...
}

The full list of classes for other cases is listed here.

Is that a general example of using JavaScript in the note type, or is there some black magic I’m not aware of that makes that code scroll the viewport to the cloze that was just revealed after flipping? :sweat_smile:

Ah nice! Good to know, thanks :slightly_smiling_face:

Anki automatically scrolls to the first HTML element with id=answer. The code simply adds this id=answer to the current cloze (which Anki gives the class="cloze").

It’s kind of hacky and I’m surprised it even worked tbh!

1 Like

Wow, that is such a hidden gem of Anki knowledge, seems also exactly made for this problem ahah. I had no idea, thanks!

1 Like

Well actually, it’s in the manual!

The cloze part isn’t there though (or at least it didn’t turn up in the search). Maybe someone should add it…

2 Likes

Wooah, hiding in plain sight, amazing :grinning_face_with_smiling_eyes: thank you so much!