简体   繁体   English

将最小Z索引设置为jQuery Datepicker

[英]Set minimum z-index to jquery datepicker

I have case, when my datepicker and some div with buttons overlaps in wrong way. 我有情况,当我的日期选择器和带有按钮的某些div以错误的方式重叠时。 For this case I can set manually z-Index to datepicker(for my case z-Index:3 will be fine), but this will broke other places, as z-index is calculated by library (as I know). 对于这种情况,我可以手动将z-Index设置为datepicker(对于我的情况,z-Index:3会很好),但这会破坏其他地方,因为z-index是由库计算的(据我所知)。

So I want to set minimum z-index value to datepicker or just increase z-Index. 所以我想将最小的z-index值设置为datepicker或只是增加z-Index。

So I add to setting object own beforeShow function: 所以我添加了设置对象自己的beforeShow函数:

options.beforeShow = function (input,inst ){
    inst.dpDiv.css('z-index', inst.dpDiv.css('z-index') + 2);
}

But this still didn't work. 但这仍然行不通。

Full Example: http://jsfiddle.net/NAgNV/2314/ 完整示例: http : //jsfiddle.net/NAgNV/2314/


PS I can't change z-Index of div with buttons. 附注:我无法使用按钮更改div的z-Index。

ANSWER UPDATE 答案更新

The event handler beforeShow runs inside the _showDatepicker method. 事件处理程序beforeShow_showDatepicker方法内运行。 If you wish to overwrite an internal method I suggest to use this one instead of _updateDatepicker : 如果您想覆盖一种内部方法,我建议使用此方法而不是_updateDatepicker

$.datepicker._showDatepicker_original = $.datepicker._showDatepicker;
   $.datepicker._showDatepicker = function(input) {
   $.datepicker._showDatepicker_original(input);
   // now change the z-index
   $.datepicker.dpDiv.css('z-index', +$.datepicker.dpDiv.css('z-index') + 2);
}

Now, you don't need anymore the event beforeShow : 现在,您不再需要beforeShow事件:

The snippet: 片段:

 ko.bindingHandlers.datepicker = { init: function(element, valueAccessor, allBindingsAccessor) { var $el = $(element); //initialize datepicker with some optional options var options = allBindingsAccessor().datepickerOptions || {}; $.datepicker._showDatepicker_original = $.datepicker._showDatepicker; $.datepicker._showDatepicker = function(input) { $.datepicker._showDatepicker_original(input); // now change the z-index console.log('Initial z-index: ' + $.datepicker.dpDiv.css('z-index')); $.datepicker.dpDiv.css('z-index', +$.datepicker.dpDiv.css('z-index') + 2); console.log('Changed z-index: ' + $.datepicker.dpDiv.css('z-index')); } $el.datepicker(options); //handle the field changing ko.utils.registerEventHandler(element, "change", function() { var observable = valueAccessor(); observable($el.datepicker("getDate")); }); //handle disposal (if KO removes by the template binding) ko.utils.domNodeDisposal.addDisposeCallback(element, function() { $el.datepicker("destroy"); }); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()), $el = $(element), current = $el.datepicker("getDate"); if (value - current !== 0) { $el.datepicker("setDate", value); } } }; var viewModel = { myDate: ko.observable(new Date("12/01/2013")), setToCurrentDate: function() { this.myDate(new Date()); } }; ko.applyBindings(viewModel); 
 td, th { padding: 5px; border: solid 1px black; } th { color: #666; } a { font-size: .75em; } .scrolled {height : 100px;} .btn-c{z-index: 3; position : relative;} 
 <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script> <div class='scrolled'> <input data-bind="datepicker: myDate, datepickerOptions: { minDate: new Date() }" /> </div> <hr /> <div class='btn-c'> <button data-bind="click: setToCurrentDate">Set To Current Date</button> </div> <hr /> <div data-bind="text: myDate"></div> 

OLD ANSWER 老答案

Because on beforeShow z-index value is reset you may delay the: 由于在beforeShow z-index值重置时,您可能会延迟:

inst.dpDiv.css('z-index', inst.dpDiv.css('z-index') + 2);

Your updated fiddle and the example: 您更新的小提琴和示例:

 ko.bindingHandlers.datepicker = { init: function(element, valueAccessor, allBindingsAccessor) { var $el = $(element); //initialize datepicker with some optional options var options = allBindingsAccessor().datepickerOptions || {}; options.beforeShow = function (input,inst ){ setTimeout(function() { inst.dpDiv.css('z-index', inst.dpDiv.css('z-index') + 2); }, 1); } $el.datepicker(options); //handle the field changing ko.utils.registerEventHandler(element, "change", function() { var observable = valueAccessor(); observable($el.datepicker("getDate")); }); //handle disposal (if KO removes by the template binding) ko.utils.domNodeDisposal.addDisposeCallback(element, function() { $el.datepicker("destroy"); }); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()), $el = $(element), current = $el.datepicker("getDate"); if (value - current !== 0) { $el.datepicker("setDate", value); } } }; var viewModel = { myDate: ko.observable(new Date("12/01/2013")), setToCurrentDate: function() { this.myDate(new Date()); } }; ko.applyBindings(viewModel); 
 td, th { padding: 5px; border: solid 1px black; } th { color: #666; } a { font-size: .75em; } .scrolled {height : 100px;} .btn-c{z-index: 3; position : relative;} 
 <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script> <div class='scrolled'> <input data-bind="datepicker: myDate, datepickerOptions: { minDate: new Date() }" /> </div> <hr /> <div class='btn-c'> <button data-bind="click: setToCurrentDate">Set To Current Date</button> </div> <hr /> <div data-bind="text: myDate"></div> 

Simply add !important in your css file: 只需在您的css文件中添加!important即可

.ui-datepicker {z-index:4 !important;}

http://jsfiddle.net/q693qtdt/ http://jsfiddle.net/q693qtdt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM