Skip to content

Commit b347589

Browse files
snipebinMichelle Sintov
authored andcommitted
WEB-2384: IOS Safari Scrolling anki#54
This PR adds workarounds for: 1. An IOS Safari bug which breaks scrolling on iFrame due to miscalculation of the content height. 2. Scrolling to anchor links on browsers which lack scrolling support for auto scrolling to elements by setting window.location.hash. The workaround adds use of element.scrollIntoView.
1 parent d8ddf3d commit b347589

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

docs/source/_templates/layout.html

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
return;
1212
}
1313

14+
// For styling necessary only when embedded in an iframe.
15+
function addEmbededClass() {
16+
document.body.classList.add("embedded");
17+
}
18+
1419
// Change document base to that of Developer.anki.com so that navigation happens at the top level.
1520
function updateDocumentBase() {
1621
var base = document.createElement('base');
@@ -21,28 +26,43 @@
2126
head.insertBefore(base, firstChild);
2227
}
2328

24-
if (document.readyState === 'loading') {
29+
// Signal to Developer.anki.com that we're done loading and ready to receive messages.
30+
// We send page title to Developer.anki.com so that the browser displays it.
31+
function notifyParentWindowOnLoad() {
32+
parent.postMessage({
33+
event: 'load',
34+
docTitle: document.title
35+
}, '*');
36+
}
37+
38+
function initEmbeddedMode() {
39+
addEmbededClass();
40+
updateDocumentBase();
41+
notifyParentWindowOnLoad();
42+
}
43+
44+
if (document.readyState !== 'complete') {
2545
window.addEventListener('load', function(){
26-
updateDocumentBase();
46+
initEmbeddedMode();
2747
});
2848
} else {
29-
updateDocumentBase();
49+
initEmbeddedMode();
3050
}
3151

3252
// Handle messages from Developer.anki.com.
3353
function receiveMessage(event) {
3454
var eventName = event.data.event;
3555
if(eventName === 'hashchange') {
3656
window.location.hash = event.data.hash;
57+
// Workaround for IOS Safari lack of support for window.location.hash scrolling.
58+
var linkedElement = document.getElementById(event.data.hash.replace('#', ''))
59+
if (linkedElement && linkedElement.scrollIntoView) {
60+
linkedElement.scrollIntoView();
61+
}
3762
}
3863
}
3964
window.addEventListener("message", receiveMessage, false);
4065

41-
// Send page title to Developer.anki.com so that the browser displays it.
42-
parent.postMessage({
43-
event: 'load',
44-
docTitle: document.title
45-
}, '*');
4666
})(window);
4767

4868
/**
@@ -56,6 +76,19 @@
5676
// }
5777

5878
</script>
79+
<style>
80+
/* Allows scrolling to anchor links on IOS Safari */
81+
.embedded .wy-grid-for-nav {
82+
position: fixed;
83+
top: 0;
84+
right: 0;
85+
bottom: 0;
86+
left: 0;
87+
overflow-y: scroll;
88+
-webkit-overflow-scrolling: touch;
89+
z-index: 1;
90+
}
91+
</style>
5992
<!-- <script async src='https://www.google-analytics.com/analytics.js'></script> -->
6093
{{ super() }}
6194
{% endblock %}

0 commit comments

Comments
 (0)