Skip to content

Instantly share code, notes, and snippets.

@smokbel
Last active February 23, 2018 01:57
Show Gist options
  • Save smokbel/939a922497d896dc97b3cbbba4235f51 to your computer and use it in GitHub Desktop.
Save smokbel/939a922497d896dc97b3cbbba4235f51 to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="heading"> This is the intro</div>
<div id="graphic">
<div id="sections">
<section class="text">
<h3>Title 1</h3>
<p>Description goes here</p>
</section>
<section class="text">
<h3>Title 1</h3>
<p>Description goes hereeee and here and ahere and ahere erere</p>
</section>
<section class="text">
<h3>Title 1</h3>
<p>Description goes hereeee and here and ahere and ahere erere</p>
</section>
<section class="text">
<h3>Title 1</h3>
<p>Description goes hereeee and here and ahere and ahere erere</p>
</section>
<section class="text">
<h3>Title 1</h3>
<p>Description goes hereeee and here and ahere and ahere erere</p>
</section>
<section class="text">
<h3>Title 1</h3>
<p>Description goes hereeee and here and ahere and ahere erere</p>
</section>
</div>
<div id="viz">
</div>
</div>
</body>
var WIDTH = window.innerWidth / 2
var HEIGHT = window.innerHeight
var translate = 'translate(' + (WIDTH / 2) + ',' + (HEIGHT / 2) + ')'
var svg = d3.select("#sticky").append("svg")
.attr('width', WIDTH)
.attr('height', HEIGHT)
var currentScrollTop = d3.select('#currentScrollTop')
var hourLayer = svg.append('g')
.attr('transform', translate)
var hourRect = hourLayer.append('rect')
.attr('x', -3)
.attr('y', -87)
.attr('width', 6)
.attr('height', 90)
.attr('fill', '#333')
var minuteLayer = svg.append('g')
.attr('transform', translate)
var minuteRect = minuteLayer.append('rect')
.attr('x', -2)
.attr('y', -118)
.attr('width', 4)
.attr('height', 120)
.attr('fill', '#333')
var body = d3.select('body').node()
var container = d3.select('#container')
var content = d3.select('#content')
var SCROLL_LENGTH = content.node().getBoundingClientRect().height - HEIGHT
var hourHandRotation = d3.scale.linear()
.domain([0, SCROLL_LENGTH])
.range([0, 360])
.clamp(true)
var minuteHandRotation = d3.scale.linear()
.domain([0, SCROLL_LENGTH])
.range([0, 360 * 12])
.clamp(true)
var scrollTop = 0
var newScrollTop = 0
container
.on("scroll.scroller", function() {
newScrollTop = container.node().scrollTop
});
var setDimensions = function() {
WIDTH = window.innerWidth / 2
HEIGHT = window.innerHeight
SCROLL_LENGTH = content.node().getBoundingClientRect().height - HEIGHT
hourHandRotation.domain([0, SCROLL_LENGTH])
minuteHandRotation.domain([0, SCROLL_LENTH])
}
var render = function() {
if (scrollTop !== newScrollTop) {
scrollTop = newScrollTop
var hourHandRotate = hourHandRotation(scrollTop)
hourLayer.attr('transform', translate + ' rotate(' + hourHandRotate + ')')
var minuteHandRotate = minuteHandRotation(scrollTop)
minuteLayer.attr('transform', translate + ' rotate(' + minuteHandRotate + ')')
currentScrollTop.text(scrollTop)
}
window.requestAnimationFrame(render)
}
window.requestAnimationFrame(render)
window.onresize = setDimensions
#heading {
height: 50vh;
}
#sections {
position: relative;
display: inline-block;
width: 300px;
z-index: 50;
margin-left: 20vw;
}
.text {
padding-bottom:25vh;
width: 100%;
}
#viz {
display: inline-block;
position: absolute;
z-index: 1;
height: 60vh;
width: 35%;
margin-left: 5vw;
background-color: DarkGrey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment