gpt4 book ai didi

javascript - 单击时平滑地扩展输入字段宽度

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:38 25 4
gpt4 key购买 nike

我想在单击 .icon 类时显示输入字段 .search。我已经设法用下面的代码做到了这一点。但是,我不想显示/隐藏,而是想通过平滑地扩展输入字段的宽度来显示输入字段。

如何做到这一点?

JSFiddle 演示:

http://jsfiddle.net/9nPW9/

HTML:

<div class="box">
<input class="search" type="search" placeholder="Search" />
<div class="icon"></div>
</div>

CSS:

.box{
position: relative;
width: 220px;
}

.search {
width: 200px;
padding: 5px;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
transition: all .5s ease;
float: left;
}


.icon{
width: 20px;
height: 20px;
background: red;
position: absolute;
right: 0;
}

JS:

$('.icon').click(function() {
$('.search').toggle();
});

最佳答案

为什么不更改代码来切换类,从而在功能 (JS) 和样式 (CSS) 之间保持清晰的分离

下面是一些示例代码,您可能希望根据自己的具体需要进行编辑。

Demo Fiddle

JS

$('.icon').click(function() {
$('.search').toggleClass('expanded');
});

CSS

 .box {
position: relative;
width: 220px;
}
.search {
width: 200px;
max-width:0;
padding: 5px;
transition: all .5s ease;
position:absolute;
right:20px;
box-sizing:border-box;
opacity:0;
}
.search.expanded {
max-width:200px;
opacity:1;
}
.icon {
width: 20px;
height: 20px;
background: red;
position: absolute;
right: 0;
}

关于javascript - 单击时平滑地扩展输入字段宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com