admin管理员组文章数量:1291097
In order to create an ellipsis after the second line within an element, I'm using jquery dotdotdot. I'm using it with the google font 'source sans pro', however it doesn't work properly until after the window is resized.
this is how the text appears before the window is resized (the ellipsis is created too early)
this is how the text appears after the window is resized (the ellipsis is in the proper position)
(I'm assuming this is happening because the font isn't fully loaded on the page's load, but I could be wrong?)
This is the way that I call the jquery dotdotdot.
$(document).ready(function(){
doResize();
$(window).on('resize', doResize);
});
function doResize() {
$(".col-mid a").dotdotdot({
ellipsis: '...',
height : 40
});
}
How would I make it so it works properly? I've tried delaying the time that the dotdotdot function is called but this seems hacky, and like a bad solution.
Here's a jsfiddle of relevant code. (Weirdly enough it seems to work fine on jsfiddle, however it doesn't work on my puter.)
In order to create an ellipsis after the second line within an element, I'm using jquery dotdotdot. I'm using it with the google font 'source sans pro', however it doesn't work properly until after the window is resized.
this is how the text appears before the window is resized (the ellipsis is created too early)
this is how the text appears after the window is resized (the ellipsis is in the proper position)
(I'm assuming this is happening because the font isn't fully loaded on the page's load, but I could be wrong?)
This is the way that I call the jquery dotdotdot.
$(document).ready(function(){
doResize();
$(window).on('resize', doResize);
});
function doResize() {
$(".col-mid a").dotdotdot({
ellipsis: '...',
height : 40
});
}
How would I make it so it works properly? I've tried delaying the time that the dotdotdot function is called but this seems hacky, and like a bad solution.
Here's a jsfiddle of relevant code. (Weirdly enough it seems to work fine on jsfiddle, however it doesn't work on my puter.)
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 6, 2014 at 6:47 user3104155user3104155 2373 silver badges7 bronze badges 4- whether the target elements are present in the dom when the page is loaded initially – Arun P Johny Commented Jan 6, 2014 at 6:48
- Yes everything's in the dom when the page loads initially, just like in this example. – user3104155 Commented Jan 6, 2014 at 6:49
-
as a debugging step can try
setTimeout(doResize, 1000)
instead ofdoResize()
– Arun P Johny Commented Jan 6, 2014 at 6:52 -
Add the option
watch: true
– Gavin Commented Dec 12, 2019 at 10:56
5 Answers
Reset to default 3If it works after resize you can trigger resize on page load and solve your problem:
$(window).trigger('resize');
OR
Try embedding your code within:
$(window).load(function(){ });
Because thats how the code is displayed in the source of jsfiddle and you mentioned that its working in jsfiddle.Therefore give it a try.
Change your script code to:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function(){
doResize();
$(window).on('resize', doResize);
});
function doResize() {
$("a").dotdotdot({
ellipsis: '...',
height : 60
});
}
});//]]>
</script>
Try this:
$(document).ready(function(){
//doResize();
$(window).on('load resize', doResize);
});
in case anyone ends up here two years after this question was posted (...like I just did because I had the same problem):
You don't need to trigger a resize() function. You can solve this problem with pure CSS:
Just choose a different (websafe) fallback font for your webfont. The fallback font should render similar/ as wide as your webfont so that the text-elements take the same space before and after your webfont is fully loaded.
For 'Source Sans Pro', '"Times New Roman", Times, serif' is probably the best option. See http://www.w3schools./cssref/css_websafe_fonts.asp
The best and only solution I found is this:
jQuery(window).load(function(){
jQuery("a").trigger("update.dot");
});
This solution is taken by the code proposed for the next versione (1.7.5) of dotdotdot. You can see it here: Added functionality to use CSS classes without need of JS programming and added some more features
You can add option through dotodotdot by watch: 'window'
$(".content-class").dotdotdot({
ellipsis : '... ',
watch : 'window',
});
本文标签: javascriptJquery dotdotdot only fired properly on window resizeStack Overflow
版权声明:本文标题:javascript - Jquery dotdotdot only fired properly on window resize - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504086a2382215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论