天天看點

bootstrap-datetimepicker控件位置異常

今天在寫畢設的時候,用到了bootstrap-datetimepicker作為日期控件。

在git上clone下最新的代碼,運作demo,發現控件區域整體下移1000px左右。

作為一個準備拿來就用的背景程式猿,此刻我的内心是崩潰的…

百度了很久,沒有找到對應的解決方案,于是自己動手去源碼修改。

最終解決方案:

打開源碼,的bootstrap-datetimepicker.js檔案

line 527行,打開這一段注釋即可

/*if (this.pickerPosition == 'top-left' || this.pickerPosition == 'top-right') {
        top = offset.top - this.picker.outerHeight();
      } else {
        top = offset.top + this.height;
      }*/
           

如果看着還是不是很舒服的話,建議注釋掉line 533 - line 544

top = top - containerOffset.top + ;
      left = left - containerOffset.left + ;
           
為什麼要這樣解決呢?
place: function () {
      if (this.isInline) return;

      if (!this.zIndex) {
        var index_highest = ;
        $('div').each(function () {
          var index_current = parseInt($(this).css('zIndex'), );
          if (index_current > index_highest) {
            index_highest = index_current;
          }
        });
        this.zIndex = index_highest + ;
      }

      var offset, top, left, containerOffset;
      if (this.container instanceof $) {
        containerOffset = this.container.offset();
      } else {
        containerOffset = $(this.container).offset();
      }

      if (this.component) {
        offset = this.component.offset();
        left = offset.left;
        if (this.pickerPosition == 'bottom-left' || this.pickerPosition == 'top-left') {
          left += this.component.outerWidth() - this.picker.outerWidth();
        }
      } else {
        offset = this.element.offset();
        left = offset.left;
      }

      var bodyWidth = document.body.clientWidth || window.innerWidth;
      if (left +  > bodyWidth) {
        left = bodyWidth - ;
      }

      /*if (this.pickerPosition == 'top-left' || this.pickerPosition == 'top-right') {
        top = offset.top - this.picker.outerHeight();
      } else {
        top = offset.top + this.height;
      }*/

      top = top - containerOffset.top + ;
      left = left - containerOffset.left + ;

      this.picker.css({
        top:    top,
        left:   left,
        zIndex: this.zIndex
      });
    },
           

上面就是相關的源碼,可以看到,注釋了line 527行之後,在後面引用了一個未初始化過的top變量

嗯… 這是一個沒經過測試就送出的小BUG…

繼續閱讀