How to control positioning of jQueryUI datepicker

前端 未结 13 1281
一整个雨季
一整个雨季 2020-12-07 16:55

The datepicker in jQueryUI renders with a dynamic position. It renders according to its css if there\'s enough room for it, but if there isn\'t enough window space it tries

相关标签:
13条回答
  • 2020-12-07 17:18
    $("first-selector").datepicker().bind('click',function () {
          $("#ui-datepicker-div").appendTo("other-selector");
    });
    
    <style>
    #ui-datepicker-div {
            position: absolute !important;
            top: auto !important;
            left: auto !important;
            z-index: 99999999 !important;
    }
    </style>
    
    0 讨论(0)
  • 2020-12-07 17:18

    By default the calendar of the date picker was display to the upper-right of my input box, so the top of it was hidden by my menu bar. Here's how I positioned the widget (jquery-ui version 1.11.4) very simply (note that 'ui-datepicker' is the class name given by jquery-ui):

    $(document).ready(function() {
    $(".ui-datepicker").css('margin-left', '-50px', 'margin-top', '-50px');
    //... 
    }
    
    0 讨论(0)
  • 2020-12-07 17:22

    bind focusin after using datepicker change css of datepicker`s widget wish help

    $('input.date').datepicker();
    $('input.date').focusin(function(){
        $('input.date').datepicker('widget').css({left:"-=127"});
    });
    
    0 讨论(0)
  • 2020-12-07 17:22

    The accepted answer works very well overall.

    However, using jQuery UI v1.11.4, I had an issue where the datepicker would position itself away from the input in a modal window (fixed positioning) when the browser window has been scrolled down. I was able to fix this problem editing the accepted answer as follows:

    const checkOffset = $.datepicker._checkOffset;
    
    $.extend($.datepicker, {
      _checkOffset: function(inst, offset, isFixed) {
        if(isFixed) {
          return checkOffset.apply(this, arguments);
        } else {
          return offset;
        }
      }
    });
    
    0 讨论(0)
  • 2020-12-07 17:22

    here is a more simple solution

    var $picker = $( ".myspanclass" ).datepicker(); // span has display: inline-block
    $picker.find('.ui-datepicker').css('margin-left', '-50px');
    
    0 讨论(0)
  • 2020-12-07 17:23

    Edit: JaredC gave an updated answer for later versions of jQuery above. Switching accepted answer to that.

    Alright, this is a matter of monkeypatching the feature it has of attempting to always render in the viewport since there's no option provided to enable/disable the feature. Luckily it's uncoupled and isolated in one function so we can just go in and override it. The following code completely disables that feature only:

    $.extend(window.DP_jQuery.datepicker,{_checkOffset:function(inst,offset,isFixed){return offset}});
    

    _checkOffset is called when it's opening and it does calculations on the offset and returns a new offset if it would otherwise be outside of the view port. This replaces the function body to just pass the original offset right through. Then you can use the beforeShow setting hack and/or the .ui-datepicker css class to put it wherever you want.

    0 讨论(0)
提交回复
热议问题