如果任何其他 div 使用 jQuery 向下滑动,则 SlideUp 一个 div

发布于 2024-10-10 19:39:24 字数 451 浏览 6 评论 0原文

我创建了一个功能,可以通过单击不同的链接来向下滑动不同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揪着可爱 2024-10-17 19:39:24

通常,您可以通过向您希望能够控制的所有 div 添加一个类,然后在每次单击开始时对该类进行 SlideUp() 操作来完成此操作。

$('a.xxx').click(function(){
    $('.sliders').slideUp()
    // slide down other div here
});

这实际上会使所有带有“滑块”类的 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.

$('a.xxx').click(function(){
    $('.sliders').slideUp()
    // slide down other div here
});

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.

无风消散 2024-10-17 19:39:24

您可能想查看 jQuery UI 手风琴

You might want to look into the jQuery UI accordion.

断舍离 2024-10-17 19:39:24

如果您正在滑动的 div 是同级的,并且类名和 ID 之间的对应关系如所描述的,您可以执行以下操作:

var links = ['a.about','a.info','a.contact','a.etc'];

$( links.join(',') ).click( function(e){
  e.preventDefault();
  $('#' + this.className).siblings().slideUp('slow').end().slideDown('slow');
});

如果所有链接都在某种容器内,您可以进一步简化:

$('div#nav-container').find('a').click( function(e){...} );

If the divs you're sliding are siblings, and the correspondence between classnames and IDs as described, you can do something like this:

var links = ['a.about','a.info','a.contact','a.etc'];

$( links.join(',') ).click( function(e){
  e.preventDefault();
  $('#' + this.className).siblings().slideUp('slow').end().slideDown('slow');
});

If all the links are within a container of some kind, you can simplify further:

$('div#nav-container').find('a').click( function(e){...} );
吻泪 2024-10-17 19:39:24

最简单的方法是为您尝试向上/向下滑动的所有元素提供额外的通用类名,然后使用锚点上的 href 属性将实际锚点连接到元素。下面的 javascript 代码(对 HTML 进行了更改)将执行与当前 javascript 相同的功能,并且可以轻松添加更多滑块:

# the html
<a class='about slide-control' href='#about'>toggle the about section</a>
<a class='info slide-control' href='#info'>toggle the info section</a>
<a class='contact slide-control' href='#contact'>toggle the contact section</a>

<div class='slider' id='about'>
    this is the about section
</div>
<div class='slider' id='info'>
    this is the info section
</div>
<div class='slider' id='contact'>
    this is the contact section
</div>    

# the javascript
$('a.slide-control').click(function() {
    $('div.slider').slideUp();
    var target = $(this).attr('href');
    $(target).slideDown('slow');
});

您只需添加正确的 HTML 即可添加新滑块:

<a class='slide-control' href='#new_section'>Tell me more about this new section!</a>

<div class='slider' id='new_section'>
    This is a great paragraph about the new section on our website!
</div>

或者,您也 可以可以使用现有的类,只需在 .slideDown 之前添加对其他元素的 .slideUp 方法调用:

$('a.about').click(function() {
    $("#info").slideUp('slow');
    $("#contact").slideUp('slow');
    $("#about").slideDown('slow');
});

// repeat for each click handler

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:

# the html
<a class='about slide-control' href='#about'>toggle the about section</a>
<a class='info slide-control' href='#info'>toggle the info section</a>
<a class='contact slide-control' href='#contact'>toggle the contact section</a>

<div class='slider' id='about'>
    this is the about section
</div>
<div class='slider' id='info'>
    this is the info section
</div>
<div class='slider' id='contact'>
    this is the contact section
</div>    

# the javascript
$('a.slide-control').click(function() {
    $('div.slider').slideUp();
    var target = $(this).attr('href');
    $(target).slideDown('slow');
});

You can add a new slider simply by adding the proper HTML:

<a class='slide-control' href='#new_section'>Tell me more about this new section!</a>

<div class='slider' id='new_section'>
    This is a great paragraph about the new section on our website!
</div>

Alternatively, you can use your existing classes and just add the .slideUp method call for your other elements before your .slideDown:

$('a.about').click(function() {
    $("#info").slideUp('slow');
    $("#contact").slideUp('slow');
    $("#about").slideDown('slow');
});

// repeat for each click handler
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文