jquery - 使 div 宽度随 jQuery 而变化

标签 jquery html css width

我有一个 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 看起来像这样:

enter image description here

但我希望它看起来像这样:

enter image description here

是否有可能,使用 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/

相关文章:

javascript - 链接显示在“查看源代码”中,但不可点击

javascript - 如何使用 Jquery/Javascript AutoComplete 解码 HTML 实体

css - 分区宽度 = 100%?

css - 不能在 div 中间居中元素

html - 两个按钮之间的垂直线

html - 上下颠倒的 CSS 方向?

HTML 边距/填充 Bootstrap 3

jQuery Ajax : How to check every X seconds for a file (with access)?

javascript - 根据数量用矩形填充页面

javascript - 单击 jquery-ui 选项卡时我需要运行一个函数,我做错了什么?