我有一个 div
,我通过以下函数每半秒生成一次内容:
function ClockAndCalendar() {
var da = new Date();
var hours = da.getHours();
var minutes = da.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var actualTime = hours + ':' + minutes + ampm;
var wd = weekday[da.getDay()];
var month = mon[da.getMonth()];
var curDate = da.getDate();
var suffix = ending[da.getDate() - 1];
var timeAndDate = (actualTime + " " + wd + ", " + month + " " + curDate + suffix + ", " + y);
var newDiv = $('#date-and-time').text(timeAndDate);
return newDiv;
}
var getTimeDateCalendar = setInterval(ClockAndCalendar, 500);
div
#date-and-time
嵌套在下面的 div
中,如下所示:
<div id="calDateTimeBox">
<i id="clock-link" class="fa fa-calendar" aria-hidden="true" data-title="Click to show calendar."></i>
<div id="date-and-time">10:06PM Wednesday, March 29th, 2017</div>
</div>
#calDateTimeBox
和 #clock-link
有 CSS:
#calDateTimeBox {
position: absolute;
top: 40px;
margin-left: -357px;
width: 340px;
height: auto;
font-family: 'Many Secrets';
font-size: 20px;
letter-spacing: 1px;
line-height: 1.2;
color: #FFF;
background: rgba(0, 0, 0, 0.75);
text-shadow: 4px 4px 1px #000;
border-radius: 5px;
text-align: center;
padding: 10px 20px;
pointer-events: none;
opacity: 0;
}
#clock-link {
font-family:'FontAwesome';
float: left;
margin-right: 10px;
margin-left: 5px;
}
当时间和日期在较长的一侧时,div 看起来像这样:
但我希望它看起来像这样:
是否有可能,使用 jQuery,让 div
在内容溢出到第二行时进行调整?如果是这样,我将如何实现这种效果?
最佳答案
改变
#calDateTimeBox {
width: 340px;
}
到:
#calDateTimeBox {
min-width: 340px;
}
问题似乎解决了。如果父容器的宽度不足以容纳它,这仍然会使日期转到另一行。
您还可以添加:
#date-and-time {
white-space: nowrap;
}
但我不建议这样做,因为您的文本会被截断/显示在 div 容器之外。
关于jquery - 使 div 宽度随 jQuery 而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106623/