jquery - 在 jQuery 中用自定义 div 替换动画宽度

标签 jquery html css onclick

我制作了一个简单的点击动画输入。我也做了一些基本样式,但我不知道如何替换 {width:'500px'} 宽度自定义 div。在这种情况下:.customwidth

这是我的完整代码:

HTML

<div class="search">
    <input type="text" placeholder="Fast search" />
    <i class="fa fa-search"></i>
</div>

CSS

.search { float:left; margin:10px 0px 0 10px;  height:31px; border:1px solid rgba(0, 0, 0, 0.10); border-radius:15px;  }
.search i { float:right; margin:6px 10px; }
.search input { float:left; margin:2px 0px 0 10px; width:70px; height:25px; border:none; outline:none; }

.customwidth { width:500px; }

JS

$(".search").click(function () {
  $('.search input').animate({ width:'500px'}, { duration:200, specialEasing: { width: 'linear' } });
});

JSFIDDLE

我想从 css 文件更改动画宽度,因此,我需要替换 width:'500px' width .customwidth 类。

这可能吗?

最佳答案

只需 .addCLass('customwidth')

DEMO

$(".search").click(function () {
    $('.search input').addClass('customwidth');
});

然后在 CSS 中使用过渡。我更改了您的 customwidth 选择器的特殊性,因为您不必使用 !important。从长远来看可能会让你陷入困境。

.search .customwidth {
    width:500px;
    transition: width .2s ease;
}

关于jquery - 在 jQuery 中用自定义 div 替换动画宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29491257/

相关文章:

html - 使用 Internet Explorer 显示 z-index 时出现问题

javascript - 通过 jquery 禁用按钮

html - 使 overflow-y 显示在 <ul> 边框内

javascript - 在线、间歇、离线 php ping 脚本

css - UIWebView iOS 4.2.1 中的阿拉伯语 TTF(TrueType 字体)

html - :Hover problem in chrome

javascript - 克隆元素并将其添加到另一个元素中

jquery - 使用 Angularjs 加载文本的 Bootstrap 按钮

jquery - 检查此数组中的值,JQuery

html - 检查器中的 CSS 元素显示错误的代码行?