【发布时间】:2015-01-07 07:00:20
【问题描述】:
我有一个块元素,它会根据其所处的状态改变其宽度。
Here's a fiddle 告诉你我的意思。
单击黑色按钮时,您会看到该按钮内的文本发生变化,从而减小了整体宽度。我想知道是否有一种方法可以通过 CSS 或 JS 让我在单击按钮时获得平滑动画的宽度。
HTML
<a id="contact-button" href="#">Get in touch</a>
<div id="primary"></div>
<div id="contact-keebs"></div>
CSS
#contact-button {
background: #000;
background-size: 24px;
color: #fff;
text-decoration: none;
text-transform: uppercase;
font-weight: bold;
padding: 10px 20px;
position: absolute;
right: 0;
top: 0;
z-index: 1;
}
#primary {
background: red;
height: 200px;
position: absolute;
width: 200px;
}
#contact-keebs {
background: blue;
display: none;
height: 200px;
position: absolute;
width: 200px;
}
JS
var c = 0;
$('#contact-button').click(function (e) {
e.preventDefault();
if (c++ % 2 == 0) {
$(this).addClass('work-button').text('Work');
$('body').addClass('contact-content');
$('#contact-keebs').finish().stop().fadeIn(500);
} else {
$(this).removeClass('work-button').text('Get in touch');
$('body').removeClass('contact-content');
$('#contact-keebs').finish().stop().fadeOut(500);
}
});
【问题讨论】:
标签: javascript jquery css