【发布时间】:2018-09-24 20:45:13
【问题描述】:
我想设置一个带有段落的框。每次单击按钮后,都应该替换本段的文本。对我来说重要的是这个文本应该作为一行留在框中,而不是中断。为此,我决定使用 FitText.js 插件,因为单词有不同的长度(给定的单词只是一个例子)。通过将其设置为表格和表格单元格,我实现了很好的对齐(垂直和水平)。你知道为什么给定的代码不能正常工作吗?
var words = ["Emily", "William Callum Smith Jr.", "Edwaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaard", "Bradley"];
var button1 = document.getElementById("give_me_a_name_please");
button1.addEventListener("click", function1);
function getRandomItem(array){
return array.splice(Math.floor(Math.random() * array.length), 1)[0];
}
function function1(){
var randomWord = getRandomItem(words);
document.getElementById('word').innerHTML = randomWord;
}
$('#give_me_a_name_please').click(function() {
function resize () { $("#word").fitText(0.6); }
});
#parent_of_word {
width: 20%;
height: 20%;
top: 10%;
left: 10%;
display: flex;
position: absolute;
text-align: center;
justify-content: center;
line-height: 100%;
border: 5px solid black;
word-break: keep-all;
white-space: nowrap;
}
#word {
color: brown;
display:flex;
align-items: center;
justify-content: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/FitText.js/0b4183af/jquery.fittext.js"></script>
<div id="parent_of_word">
<p id="word">----------------</p>
</div>
<button id="give_me_a_name_please">Button</button>
【问题讨论】:
-
你忘了问一个编程问题,@wymy...不清楚你在问什么。
-
我会编辑它。
-
@wyzy,您刚刚更改了您的问题以包含我回答的重要部分。你不应该那样做。您只能进行修改,以便清楚您的问题是什么,并保持清晰,以便对其他未来的访问者有所帮助。
-
@wymyszony 您希望框扩大以适合文本还是文本应调整并适合框?
-
文本应该调整并适合框。
标签: javascript jquery html css jquery-plugins