输入字段中的响应文本
Responsive Text in Inputfield
我有一个输入字段,我通过 JavaScript 在其中添加值。
我该怎么做,如果我的输入字段是 "full",例如 30 个字符,字体会变小,以便所有内容始终可见?
input {
height:50px;
width:200px;
font-size:20px;
padding:5px;
}
<input type="text">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
input {
height:50px;
width:200px;
font-size:20px;
padding:5px;
}
</style>
<input type="text" name="input" id="input">
<script>
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
$.fn.inputfit = function(options) {
var settings = $.extend({
minSize : 10,
maxSize : false
}, options);
this.each(function() {
var $input = $(this);
if ( !$input.is(':input') ) {
return;
}
$input.off('keyup.inputfit keydown.inputfit');
var maxSize = parseFloat(settings.maxSize || $input.css('font-size'), 10);
var width = $input.width();
var clone = $input.data('inputfit-clone');
if (!clone) {
clone = $('<div></div>', {
css : {
fontSize : $input.css('font-size'),
fontFamily : $input.css('font-family'),
fontStyle : $input.css('font-style'),
fontWeight : $input.css('font-weight'),
fontVariant : $input.css('font-variant'),
letterSpacing: $input.css('letter-spacing'),
whiteSpace : 'nowrap',
position : 'absolute',
left : '-9999px',
visibility : 'hidden'
}
}).insertAfter($input);
$input.data('inputfit-clone', clone);
}
$input.on('keyup.inputfit keydown.inputfit', function() {
var $this = $(this);
clone.text($this.val());
var ratio = width / (clone.width() || 1),
currentFontSize = parseInt( $this.css('font-size'), 10 ),
fontSize = Math.floor(currentFontSize * ratio);
if (fontSize > maxSize) { fontSize = maxSize; }
if (fontSize < settings.minSize) { fontSize = settings.minSize; }
$this.css('font-size', fontSize);
clone.css('font-size', fontSize);
}).triggerHandler('keyup.inputfit');
});
return this;
};
}));
$('input').inputfit();
</script>
尽我所能,这是您要找的吗?
我有一个输入字段,我通过 JavaScript 在其中添加值。 我该怎么做,如果我的输入字段是 "full",例如 30 个字符,字体会变小,以便所有内容始终可见?
input {
height:50px;
width:200px;
font-size:20px;
padding:5px;
}
<input type="text">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
input {
height:50px;
width:200px;
font-size:20px;
padding:5px;
}
</style>
<input type="text" name="input" id="input">
<script>
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
$.fn.inputfit = function(options) {
var settings = $.extend({
minSize : 10,
maxSize : false
}, options);
this.each(function() {
var $input = $(this);
if ( !$input.is(':input') ) {
return;
}
$input.off('keyup.inputfit keydown.inputfit');
var maxSize = parseFloat(settings.maxSize || $input.css('font-size'), 10);
var width = $input.width();
var clone = $input.data('inputfit-clone');
if (!clone) {
clone = $('<div></div>', {
css : {
fontSize : $input.css('font-size'),
fontFamily : $input.css('font-family'),
fontStyle : $input.css('font-style'),
fontWeight : $input.css('font-weight'),
fontVariant : $input.css('font-variant'),
letterSpacing: $input.css('letter-spacing'),
whiteSpace : 'nowrap',
position : 'absolute',
left : '-9999px',
visibility : 'hidden'
}
}).insertAfter($input);
$input.data('inputfit-clone', clone);
}
$input.on('keyup.inputfit keydown.inputfit', function() {
var $this = $(this);
clone.text($this.val());
var ratio = width / (clone.width() || 1),
currentFontSize = parseInt( $this.css('font-size'), 10 ),
fontSize = Math.floor(currentFontSize * ratio);
if (fontSize > maxSize) { fontSize = maxSize; }
if (fontSize < settings.minSize) { fontSize = settings.minSize; }
$this.css('font-size', fontSize);
clone.css('font-size', fontSize);
}).triggerHandler('keyup.inputfit');
});
return this;
};
}));
$('input').inputfit();
</script>
尽我所能,这是您要找的吗?