admin管理员组文章数量:1328014
I have a script on my page that rearranges a bunch of boxes into a pinterest like mosaic, using the excellent jQuery Masonry plugin. I call the box layout rendering method like this from the bottom of the page (just before ):
<script type="text/javascript">
$(function() {
wall.drawBoxes();
});
</script>
I also use google web fonts like this, just after the tag:
<script type="text/javascript">
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
</script>
The problem is the boxes are rendered before the font has been loaded. And when the font has loaded, the boxes increase in size, making the rendered mosaic layout look like crap.
What can I do to prevent that?
I have a script on my page that rearranges a bunch of boxes into a pinterest like mosaic, using the excellent jQuery Masonry plugin. I call the box layout rendering method like this from the bottom of the page (just before ):
<script type="text/javascript">
$(function() {
wall.drawBoxes();
});
</script>
I also use google web fonts like this, just after the tag:
<script type="text/javascript">
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
</script>
The problem is the boxes are rendered before the font has been loaded. And when the font has loaded, the boxes increase in size, making the rendered mosaic layout look like crap.
What can I do to prevent that?
Share Improve this question asked Jun 13, 2012 at 16:26 Peter EvjanPeter Evjan 2,4333 gold badges32 silver badges50 bronze badges 1- You may try using $(document).load(function() { // data }); – Kane Cohen Commented Jun 13, 2012 at 16:31
5 Answers
Reset to default 2You could add loading
callback - it fires when all fonts have finished loading.
$(document).ready(function() {
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] },
loading: function() {wall.drawBoxes()}
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
});
I added the fontactive
callback which fires then a font has finished loading.
$(document).ready(function() {
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] },
fontactive: function(fontFamily, fontDescription) { wall.drawBoxes() }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
});
You can either move the web font call before the drawboxes call to load the fonts first.
I'd suggest moving your functions into a domready function. e.g.
You could also put the font call in the <head>
to load the fonts before the DOM is loaded.
Could you just load the google stylesheet? <link href="http://fonts.googleapis./css?family=Lobster:regular" rel="stylesheet" type="text/css" >
$(document).ready(function() {
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
$(function() {
wall.drawBoxes();
});
});
You could add the active
callback - it fires when all fonts have rendered.
$(document).ready(function() {
WebFontConfig = {
google: { families: [ 'Montserrat::latin' ] },
active: function() {wall.drawBoxes()}
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
});
For a reference on the events available, see the webfontloader readme.
It is simpler using css:
@import url(http://fonts.googleapis./css?family=....);
本文标签: javascriptGoogle Web Fonts loading after layout has renderedStack Overflow
版权声明:本文标题:javascript - Google Web Fonts loading after layout has rendered - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244707a2439055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论