How to control positioning of jQueryUI datepicker

前端 未结 13 1363
一整个雨季
一整个雨季 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:27

    The problem I was having is that the datepicker is positioned incorrectly inside fixed elements, such as fancy/lightboxes. For some reason, the datepicker switches into "fixed" positioning mode when this happens, and then the offset calculation becomes incorrect, when absolute positioning would have worked just fine.

    However, we can get the correct fixed position with this hack:

    const checkOffset = $.datepicker._checkOffset;
    
    $.extend($.datepicker, {
        _checkOffset: function(inst, offset, isFixed) {
            if(!isFixed) {
                return checkOffset.apply(this, arguments);
            }
    
            let isRTL = this._get(inst, "isRTL");
            let obj = inst.input[0];
    
            while (obj && (obj.type === "hidden" || obj.nodeType !== 1 || $.expr.filters.hidden(obj))) {
                obj = obj[isRTL ? "previousSibling" : "nextSibling"];
            }
    
            let rect = obj.getBoundingClientRect();
    
            return {
                top: rect.top,
                left: rect.left,
            };
        }
    });
    

提交回复
热议问题