如果任何其他 div 使用 jQuery 向下滑动,则 SlideUp 一个 div
我创建了一个功能,可以通过单击不同的链接来向下滑动不同的 div。以下是基本代码。但是,有一个小问题,例如,如果我单击第一个链接,它会向下滑动 div。现在,如果我单击下一个链接,它会保留第一个 div,并向下滑动第二个 div。不过,我想这样做,如果有任何 div 向下滑动,并且我单击任何链接,它应该向上滑动前一个 div 并向下滑动新的 div。我将感谢您在这方面的帮助。谢谢。
$("a.about").click(function(){
$("#about").slideDown("slow");
});
$("a.info").click(function(){
$("#info").slideDown("slow");
});
$("a.contact").click(function(){
$("#contact").slideDown("slow");
});
I have made a function to slidedown different div's by clicking different links. Following is basic code. However, there's a little problem that, for example, if i click on first link, it slides down div. Now If i click on next link, it keeps the first div, and slides down the second div. However I want to do it so that, if there is any div slided down, and I click on any link, it should slide up the previous div and slide down the newer one. I will appriciate your help in this regard. Thanks.
$("a.about").click(function(){
$("#about").slideDown("slow");
});
$("a.info").click(function(){
$("#info").slideDown("slow");
});
$("a.contact").click(function(){
$("#contact").slideDown("slow");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,您可以通过向您希望能够控制的所有 div 添加一个类,然后在每次单击开始时对该类进行 SlideUp() 操作来完成此操作。
这实际上会使所有带有“滑块”类的 div 向上滑动,但会产生先前单击的 div 向上滑动的错觉,因为只有一个应该向下滑动。
you can do this generally by adding a class to all the divs you want to be able to control, and then slideUp()'ing that class at the beginning of every click.
this will actually slide up ALL divs with the class 'sliders' but will give the illusion of the previously clicked div sliding up as only one should ever be down.
您可能想查看 jQuery UI 手风琴。
You might want to look into the jQuery UI accordion.
如果您正在滑动的 div 是同级的,并且类名和 ID 之间的对应关系如所描述的,您可以执行以下操作:
如果所有链接都在某种容器内,您可以进一步简化:
If the divs you're sliding are siblings, and the correspondence between classnames and IDs as described, you can do something like this:
If all the links are within a container of some kind, you can simplify further:
最简单的方法是为您尝试向上/向下滑动的所有元素提供额外的通用类名,然后使用锚点上的
href
属性将实际锚点连接到元素。下面的 javascript 代码(对 HTML 进行了更改)将执行与当前 javascript 相同的功能,并且可以轻松添加更多滑块:您只需添加正确的 HTML 即可添加新滑块:
或者,您也 可以可以使用现有的类,只需在
.slideDown
之前添加对其他元素的.slideUp
方法调用:The easiest approach would be to give an extra generic classname to all of the elements you are trying to slide up/down, then connect the actual anchors to the elements, using the
href
attribute on the anchor. The below bit of javascript (with the changes to the HTML) will perform the same functions as your current javascript, as well as make it easy to add more sliders:You can add a new slider simply by adding the proper HTML:
Alternatively, you can use your existing classes and just add the
.slideUp
method call for your other elements before your.slideDown
: