Skip to content

Commit a322740

Browse files
Add lazy loading
1 parent f9b1678 commit a322740

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

js/lazysizes.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/ls.unveilhooks.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
"doc:images": "watch 'node src/modules/images.js' src/templates/docs/images",
2525
"doc:bgsize": "watch 'node src/modules/background-size.js' src/templates/docs/background-size",
2626
"build:all": "node build.js && bash script.sh",
27-
"cson": "node src/components-cson-build.js"
27+
"cson": "node src/components-cson-build.js",
28+
"copy:lazysizes": "cp node_modules/lazysizes/lazysizes.min.js js/",
29+
"copy:lazysizes:plugins": "cp node_modules/lazysizes/plugins/unveilhooks/ls.unveilhooks.min.js js/",
30+
"postinstall": "npm run copy:lazysizes && npm run copy:lazysizes:plugins"
2831
},
2932
"repository": "tachyons-css/tachyons-css.github.io",
3033
"author": "mrmrs",
@@ -40,6 +43,7 @@
4043
"imagemin": "^5.2.2",
4144
"imagemin-mozjpeg": "^6.0.0",
4245
"jimp": "^0.2.27",
46+
"lazysizes": "^3.0.0-rc2",
4347
"nightmare": "^2.8.1",
4448
"pretty-hrtime": "^1.0.3",
4549
"tachyons-background-size": "^5.0.3",

src/components-build-defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ module.exports = {
2525
headerTemplatePath: 'src/templates/header.html',
2626
headTemplatePath: 'src/templates/head.html',
2727
highlightTemplatePath: 'src/templates/highlight.html',
28+
lazysizesTemplate: 'src/templates/lazysizes.html',
2829
};

src/components-build-index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ module.exports = _options => new Promise((resolve, reject) => {
7878
const footer = fs.readFileSync(options.footerTemplatePath, 'utf8');
7979
const head = fs.readFileSync(options.headTemplatePath, 'utf8');
8080
const header = fs.readFileSync(options.headerTemplatePath, 'utf8');
81-
const indexTemplate = fs.readFileSync(options.componentsIndexTemplatePath, 'utf8');
81+
const componentsIndexTemplate = fs.readFileSync(options.componentsIndexTemplatePath, 'utf8');
82+
const lazysizesTemplate = fs.readFileSync(options.lazysizesTemplate, 'utf8');
8283

83-
const compiledPage = _.template(indexTemplate)({
84+
const compiledPage = _.template(componentsIndexTemplate)({
8485
componentsForNav,
8586
title: 'Components',
8687
analytics,
8788
footer,
8889
head,
8990
header,
91+
lazysizesTemplate,
9092
options,
9193
});
9294
mkdirp.sync(path.dirname(options.componentsIndexPath));

src/templates/components-index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<style type="text/css" media="screen">
88
.transparent { color: transparent; }
99
</style>
10+
<%= lazysizesTemplate %>
11+
<script src="/js/ls.unveilhooks.min.js" async=""></script>
12+
<script src="/js/lazysizes.min.js" async=""></script>
1013
</head>
1114
<body class="w-100 sans-serif">
1215
<%= header %>
@@ -32,8 +35,9 @@ <h1 class="f6 fw6 pb2 bb b--black-10 black-70 ttc"><%= category %></h1>
3235
href="<%= componentForNav.href %>"
3336
title="<%= componentForNav.name %>">
3437
<div class="ba b--dashed b--black-20 aspect-ratio aspect-ratio--<%= options.screenshotAspectRatio %>">
35-
<div class="aspect-ratio--object"
36-
style="background:url(<%= componentForNav.screenshot.href %>) no-repeat <%= componentForNav.frontMatter.screenshot && componentForNav.frontMatter.screenshot['background-position'] || 'center center' %>/<%= componentForNav.frontMatter.screenshot && componentForNav.frontMatter.screenshot['background-size'] || 'cover' %>;"></div>
38+
<div class="aspect-ratio--object lazyload"
39+
data-bg="<%= componentForNav.screenshot.href %>"
40+
style="background-repeat: no-repeat; background-position: <%= componentForNav.frontMatter.screenshot && componentForNav.frontMatter.screenshot['background-position'] || 'center center' %>; background-size: <%= componentForNav.frontMatter.screenshot && componentForNav.frontMatter.screenshot['background-size'] || 'cover' %>;"></div>
3741
</div>
3842
<p class="f5 tc">
3943
<%= componentForNav.name %>

src/templates/lazysizes.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<style type="text/css" media="screen">
2+
/* https://github.com/aFarkas/lazysizes#css-api */
3+
.lazyload {
4+
opacity: 0;
5+
}
6+
7+
.lazyloading {
8+
opacity: 1;
9+
transition: opacity 300ms;
10+
background-color: #eee;
11+
}
12+
</style>

0 commit comments

Comments
 (0)