Created
April 12, 2014 07:10
-
-
Save minwe/10522594 to your computer and use it in GitHub Desktop.
懒加载disqus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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