jQuery datepicker显示在页面底部(Keith-Wood datepicker)

时间:2014-08-21 20:22:34

标签: javascript jquery css html5

我遇到了jquery datepicker(Keith-Wood)(http://keith-wood.name/datepick.html)的问题。它在jquery 1.6.4上运行良好,但在我迁移到jQuery 1.10.2之后,小部件出现在页面底部的每个使用日期选择器的实例上。

功能正常,只是位置搞砸了。

widget opening at the bottom

我需要在DateAdded部分下面打开小部件。如果我切换回jQuery 1.6.4,定位是正确的。这是使用jQuery 1.6.4时的屏幕截图

enter image description here

以下是我用来触发日历的功能。

function open_calendar()
{
    jQuery('.open_calendar').datepick(
    {
       beforeShow: function(input, inst)
       {
          inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth + 'px'});
       },
       dateFormat: 'mm/dd/yy',
       rangeSelect: false,
       monthsToShow: [1,2],
       monthsOffset: 0,
       monthsToStep: 1,
       //supress the inline css used by jquery for div width
onShow:function(){
        jQuery('.datepick-multi').css({'width':'426'});
        jQuery('.datepick-popup').css({'z-index':'20000'});
        },
        constrainInput:false,
        showOnFocus:false,
        showTrigger:"<button type='button' title='Pick Date' style='padding:1px;' name='StartDatePicker' id='StartDatePicker'><img src='/images/calendarpicker.gif' alt='Pick Date' width='16' height='13'></button>"
        });
}

我需要让窗口小部件的位置叠加在触发它的文本框的正下方。

3 个答案:

答案 0 :(得分:3)

当您更新jquery时,您是否还更新了来自http://keith-wood.name/datepick.html的jquery.plugin.js,jquery.datepick.js和jquery.datepick.css?我认为旧版本的datepick插件可能与较新版本的jQuery不兼容,但如果你更新datepick插件,它应该可以工作。检查出来,它适用于5.0的datepick插件和jQuery版本1.10.2。

http://jsfiddle.net/t3kLr8d4/3/

<强> HTML

<link rel="stylesheet" href="http://keith-wood.name/css/jquery.datepick.css" id="theme">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://keith-wood.name/js/jquery.plugin.js"></script>
<script src="http://keith-wood.name/js/jquery.datepick.js"></script>

<input type="text" id="date" />

<强> CSS

/* Default styling for jQuery Datepicker v5.0.0. */
.datepick {
    background-color: #fff;
    color: #000;
    border: 1px solid #444;
    border-radius: 0.25em;
    -moz-border-radius: 0.25em;
    -webkit-border-radius: 0.25em;
    font-family: Arial,Helvetica,Sans-serif;
    font-size: 90%;
}
.datepick-rtl {
    direction: rtl;
}
.datepick-popup {
    z-index: 1000;
}
.datepick-disable {
    position: absolute;
    z-index: 100;
    background-color: white;
    opacity: 0.5;
    filter: alpha(opacity=50);
}
.datepick a {
    color: #fff;
    text-decoration: none;
}
.datepick a.datepick-disabled {
    color: #888;
    cursor: auto;
}
.datepick button {
    margin: 0.25em;
    padding: 0.125em 0em;
    background-color: #fcc;
    border: none;
    border-radius: 0.25em;
    -moz-border-radius: 0.25em;
    -webkit-border-radius: 0.25em;
    font-weight: bold;
}
.datepick-nav, .datepick-ctrl {
    float: left;
    width: 100%;
    background-color: #000;
    color: #fff;
    font-size: 90%;
    font-weight: bold;
}
.datepick-ctrl {
    background-color: #600;
}
.datepick-cmd {
    width: 30%;
}
.datepick-cmd:hover {
    background-color: #777;
}
.datepick-ctrl .datepick-cmd:hover {
    background-color: #f08080;
}
.datepick-cmd-prevJump, .datepick-cmd-nextJump {
    width: 8%;
}
a.datepick-cmd {
    height: 1.5em;
}
button.datepick-cmd {
    text-align: center;
}
.datepick-cmd-prev, .datepick-cmd-prevJump, .datepick-cmd-clear {
    float: left;
    padding-left: 2%;
}
.datepick-cmd-current, .datepick-cmd-today {
    float: left;
    width: 35%;
    text-align: center;
}
.datepick-cmd-next, .datepick-cmd-nextJump, .datepick-cmd-close {
    float: right;
    padding-right: 2%;
    text-align: right;
}
.datepick-rtl .datepick-cmd-prev, .datepick-rtl .datepick-cmd-prevJump,
.datepick-rtl .datepick-cmd-clear {
    float: right;
    padding-left: 0%;
    padding-right: 2%;
    text-align: right;
}
.datepick-rtl .datepick-cmd-current, .datepick-rtl .datepick-cmd-today {
    float: right;
}
.datepick-rtl .datepick-cmd-next, .datepick-rtl .datepick-cmd-nextJump,
.datepick-rtl .datepick-cmd-close {
    float: left;
    padding-left: 2%;
    padding-right: 0%;
    text-align: left;
}
.datepick-month-nav {
    float: left;
    background-color: #777;
    text-align: center;
}
.datepick-month-nav div {
    float: left;
    width: 12.5%;
    margin: 1%;
    padding: 1%;
}
.datepick-month-nav span {
    color: #888;
}
.datepick-month-row {
    clear: left;
}
.datepick-month {
    float: left;
    width: 15em;
    border: 1px solid #444;
    text-align: center;
}
.datepick-month-header, .datepick-month-header select, .datepick-month-header input {
    height: 1.5em;
    background-color: #444;
    color: #fff;
    font-weight: bold;
}
.datepick-month-header select, .datepick-month-header input {
    height: 1.4em;
    margin: 0em;
    padding: 0em;
    border: none;
    font-size: 100%;
}
.datepick-month-header input {
    position: absolute;
    display: none;
}
.datepick-month table {
    width: 100%;
    border-collapse: collapse;
}
.datepick-month thead {
    border-bottom: 1px solid #aaa;
}
.datepick-month th, .datepick-month td {
    margin: 0em;
    padding: 0em;
    font-weight: normal;
    text-align: center;
}
.datepick-month th {
    border: 1px solid #777;
}
.datepick-month th, .datepick-month th a {
    background-color: #777;
    color: #fff;
}
.datepick-month td {
    background-color: #eee;
    border: 1px solid #aaa;
}
.datepick-month td.datepick-week {
    border: 1px solid #777;
}
.datepick-month td.datepick-week * {
    background-color: #777;
    color: #fff;
    border: none;
}
.datepick-month a {
    display: block;
    width: 100%;
    padding: 0.125em 0em;
    background-color: #eee;
    color: #000;
    text-decoration: none;
}
.datepick-month span {
    display: block;
    width: 100%;
    padding: 0.125em 0em;
}
.datepick-month td span {
    color: #888;
}
.datepick-month td .datepick-other-month {
    background-color: #fff;
}
.datepick-month td .datepick-weekend {
    background-color: #ddd;
}
.datepick-month td .datepick-today {
    background-color: #f0c0c0;
}
.datepick-month td .datepick-highlight {
    background-color: #f08080;
}
.datepick-month td .datepick-selected {
    background-color: #777;
    color: #fff;
}
.datepick-month th.datepick-week {
    background-color: #777;
    color: #fff;
}
.datepick-status {
    clear: both;
    background-color: #ddd;
    text-align: center;
}
.datepick-clear-fix {
    clear: both;
}

<强>的Javascript

$('#date').datepick({
       beforeShow: function(input, inst)
       {
          inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth + 'px'});
       },
       dateFormat: 'mm/dd/yy',
       rangeSelect: false,
       monthsToShow: [1,2],
       monthsOffset: 0,
       monthsToStep: 1,
       //supress the inline css used by jquery for div width
onShow:function(){
        jQuery('.datepick-multi').css({'width':'426'});
        jQuery('.datepick-popup').css({'z-index':'20000'});
        },
        constrainInput:false,
        showOnFocus:false,
        showTrigger:"<button type='button' title='Pick Date' style='padding:1px;' name='StartDatePicker' id='StartDatePicker'><img src='/images/calendarpicker.gif' alt='Pick Date' width='16' height='13'></button>"
});

答案 1 :(得分:0)

以防人们遇到像我这样的类似问题,这是我使用的解决方案,

function open_calendar()
{
$('.open_calendar').datepick({
    beforeShow: function(input, inst)
    {
        inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth + 'px'});
    },
dateFormat: 'mm/dd/yy',
    rangeSelect: false,
    monthsToShow: [1,2],
    monthsOffset: 0,
    monthsToStep: 1,
    //supress the inline css used by jquery for div width
    onShow:function(){
        //this will retrieve the postion of the element that triggers your calendar
        var position=jQuery(this).position();
        jQuery('.datepick-multi').css({'width':'426'});
        //I added the 25px to the position to show the calendar just below the text box
        jQuery('.datepick-popup').css({'z-index':'20000','top':position.top+25});
    },
    constrainInput:false,
    showOnFocus:false,
    showTrigger:"<button type='button' title='Pick Date' style='padding:1px;' name='StartDatePicker' id='StartDatePicker'><img src='/images/calendarpicker.gif' alt='Pick Date' width='16' height='13'></button>"
    });
}

我添加25px的原因是因为我网站上的所有文本框都调整到了该高度,您可以根据自己的使用情况对其进行修改。

  

注意:jQuery不支持获取隐藏元素的位置坐标或者考虑在body元素上设置边框,边距或填充。有关详细信息,请查看此jQuery Position

答案 2 :(得分:0)

对于那些在更高版本的jquery中使用keith wood的日期选择器感到沮丧的人。 (我使用jquery 3.3.1)。 这就是我所做的。

CSS:

.calendars-popup {
    position: absolute;
    top: unset !important;
    left: unset !important;
    right: 15px;
}

除了IE(11,10,9)之外,css足以覆盖所有浏览器。 对于IE,请进行以下更改。

jquery.calendars.picker.js:

1)将andSelf替换为addBack

2)_showPlugin方法:评论此行

inst.div.css({left: offset.left, top: offset.top});

3)_update方法:注释这些行

left: target.offset().left,
top: target.offset().top + target.outerHeight()

这些行导致top&amp;的内联css。左,这使IE定位乱七八糟。

这可能不是最好的方法,但它为我解决了。

BTW :我也使用了他们的阿拉伯语选择器。