Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 智能设置Div宽度_Javascript_Jquery_Css_Layout_Html - Fatal编程技术网

Javascript 智能设置Div宽度

Javascript 智能设置Div宽度,javascript,jquery,css,layout,html,Javascript,Jquery,Css,Layout,Html,一段时间以来,我一直在试图找到一种方法,以一种吸引人的方式,根据内容设置一个页面的宽度。我希望能够告诉读者,根据段落中的单词数量,每隔一段时间就要换行一次。例如,如果是一个300字的长段落,那么它的宽度是500px,分为多少行,但是如果是10字,它的宽度只有100px,分为两行 我并不迫切需要它,但我已经放弃了谷歌搜索,我很好奇这里是否有人有任何想法。也许你可以使用这个小脚本垂直移动鼠标。它最初是在很久以前用ActionScript2编写的 ​ 您可能可以使用“最小宽度”和“最大宽度”来执行某些

一段时间以来,我一直在试图找到一种方法,以一种吸引人的方式,根据内容设置一个页面的宽度。我希望能够告诉读者,根据段落中的单词数量,每隔一段时间就要换行一次。例如,如果是一个300字的长段落,那么它的宽度是500px,分为多少行,但是如果是10字,它的宽度只有100px,分为两行


我并不迫切需要它,但我已经放弃了谷歌搜索,我很好奇这里是否有人有任何想法。

也许你可以使用这个小脚本垂直移动鼠标。它最初是在很久以前用ActionScript2编写的


您可能可以使用“最小宽度”和“最大宽度”来执行某些操作……文本布局是否有预先确定的规则可以遵循?根据Andrew的建议,快速调整:。您到底想做什么?是否要使图形具有特定的形状?黄金比例?方形?如果没有解释,或者你尝试过什么,我怎么能做到这一点,那么很难给出有意义的答案。最简单的解决方案是,根据字数使用脚本设置宽度,并在内容更改时再次调用相同的函数。您要么必须控制所有可能更新内容的代码,要么必须轮询div以获取内容更改某些解释将非常棒。我不明白这和这个问题有什么关系。OP根据div中的字数询问div宽度。抱歉,我仍然不知道这段代码有多有用。你的代码根据鼠标位置改变高度,对我来说不是很有吸引力;框外有大量文本。
/*
outerNode : block element with a helper wrapper element inside
Fiddles with the width of outerNode until it reaches desired height (maxH)
Width is limited between minW and maxW
*/
var fitToHeight = function( outerNode, minW, maxW, maxH ){
    var L,H,A; // Lo,Hi,Actual width for binary search
    var $set = $(outerNode);       // new width will be applied to the outer node
    var $get = $($set).children(); // actual width / height readout

    // Set initial width to the longest non-wrapping word (
    $set.width( minW );
    L = H = Math.min( maxW, Math.max( minW, $get.width())); // longest word

    // grow the width until height falls below maxH
    while( $get.height() > maxH && H < maxW ){
        // remember the width of the last too high box: it will be the lower limit for bin.search
        L = H;
        H = Math.min( H*2, maxW ); // limited grow of upper limit
        $set.width( H );
    }
    H = Math.min( maxW, $get.width() );

    // binary search for the ideal width where height just falls below maxH
    do{
        A = Math.round( ( H + L )/2 );
        $set.width( A );
        if( $get.height() > maxH ) L=A; else H=A;
    }while( H - L > 1 );

    // width might be smaller by a subpixel, so increment it to guarantee maximum height
    if( $get.height() > maxH )
        $set.width( A+1 );

    // set block's height if necessary
    $set.height( maxH );
};


jQuery.fn.fitToHeight = function( minW, maxW, maxH ){
    this.each(function(){
        fitToHeight( this, minW, maxW, maxH );
    });
}