Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created June 14, 2018 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/9cc1e92aa5f708e995d38ac7cd683d75 to your computer and use it in GitHub Desktop.
Save nolanlawson/9cc1e92aa5f708e995d38ac7cd683d75 to your computer and use it in GitHub Desktop.
Demo scroll anchoring
<!doctype html>
<html lang="en">
<head>
<title>
Demo scroll anchoring
</title>
<style>
body {
max-width: 600px;
margin: 0 auto;
}
.flex {
display: flex;
width: 100%;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.scrollable1, .scrollable2 {
flex: 1;
overflow-y: scroll;
display: flex;
}
table {
flex: 1;
}
</style>
</head>
<body>
<div class="flex">
<div class="scrollable1"></div>
<div class="scrollable2"></div>
</div>
<script>
(function () {
'use strict'
var ROWS = 1000
var COLUMNS = 50
var $ = document.querySelector.bind(document)
var scrollable1 = $('.scrollable1')
var scrollable2 = $('.scrollable2')
var els = [scrollable1, scrollable2]
els.forEach(el => {
var table = document.createElement('table')
for (var i = 0; i < ROWS; i++) {
var tr = document.createElement('tr')
for (var j = 0; j < COLUMNS; j++) {
var td = document.createElement('td')
var str = btoa(Math.random())
td.innerHTML = str.charAt(Math.floor(Math.random() * str.length))
tr.appendChild(td)
}
table.appendChild(tr)
}
el.appendChild(table)
})
scrollable2.addEventListener('scroll', e => {
scrollable1.scrollTop = e.target.scrollTop
})
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment