我在CSS中遇到了一些麻烦。我想做的是使用selectize.js在材料选择中添加一个浮动标签,如材料设计(我想):
.selectize-control {
.selectize-input{
border: none;
border-radius: 0 !important;
box-shadow: none !important;
background: transparent;
border: none;
border-radius: 0 !important;
box-shadow: none !important;
display: block !important;
width: 100% !important;
height: 43px !important;
font-size: 15px !important;
background: none !important;
font-family: "Novecento Normal" !important;
}
border-bottom: 2px solid #e3e6f0 !important;
&:focus-within {
border-bottom: 2px solid #7a2932 !important;
& + span {
transform: translateY(-25px) scale(1) !important;
color: #7a2932 !important;
& + .border {
transform: scaleX(1) !important;
}
}
}
}
类似于此JSFiddle所示。
问题是:当焦点丢失时,即使输入具有值,标签也会自动变回原位。
我很确定问题出在
&:focus-within {...
但是我无法解决这个问题。
有人知道该怎么做吗?
答案 0 :(得分:2)
请勿使用“:focus-within”,因为此CSS不支持Internet Explorer和Edge浏览器。 请查看以下链接: https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within
$(function() {
$('#select-name').selectize({
plugins: ['remove_button']
});
// Add this js
$('.selectize-control').on("blur", ".selectize-input", function () {
if($(this).hasClass("has-items")){
$('.selectize-control').next("span").addClass("active");
}else{
$('.selectize-control').next("span").removeClass("active");
}
});
});
像这样使用CSS
.selectize-control + span.active{ transform: translateY(-25px) scale(1) !important; color: #7a2932 !important; }
答案 1 :(得分:0)
感谢Vipin,但我做到了codepen
(function($el) {
var $label = $el.prev();
$el.selectize({
plugins: ['remove_button'],
onFocus: function() {
$label.addClass('fp-floating-label--focused');
},
onBlur: function() {
$label.removeClass('fp-floating-label--focused');
},
onItemAdd: function(){
$label.addClass('fp-floating-label--valued');
},
onDelete: function(){
var count = $el.find('option').length
if (count <= 1){
$label.removeClass('fp-floating-label--valued');
$(this).focus();
$label.removeClass('fp-floating-label--focused');
}
},
onChange: function(value) {
value = value.trim();
if (value) {
$label.addClass('fp-floating-label--valued');
} else {
$label.removeClass('fp-floating-label--valued');
}
}
});