Jquery - 动画高度切换
我在屏幕顶部有一个 10 像素的栏,当单击该栏时,我希望它以动画方式显示到 40 像素的高度,然后如果再次单击,则以动画方式返回到 10 像素。我尝试更改 div 的 id,但它不起作用。我怎样才能让它发挥作用,或者有更好的方法吗?
正文 html:
css:
#topbar-show { width: 100%; height: 10px; background-color: #000; }
#topbar-hide { width: 100%; height: 40px; background-color: #000; }
javascript:
$(document).ready(function(){
$("#topbar-show").click(function(){
$(this).animate({height:40},200).attr('id', 'topbar-hide');
});
$("#topbar-hide").click(function(){
$(this).animate({height:10},200).attr('id', 'topbar-show');
});
});
I have a 10px bar along the top of the screen that, when clicked, I want it to animate to a height of 40px and then if clicked again, animate back down to 10px. I tried changing the id of the div, but it isn't working. How could I get this to work, or is there a better way to do it?
body html:
<div id="topbar-show"></div>
css:
#topbar-show { width: 100%; height: 10px; background-color: #000; }
#topbar-hide { width: 100%; height: 40px; background-color: #000; }
javascript:
$(document).ready(function(){
$("#topbar-show").click(function(){
$(this).animate({height:40},200).attr('id', 'topbar-hide');
});
$("#topbar-hide").click(function(){
$(this).animate({height:10},200).attr('id', 'topbar-show');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试一下:
Give this a try:
您应该使用
class
来实现您想要的:css:
javascript:
You should be using a
class
to achieve what you want:css:
javascript:
您可以使用
toggle-event
(docs)< /i> 方法分配 2 个(或更多)每次点击时切换的处理程序。示例: http://jsfiddle.net/SQHQ2/1/
或您可以创建自己的切换行为:
示例: http://jsfiddle.net/SQHQ2/< /a>
You can use the
toggle-event
(docs) method to assign 2 (or more) handlers that toggle with each click.Example: http://jsfiddle.net/SQHQ2/1/
or you could create your own toggle behavior:
Example: http://jsfiddle.net/SQHQ2/
很晚了但我很抱歉。抱歉,如果这“效率低下”,但如果您发现上述所有方法均不起作用,请尝试此操作。也适用于 1.10 以上
Very late but I apologize. Sorry if this is "inefficient" but if you found all the above not working, do try this. Works for above 1.10 also
下面的代码在 jQuery2.1.3 中为我工作,
不需要计算你的 div 高度、填充、边距和边框。它会照顾的。
The below code worked for me in jQuery2.1.3
Need not calculate your div height,padding,margin and borders. It will take care.
这将是一种可能性:
That would be a possibility:
你也可以使用这个技巧:
替换
为
这将在尚未加载的对象上绑定一个事件...
;)
You can also use this trick:
replace
by
This will bind an event on a not loaded yet object...
;)
我只是想告诉您解决方案不起作用的原因:
当
$(document).ready()
执行时仅$('#topbar-show')
选择器可以从 DOM 中找到匹配的元素。#topbar-show
元素尚未创建。要解决此问题,您可以使用实时事件绑定,
这是修复解决方案的最简单方法。这些答案的其余部分进一步为您提供更好的解决方案,而不是修改希望永久的 id 属性。
I just thought to give you the reason why your solution did not work:
When
$(document).ready()
is executed only the$('#topbar-show')
selector can find a matching element from the DOM. The#topbar-show
element has not been created yet.To get past this problem, you may use live event bindings
This is the most simple way to fix you solution. The rest of these answer go further to provide you a better solutions instead that do not modify the hopefully permanent id attribute.
为我工作:
Worked for me: