原文:How to Create Calendar using jQuery and CSS3
在本教程中,你可以在Vladimir Kudinov的Futurico UI Pro包里找到我们将编写的jQuery和CSS3日历,对于样式我们需要使用CSS,而功能性则需要用到jQuery和 jQuery UI。jQuery UI中,我们将只使用“日期选择器”脚本,所以你不必下载 jQuery UI的所有可用组件以保证文件尺寸更低。
步骤一:HTML标记
为了创建日历,我们只需要给div标签添加一个ID:
<div id="calendar"></div>
然后在body的关闭标签之前添加jQuery和jQuery UI的脚本文件。
我们还需要调用“日期选择器”,所以你需要使用和你的div相同的id名。接下来还要设置一些参数:行内标签使日历可见,所以我们并不需要一个按钮或输入框用于点击;将日历的第一天也就是“星期一”设为1;我们添加其它月份日期显示来填充表格。关于所有参数的更多信息请参阅文档。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery-ui-datepicker.min.js"></script>
<script>
$('#calendar').datepicker({
inline: true,
firstDay: 1,
showOtherMonths: true,
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
});
</script>
步骤二:容器
首先让我们删除所有的外边距、内填充和边框等:
.ui-datepicker,
.ui-datepicker table,
.ui-datepicker tr,
.ui-datepicker td,
.ui-datepicker th {
margin: 0;
padding: 0;
border: none;
border-spacing: 0;
}
然后定义日历容器的样式,首先添加背景颜色、圆角和阴影,我们将字体设为“Tahoma”并设置字体大小。
.ui-datepicker {
display: none;
width: 294px;
padding: 35px;
cursor: default;
text-transform: uppercase;
font-family: Tahoma;
font-size: 12px;
background: #141517;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0px 1px 1px rgba(255,255,255, .1), inset 0px 1px 1px rgb(0,0,0);
-moz-box-shadow: 0px 1px 1px rgba(255,255,255, .1), inset 0px 1px 1px rgb(0,0,0);
box-shadow: 0px 1px 1px rgba(255,255,255, .1), inset 0px 1px 1px rgb(0,0,0);
}
步骤三:头部
为了定义日历头部(月和年)样式,我们需要改变字体颜色、添加一个底部边框以及更多的基本样式。
.ui-datepicker-header {
position: relative;
padding-bottom: 10px;
border-bottom: 1px solid #d6d6d6;
}
.ui-datepicker-title { text-align: center; }
.ui-datepicker-month {
position: relative;
padding-right: 15px;
color: #565656;
}
.ui-datepicker-year {
padding-left: 8px;
color: #a8a8a8;
}
我们将使用:before伪类选择器来添加绿色小圆点,这将允许我们在“月份”元素后插入内容,然后定义它的样式和位置。
.ui-datepicker-month:before {
display: block;
position: absolute;
top: 5px;
right: 0;
width: 5px;
height: 5px;
content: '';
background: #a5cd4e;
background: -moz-linear-gradient(top, #a5cd4e 0%, #6b8f1a 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#a5cd4e), color-stop(100%,#6b8f1a));
background: -webkit-linear-gradient(top, #a5cd4e 0%,#6b8f1a 100%);
background: -o-linear-gradient(top, #a5cd4e 0%,#6b8f1a 100%);
background: -ms-linear-gradient(top, #a5cd4e 0%,#6b8f1a 100%);
background: linear-gradient(top, #a5cd4e 0%,#6b8f1a 100%);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
步骤四:上个月和下个月
我们将使用一个背景图像定义前一个和下一个箭头的样式,然后我们定位一个在左边,一个在右边。
.ui-datepicker-prev,
.ui-datepicker-next {
position: absolute;
top: -2px;
padding: 5px;
cursor: pointer;
}
.ui-datepicker-prev {
left: 0;
padding-left: 0;
}
.ui-datepicker-next {
right: 0;
padding-right: 0;
}
.ui-datepicker-prev span,
.ui-datepicker-next span{
display: block;
width: 5px;
height: 10px;
text-indent: -9999px;
background-image: url(../img/arrows.png);
}
.ui-datepicker-prev span { background-position: 0px 0px; }
.ui-datepicker-next span { background-position: -5px 0px; }
.ui-datepicker-prev-hover span { background-position: 0px -10px; }
.ui-datepicker-next-hover span { background-position: -5px -10px; }
步骤五:日历样式
定义星期中每个日期的样式,我们将添加一个顶部和底部的内填充,并且改变它的颜色。
.ui-datepicker-calendar th {
padding-top: 15px;
padding-bottom: 10px;
text-align: center;
font-weight: normal;
color: #a8a8a8;
}
然后我们将通过添加一些内填充,改变颜色以及给每个数字添加一个透明边框来定义“日期网格”的样式。有必要给激活日期添加一个边框,当我们点击一个日期时我们需要给它添加一个透明边框,以避免跳转。
.ui-datepicker-calendar td {
padding: 0 7px;
text-align: center;
line-height: 26px;
}
.ui-datepicker-calendar .ui-state-default {
display: block;
width: 26px;
outline: none;
text-decoration: none;
color: #a8a8a8;
border: 1px solid transparent;
}
在激活状态下我们将改变文字和边框颜色为绿色,对于月份中的其它日期将改变为比较暗的颜色。
.ui-datepicker-calendar .ui-state-active {
color: #6a9113;
border: 1px solid #6a9113;
}
.ui-datepicker-other-month .ui-state-default { color: #565656; }
最后
日历到这里就完成了,如果你有任何问题请写在评论里告诉我,也不要忘记留下一些反馈意见并且分享给你的朋友,如果你想了解最新的教程和文章请关注我们。
原创译文 转载请注明出处
英文原文:How to Create Calendar using jQuery and CSS3