Skip to content

Commit 0f48382

Browse files
committed
Cleaner and working scrollIntoView
1 parent f1fffe5 commit 0f48382

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

index.html

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,15 @@
1616
</style>
1717
<script src="/socket.io/socket.io.js"></script>
1818
<script>
19-
20-
var SCROLL_PADDING = 100;
21-
var cumulativeOffset = function(element) {
22-
var top = 0, left = 0;
23-
do {
24-
top += element.offsetTop || 0;
25-
left += element.offsetLeft || 0;
26-
element = element.offsetParent;
27-
} while(element);
28-
return { top: top, left: left };
29-
};
30-
3119
var socket = io.connect('http://'+ window.location.host +'/');
3220
socket.on('connect', function () {
3321
socket.on('newContent', function(newHTML) {
3422
document.querySelector(".markdown-body").innerHTML = newHTML;
35-
var marker = document.querySelector("#marker");
23+
24+
// scroll to the hyperlink with id="marker"
25+
let marker = document.getElementById("marker");
3626
if(marker) {
37-
window.scrollTo(0, cumulativeOffset(marker).top - SCROLL_PADDING);
27+
marker.scrollIntoView();
3828
}
3929
});
4030
socket.on('die', function(newHTML) {

0 commit comments

Comments
 (0)