Skip to content

Instantly share code, notes, and snippets.

@minwe
Created April 12, 2014 07:10
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 minwe/10522594 to your computer and use it in GitHub Desktop.
Save minwe/10522594 to your computer and use it in GitHub Desktop.
懒加载disqus
(function () {
var disqus_shortname = 'evanyoublog',
el = document.getElementById('disqus_thread')
window.addEventListener('scroll', checkPosition)
window.addEventListener('load', checkPosition)
function checkPosition () {
var rect = el.getBoundingClientRect()
if (rect.top < window.innerHeight) {
window.removeEventListener('scroll', checkPosition)
window.removeEventListener('load', checkPosition)
loadDisqus()
}
}
function loadDisqus () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment