简体   繁体   English

Jquery添加尽可能多的跨度来填充div

[英]Jquery add as many spans as it takes to fill a div

I have an HTML div ` 我有一个HTML div`

<div class="words_padding">

</div>

That is 800px and i have got this jquery array 那是800px,我有这个jquery数组

var words=["thing","my","low","life","that","repeat","his","meaning","if","she","he","never","tell","part","tubes","how","should","come","off","on","it","about","me","and","do","same","put","country","math","like"];

I want to fill that div with as many spans as that div can take without getting out of borders. 我希望用尽可能多的跨度来填充该div,而div可以在不离开边界的情况下完成。
I have tried doing it with for like this 我试过这样做

    for (var i = 0; i <= 8; i++) {
        $('.words_padding').append("<span class=\"words_totp\">"+words[Math.floor((Math.random() * words.length))]+"</span>");
    }

But some words are longer than other ones so it goes out of borders Jsfiddle: https://jsfiddle.net/2yxvg5ye/2/ 但是有些单词比其他单词更长,所以它超出了边界Jsfiddle: https ://jsfiddle.net/2yxvg5ye/2/

I think this is simple enough.. The JS is as follows: 我觉得这很简单.. JS如下:

var words=["thing","my","low","life","that","repeat","his","meaning","if","she","he","never","tell","part","tubes","how","should","come","off","on","it","about","me","and","do","same","put","country","math","like"];
$('body').on('click', '.Start_tp', function(event) {
    event.preventDefault();
    var $newWord, $wrapper = $('.words_padding'), canLoop = true, totalWidthsUsed = 0;
    while (canLoop) {
        $newWord = $("<span class=\"words_totp\">"+words[Math.floor((Math.random() * words.length))]+"</span>");
        $wrapper.append($newWord);
        $newWord = $wrapper.find('.words_totp').last();
        totalWidthsUsed += $newWord.outerWidth();
        if(totalWidthsUsed > 800) {
           $newWord.remove();
           canLoop = false;
        }
    }
});

Updated JSFiddle: https://jsfiddle.net/2yxvg5ye/4/ 更新了JSFiddle: https ://jsfiddle.net/2yxvg5ye/4/

Here's what I propose: 这是我的建议:

<div class="words_padding" style="width: 800px;">

</div>

<div style="display: none;" id="hid"></div>

The div #hid is a tool for the script to come, hence its display: none; div #hid是脚本的工具,因此它的display: none; .

var words=["thing","my","low","life","that","repeat","his","meaning","if","she","he","never","tell","part","tubes","how","should","come","off","on","it","about","me","and","do","same","put","country","math","like"];


$(".words_padding").append("<span class=\"words_totp\">"+ w +"</span>");
var aux=1;
while (aux==1){
    var w = words[Math.floor((Math.random() * words.length))];
    $("#hid").append("<span class=\"words_totp\">"+ w +"</span>");       if($(".words_totp").last().position().left+$(".words_totp").last().outerWidth()+$("#hid").find(">:first-child").outerWidth()<=800){
        $(".words_padding").append($("#hid")[0]);
        $("hid").empty();
    }else{
        aux=0;
    }   
}

Works for me! 适合我! Not the cleanest way, but seems to do the job. 不是最干净的方式,但似乎做了这个工作。 Let me know if you have questions :) 如果您有疑问,请告诉我:)

I got close, but for the life of me can't figure out why this takes two clicks of the button to work. 我接近了,但是对于我的生活无法弄清楚为什么这需要两次点击按钮才能工作。 That being said, if you can solve that issue, then you should be good to go. 话虽这么说,如果你能解决这个问题,那么你应该好好去。

What this does is call a function to randomize the list of words, then appends all of them to the div, but they default to display:none; 这样做是调用一个函数来随机化单词列表,然后将它们全部附加到div,但它们默认display:none;

It then goes through all the span elements added and does the math to see if the width is less than the container. 然后,它会遍历所有添加的span元素,并进行数学运算以查看宽度是否小于容器。 If it is, then it will fadeIn() a span element, until the max width is reached. 如果是,那么它将fadeIn()一个span元素,直到达到最大宽度。

  var words = ["thing", "my", "low", "life", "that", "repeat", "his", "meaning", "if", "she", "he", "never", "tell", "part", "tubes", "how", "should", "come", "off", "on", "it", "about", "me", "and", "do", "same", "put", "country", "math", "like"]; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } $('body').on('click', '.Start_tp', function(event) { event.preventDefault(); words = shuffle(words); var maxWidth = $('.words_padding').width(); var width = 0; $(words).each(function(i) { $('.words_padding').append("<span class=\\"words_totp\\" style='display:none;'>" + words[i] + "</span>"); $('.words_padding span').each(function() { width = width + $(this).outerWidth(); if (width < maxWidth) { $(this).fadeIn(); } }); }); }); 
 .words_totp { font-size: 38px; color: gray; padding-left: 10px; padding-right: 10px; font-family: monospace; } .words_padding { word-break: break-word; margin-left: 12px; margin-top: 10px; background: white; width: 800px; position: relative; left: 25px; height: 70px; } .Start_tp { font-size: 30px; border: 0; background: #03a9f4; padding: 5px; color: white; position: relative; top: 100px; left: 100px; width: 200px; cursor: pointer; outline: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="words_padding"></div> <input type="button" class="Start_tp" value="Start"> 

Here is a fiddle as well: https://jsfiddle.net/8k44gvrm/1/ 这也是一个小提琴: https//jsfiddle.net/8k44gvrm/1/

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

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