如何在 .animate 上添加输入宽度

发布于 2025-01-05 02:01:52 字数 245 浏览 3 评论 0原文

我想在 .animate({width: "auto"}, 'slow'); 上设置 width auto,但这不起作用? 如果有人可以提供帮助,我将非常感激。

小提琴:

http://jsfiddle.net/DCjYA/327/

谢谢你 !

I want to set width auto on .animate({width: "auto"}, 'slow');, but this is not working?
If someone can help I'll really appreciate.

fiddle:

http://jsfiddle.net/DCjYA/327/

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2025-01-12 02:01:52

编辑:
在模糊上添加了一个动态元素(按钮)来计算宽度并立即删除。所以您不需要更改 html 中的任何内容。

DEMO

var $getWidth = $('#getTextWidth');

$('input[type=text]')
.focus(function() {
    var $this = $(this);
    if ($this.val().length > 22) {
        //$(this).data('default', $(this).data('default') || $(this).width());
        $this.stop().animate({width: 370}, 'slow');
        $this.parent().addClass('cooling');
    }
})
.blur(function() { /* lookup the original width */
    var $this = $(this);

    var textWidth = $('<input type="button" />')
        .appendTo('body')
        .attr({'id': 'tmpSpan'})
        .css({display: 'none'})
        .val($this.val()).outerWidth();
    $('#tmpSpan').remove();
    $this.stop().animate({width: textWidth + 'px'},'fast');
    $this.parent().removeClass('cooling');
});

我添加了一个隐藏的跨度,然后在span 来计算跨度的宽度。然后获取跨度的外部宽度并将其应用于模糊的文本框。

演示

Edit:
Added a dynamic element (button) on blur to calculate the width and removed right away.. So you don't need to change anything in html.

DEMO

var $getWidth = $('#getTextWidth');

$('input[type=text]')
.focus(function() {
    var $this = $(this);
    if ($this.val().length > 22) {
        //$(this).data('default', $(this).data('default') || $(this).width());
        $this.stop().animate({width: 370}, 'slow');
        $this.parent().addClass('cooling');
    }
})
.blur(function() { /* lookup the original width */
    var $this = $(this);

    var textWidth = $('<input type="button" />')
        .appendTo('body')
        .attr({'id': 'tmpSpan'})
        .css({display: 'none'})
        .val($this.val()).outerWidth();
    $('#tmpSpan').remove();
    $this.stop().animate({width: textWidth + 'px'},'fast');
    $this.parent().removeClass('cooling');
});

I added a hidden span, and then added the textbox content inside the span to calculate the width of the span. Then got the outerWidth of the span and applied it to textbox on blur.

DEMO

半衾梦 2025-01-12 02:01:52

这是有效的及其简单的 DEMO

$(document).ready(function () {
$("input[type=text]").focus(function () {
    $(this).animate({
        width: "160px"
    }, 100);
});
$(this).focusout(function () {
    $("input[type=text]").animate({
        width: "130px"
    }, 100);
});});

This works and its simple DEMO

$(document).ready(function () {
$("input[type=text]").focus(function () {
    $(this).animate({
        width: "160px"
    }, 100);
});
$(this).focusout(function () {
    $("input[type=text]").animate({
        width: "130px"
    }, 100);
});});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文