【问题标题】:JQuery Datepicker - vertically center dateJQuery Datepicker - 垂直居中日期
【发布时间】:2016-10-19 09:23:20
【问题描述】:

我目前正在为 jquery datepicker 设置样式,但我正在努力将日期位置垂直居中。

这是我目前得到的: https://jsfiddle.net/L4vrkpmc/1/

由于日期选择器使用的是我尝试过的表

.ui-datepicker-calendar td{
  vertical-align:center;
}

但它不会工作。有人知道如何垂直居中日期吗?

html,
body {
  height: 100%;
}


/* DatePicker Container */

#calendar {
  height: 100%;
}

.ui-datepicker {
  width: 100%;
  height: 100%;
  margin: 5px auto 0;
  font: 9pt Arial, sans-serif;
  -webkit-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .5);
  -moz-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .5);
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .5);
}

.ui-datepicker a {
  text-decoration: none;
}


/* DatePicker Table */

.ui-datepicker table {
  width: 100%;
  height: 100%;
  text-align: center;
}

.ui-datepicker-header {
  background: url('../img/dark_leather.png') repeat 0 0 #000;
  color: #e0e0e0;
  font-weight: bold;
  -webkit-box-shadow: inset 0px 1px 1px 0px rgba(250, 250, 250, 2);
  -moz-box-shadow: inset 0px 1px 1px 0px rgba(250, 250, 250, .2);
  box-shadow: inset 0px 1px 1px 0px rgba(250, 250, 250, .2);
  text-shadow: 1px -1px 0px #000;
  filter: dropshadow(color=#000, offx=1, offy=-1);
  line-height: 30px;
  border-width: 1px 0 0 0;
  border-style: solid;
  border-color: #111;
}

.ui-datepicker-title {
  text-align: center;
}

.ui-datepicker-prev,
.ui-datepicker-next {
  display: inline-block;
  width: 30px;
  height: 30px;
  text-align: center;
  cursor: pointer;
  line-height: 600%;
  overflow: hidden;
}

.ui-datepicker-prev {
  float: left;
  background-position: center -30px;
}

.ui-datepicker-next {
  float: right;
  background-position: center 0px;
}

.ui-datepicker thead {
  background-color: #f7f7f7;
  background-image: -moz-linear-gradient(top, #f7f7f7 0%, #f1f1f1 100%);
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f7f7f7), color-stop(100%, #f1f1f1));
  background-image: -webkit-linear-gradient(top, #f7f7f7 0%, #f1f1f1 100%);
  background-image: -o-linear-gradient(top, #f7f7f7 0%, #f1f1f1 100%);
  background-image: -ms-linear-gradient(top, #f7f7f7 0%, #f1f1f1 100%);
  background-image: linear-gradient(top, #f7f7f7 0%, #f1f1f1 100%);
  filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#f7f7f7', endColorstr='#f1f1f1', GradientType=0);
  border-bottom: 1px solid #bbb;
}

.ui-datepicker th {
  text-transform: uppercase;
  font-size: 6pt;
  padding: 5px 0;
  color: #666666;
  text-shadow: 1px 0px 0px #fff;
  filter: dropshadow(color=#fff, offx=1, offy=0);
}

.ui-datepicker tbody td {
  padding: 0;
  border-right: 1px solid #bbb;
}

.ui-datepicker tbody td:last-child {
  border-right: 0px;
}

.ui-datepicker tbody tr {
  border-bottom: 1px solid #bbb;
}

.ui-datepicker tbody tr:last-child {
  border-bottom: 0px;
}

.ui-datepicker td span,
.ui-datepicker td a {
  display: inline-block;
  font-weight: bold;
  text-align: center;
  width: 100%;
  height: 100%;
  color: #666666;
  text-shadow: 1px 1px 0px #fff;
  filter: dropshadow(color=#fff, offx=1, offy=1);
}

.ui-datepicker-calendar .ui-state-default {
  background: #ededed;
  background: -moz-linear-gradient(top, #ededed 0%, #dedede 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ededed), color-stop(100%, #dedede));
  background: -webkit-linear-gradient(top, #ededed 0%, #dedede 100%);
  background: -o-linear-gradient(top, #ededed 0%, #dedede 100%);
  background: -ms-linear-gradient(top, #ededed 0%, #dedede 100%);
  background: linear-gradient(top, #ededed 0%, #dedede 100%);
  filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#ededed', endColorstr='#dedede', GradientType=0);
  -webkit-box-shadow: inset 1px 1px 0px 0px rgba(250, 250, 250, .5);
  -moz-box-shadow: inset 1px 1px 0px 0px rgba(250, 250, 250, .5);
  box-shadow: inset 1px 1px 0px 0px rgba(250, 250, 250, .5);
}

.ui-datepicker-calendar .ui-state-hover {
  background: #f7f7f7;
}

.ui-datepicker-calendar .ui-state-active {
  background: #6eafbf;
  -webkit-box-shadow: inset 0px 0px 10px 0px rgba(0, 0, 0, .1);
  -moz-box-shadow: inset 0px 0px 10px 0px rgba(0, 0, 0, .1);
  box-shadow: inset 0px 0px 10px 0px rgba(0, 0, 0, .1);
  color: #e0e0e0;
  text-shadow: 0px 1px 0px #4d7a85;
  filter: dropshadow(color=#4d7a85, offx=0, offy=1);
  border: 1px solid #55838f;
  position: relative;
  margin: -1px;
}

.ui-datepicker-unselectable .ui-state-default {
  background: #f4f4f4;
  color: #b4b3b3;
}

.ui-datepicker-calendar td:first-child .ui-state-active {
  margin-left: 0;
}

.ui-datepicker-calendar td:last-child .ui-state-active {
  margin-right: 0;
}

.ui-datepicker-calendar tr:last-child .ui-state-active {
  margin-bottom: 0;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
  $(function() {
    $('#calendar').datepicker({
      inline: true,
      showOtherMonths: true,
      firstDay: 1,
      dayNamesMin: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
    });
  });

</script>

<div id="calendar"></div>

感谢您的帮助:)

【问题讨论】:

  • vertical-align:middle 是正确的语法。但我不确定这是否会奏效。在表格单元格的情况下,除非分配了其他属性,否则默认情况下内容应垂直居中。检查是否为 td 分配了其他属性。

标签: javascript jquery html css datepicker


【解决方案1】:

如果“a”标签中不包含任何子标签,则不能垂直对齐文本。我使用 jQuery 在“a”标签下用&lt;span&gt; 包裹了文本。这是代码:

$(".ui-datepicker td").each(function(){
    var dateText = $(this).find("a").text();
    $(this).find("a").html("<span>"+dateText+"</span>");
});

还有一些简单的 CSS 更改:

.ui-datepicker td a{
    display: table;
    position: relative;
    height: 100%;
    width: 100%;
}
.ui-datepicker td a span{
    display: table-cell;
    text-align:center;
    vertical-align: middle;
}

【讨论】:

  • 那行不通。它仅以其他月份的日子为中心,而不是实际月份的中心
  • Nvm,我忘了把你的 scrupt 移到一个函数中,现在可以完美运行了!
【解决方案2】:

您可以使用仅使用 CSS 来实现此目的,只需将数字的 line-height 设置为与正方形的相同高度:

.ui-state-default {
    height: 45px;
    line-height: 45px;
    padding: 0;
}

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 2011-04-20
    • 2011-02-03
    • 2012-04-26
    • 2011-08-10
    • 2013-10-15
    • 2012-10-06
    • 2015-04-01
    相关资源
    最近更新 更多