我在使用 jQuery 切换侧边栏时遇到问题
我正在尝试使用 jquery 创建一个简单的切换侧边栏,按下按钮时它会展开和收缩。 按下时该按钮也会变为另一种类型的按钮。 侧边栏将会展开,但由于某种原因,它不会移回原来的位置。 您可以在 http://www.jqueryhelp 中查看 javascript 和 html 的副本.com/viewtopic.php?p=4241#4241
这是工作代码,谢谢 Bendeway! :D
$(".btn-slide").live('click', function(e){
e.preventDefault();
$("#sidebar").animate({opacity: "show", left: 250}, "slow");
$(this).toggleClass("btn-slide").toggleClass("active");
});
$(".active").live('click', function(e){
e.preventDefault();
$("#sidebar").animate({opacity: "hide", left: 100}, "slow");
$(this).toggleClass("btn-slide").toggleClass("active");
});
I'm trying to create a simple toggling sidebar using jquery, where it expands and contracts when a button is pressed. The button also changes to another type of button when pressed. The sidebar will expand, but for some reason, it will not move back to it's original position.
You can see a copy of the javascript and html at http://www.jqueryhelp.com/viewtopic.php?p=4241#4241
Here is the working code, thanks Bendeway! :D
$(".btn-slide").live('click', function(e){
e.preventDefault();
$("#sidebar").animate({opacity: "show", left: 250}, "slow");
$(this).toggleClass("btn-slide").toggleClass("active");
});
$(".active").live('click', function(e){
e.preventDefault();
$("#sidebar").animate({opacity: "hide", left: 100}, "slow");
$(this).toggleClass("btn-slide").toggleClass("active");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用带有负数的 left 来代替 right。 另外我建议使用 PreventDefault 而不是返回 false。
更新
我刚刚注意到的另一件事是,当文档准备好时,您将单击事件附加到 .active 按钮,但是当文档准备好时,在您更改它后没有出现 .active 按钮。 这里有几个选项。
首先是使用 jquery 1.3 的新 live 功能。
另一个选项是设置单击不同修饰符上的事件(例如,可能在 id 上)。
然后用它来处理点击
或更简洁
我能想到的最后一个选项是在更改点击事件后设置它们,但我不会这样做,所以我不会提供示例。
最后,我会将警报放入您的活动回调中,并确保您的活动按钮事件实际上正在触发。
try instead of right use left with a negative number. in addition I would recommend using preventDefault instead of returning false.
Update
Another piece i just noticed is that your attaching a click event to the .active button, when the document is ready, but there is no .active button when the document is ready that comes in after you change it. There are a couple options here.
First is to use the new live feature of jquery 1.3
The other option would be to have set the click event on a different modifier (eg. on the id, maybe).
then use this to handle the click
or even more concise
The final option that I can think of would be to set the click events after you change them, but I wouldn't do that so I'm not going to supply a sample.
Lastly, I would put in alert into your active callback and make sure that your active button event is actually firing.
按照您的逻辑编写方式,我认为您需要在单击处理程序内的两个类上执行“toggleClass”,这将添加一个并删除另一个。 例如,当单击“活动”项目时,您会切换(添加)btn-slide 类,但这也会将“活动”类保留在原位。
当然,除了使用“toggleClass”之外,您还可以使用“addClass”和“removeClass”来更明确。
我建议使用 Firebug 之类的工具来观察 DOM 内部发生的情况。
The way your logic is written, I think you need to do a 'toggleClass' on both classes inside your click handlers which will add one and remove the other. For example, when your "active" item is clicked you toggle (add) the btn-slide class, but this will leave the "active" class in place too.
Of course instead of using "toggleClass" you could also use "addClass" and "removeClass" to be more explicit.
I recommend using a tool like Firebug to watch what's happening inside your DOM.
这有效。
This works.
我选择以稍微不同的方式来做这件事。 初始化时我们实验的高度为 200px,宽度是自动的。 当您单击超链接时,实验将在 1.3 秒内隐藏,开关设置为 0。当您再次单击时,它会在 1.3 秒内返回,开关设置回 1。就像电灯开关一样。 我在这里使用了动画,因为简单的淡入淡出或隐藏让我感到无聊......
我在归零之前获得元素的宽度和高度的原因是如果在中说宽度:100%”和高度:“100%”我的动画会将其设置为页面宽度和高度的 100%...我们不希望这样
注意:我还在一段时间内使用了非常好的淡入淡出效果。出色地 :)
I chose to do this a little differently. this initializes with the height of our experiment being 200px and the width is automatic. When you click the hyperlink the experiment is hidden in 1.3 seconds and a switch is set to 0. when you click again it comes back over a period of 1.3 seconds and the switch gets set back to 1. like a light switch. I used the animate here because a simple fade or hide got me bored...
The reason why I got the widths and heights of the element before zero-ing them was if said width:100%" and height: "100%" in my animate it would set it to 100% of the page's width and height...we don't want that.
NOTE: I am also using the opacity over a time period. quite nice for fading as well :)