简体   繁体   English

带有多个目标的jQuery slideDown

[英]jQuery slideDown with multiple targets

I'm hoping someone can help me make this work correctly. 我希望有人能帮助我正确完成这项工作。

I have this HTML: 我有这个HTML:

    <div id="courses_container" class="row">
    <div class="section group">
        <div class="col one-third">
            <p class="mod-count">1</p>
            <p class="reveal"><a class="modselect" target="4">view modules</a></p>
        </div>
        <div class="col one-third">
            <p class="mod-count">2</p>
            <p class="reveal"><a class="modselect" target="5">view modules</a></p>
        </div>
        <div class="col one-third">
            <p class="mod-count">3</p>
            <p class="reveal"><a class="modselect" target="6">view modules</a></p>
        </div>
        <div class="col one-third">
            <p class="mod-count">4</p>
            <p class="reveal"><a class="modselect" target="7">view modules</a></p>
        </div>
        <div class="col one-third">
            <p class="mod-count">5</p>
            <p class="reveal"><a class="modselect" target="8">view modules</a></p>
        </div>
        <div class="col one-third">
            <p class="mod-count">6</p>
            <p class="reveal"><a class="modselect" target="9">view modules</a></p>
        </div>
    </div>
</div>

<div id="child-container">
    <div class="row first" id="modules-container-4">
        <div class="section group">
            <div class="col fw"><p>WILL CONTAIN THE MODULES IN COURSE 4</p></div>
        </div>
    </div> 
    <div class="row" id="modules-container-5">
        <div class="section group">
            <div class="col fw"><p>WILL CONTAIN THE MODULES IN COURSE 5</p>
            <p>SOME OF THESE WILL HAVE LONGER CONTENT</p></div>
        </div>
    </div>
    <div class="row" id="modules-container-6">
        <div class="section group">
            <div class="col fw"><p>WILL CONTAIN THE MODULES IN COURSE 6</p>
            <p>SOME OF THESE WILL HAVE LONGER CONTENT</p>
            <p>AND THE CONTENT OF SOME WILL BE LONGER STILL</p></div>
        </div>
    </div>
    <div class="row" id="modules-container-7">
        <div class="section group">
            <div class="col fw"><p>WILL CONTAIN THE MODULES IN COURSE 7</p></div>
        </div>
    </div>
</div>

This CSS: 此CSS:

#child-container {
    position: relative;
    width: 100%;
    margin: 0 auto;
    background-color: #000;
    display: none;
}

#child-container p {
    color: #fff;
    font-size: 1em;
    line-height: 1.1em;
    text-align: center;
}

#child-container .row:not(.first) {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
}

#child-container .row.first {
    display: block;
}


.row {
    position: relative;
    width: 100%;
    margin: 0 auto;
}

.section {
    clear: both;
    padding: 0;
    margin: 0;
}

.col {
    display: block;
    float:left;
    margin: 1% 0 1% 1.6%;
}

.col:first-child { margin-left: 0; }

.group:before,
.group:after {
    content:"";
    display:table;
}

.group:after {
    clear:both;
}

.fw {
    width: 100%;
}

#courses_container .one-third:nth-child(4n+0) {
    margin-left: 0;
}

.one-third {
    width: 32.26%;
}

And this jQuery: 而这个jQuery:

(function ($) {
    $(document);
}(jQuery));

jQuery(document).ready(function() {

    jQuery('.reveal').on('click', function(){

        jQuery('#child-container').slideDown();

    });

})

Let's have a fiddle! 让我们摆弄吧! : http://jsfiddle.net/4gMDH/5/ http//jsfiddle.net/4gMDH/5/

I know my jQuery is probably pretty far off, but I've backtracked to where I last had it closest to working (if that makes sense!) 我知道我的jQuery可能还差得很远,但是我已经回溯到上一次使它最接近工作的地方(如果这有道理!)

I'm trying to make each of the rows in .child-container respond to a click on links in the set of six above it. 我正在尝试使.child-container中的每一行响应单击它上方的六个集合中的链接。

On each link-click, I'd like the previously selected div to hide before the next is revealed. 在每次单击链接时,我希望在下一个显示之前隐藏先前选择的div。

Thank you. 谢谢。

I think you want something like this. 我想你想要这样的东西。 Change the target to data-target and move it to the element with the reveal class. target更改为data-target然后将其移动到带有reveal类的元素。 I would actually put all this into the <a> element instead of <p> 我实际上会将所有这些内容放入<a>元素而不是<p>

<a class="reveal" data-target="4">

Then in your jQuery: 然后在您的jQuery中:

jQuery('.reveal').on('click', function(e) {
  e.preventDefault();
  var target = jQuery(this).data('target');
  var childid = '#modules-container-'+target;
  jQuery('#child-container > .row').slideUp();
  jQuery(childid).slideDown();
});

What this does is, it grabs the value of data-target from the link which you just clicked, for example, 4. It appends this value to get the id of the row you want to show, #modules-container-4 . 它的作用是,它从刚刚单击的链接中获取data-target的值,例如4。它附加此值以获得要显示的行的ID,即#modules-container-4 Then it hides all of the rows (of course only the currently visible one will slide up), and finally slides down the one you want. 然后,它隐藏了所有行(当然,只有当前可见的行会向上滑动),最后向下滑动所需的行。

Update: I also had to remove display:none from the #child-container CSS, and position:absolute from the rows. 更新:我还必须从#child-container CSS中删除display:none ,并从行中删除position:absolute Here's a fiddle: 这是一个小提琴:

http://jsfiddle.net/CSL65/1/ http://jsfiddle.net/CSL65/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM