简体   繁体   English

$(window).resize()和$(document).ready()计算不同的值

[英]$(window).resize() and $(document).ready() calculate different values

I am trying to make text adaptive using jQuery. 我试图使用jQuery使文本自适应。 Here is the fiddle: 这是小提琴:

http://jsfiddle.net/bq2ca7ch/ http://jsfiddle.net/bq2ca7ch/

You can see a div with some text in it. 你可以看到一个包含一些文本的div。 The div doesn't have a specified height, and it's height is calculated from text height and 10% paddings on top and bottom. div没有指定的高度,它的高度是根据文本高度和顶部和底部的10%填充计算的。

I want font-size to be responsive. 我希望font-size能够响应。 Let's say, div's original size was 124px, and font-size was 50px, so I want to keep this ratio. 比方说,div的原始大小是124px,字体大小是50px,所以我想保持这个比例。 That means I need to know, what percent 50 is from 124. It is about 40.32 (50/124*100). 这意味着我需要知道,50%的百分比是124.它大约是40.32(50/124 * 100)。 That means that I need to set font-size to value, equal to container height/100 * 40.32. 这意味着我需要将font-size设置为value,等于容器高度/ 100 * 40.32。 Here is the code I used: 这是我使用的代码:

function foo(){
var container = $(".box");
var containerHeight = $(".box").innerHeight().toFixed();
var neededSize = (containerHeight/100*40.32).toFixed();

container.css("font-size", neededSize + "px");
}

$(window).resize(foo);
$(document).ready(foo);

That seems to be working, but only when I resize the page. 这似乎有效,但只有当我调整页面大小时。 When I reload it, there is some different value. 当我重新加载它时,有一些不同的价值。 Why does the same function gives different values on resize and onload? 为什么相同的函数在resize和onload上给出不同的值?

What i observed that Size changes because : 我观察到尺寸变化的原因是:

1. When you just reload .the function runs only once. 1.当你刚刚重新加载时,该功能只运行一次。

2.But when you resize , the function runs twice and changes the font size because the again calculations are done based on new height. 2.但是,当您调整大小时,该函数会运行两次并更改字体大小,因为再次计算是基于新高度完成的。 Main thing is on resize it is calculating wrong innerheight 主要是调整大小是计算错误的innerheight

See this: 看到这个:

function foo(jQuery ){
    var container = $(".box");
    var containerHeight = $(".box").innerHeight(true).toFixed(2);
    var neededSize = (containerHeight/100*40.32).toFixed(2);
alert(containerHeight );
    container.css("font-size", neededSize + "px");
}

$(window).resize(foo);
$(document).ready(foo);

Resize method is not reliable. 调整大小的方法不可靠。 Code in a resize handler should never rely on the number of times the handler is called. 调整大小处理程序中的代码绝不应该依赖于调用处理程序的次数。 Depending on implementation, resize events can be sent continuously as the resizing is in progress (the typical behavior in Internet Explorer and WebKit-based browsers such as Safari and Chrome), or only once at the end of the resize operation (the typical behavior in some other browsers such as Opera). 根据实施情况,调整大小事件可以在调整大小正在进行时连续发送(在Internet Explorer和基于WebKit的浏览器(如Safari和Chrome)中的典型行为),或者仅在调整大小操作结束时发送一次(典型行为在其他一些浏览器,如Opera)。

I tried with this , it worked for me both are giving the same result for me . 我试过这个,它对我有用,两个都给了我同样的结果。 try this container.css("font-size":neededSize + "px"); 试试这个container.css("font-size":neededSize + "px");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM