如何将幻灯片应用于向上或向下移动的元素

时间:2017-07-21 22:30:09

标签: javascript jquery html css

我想将.slideUp()或类似的元素应用于淡化消失器的元素之后留下的元素。当元素重新出现时,应用slideDown()。关键是我希望元素移动简单明了。谢谢你的帮助!!下面的代码段可能会更好地解释:

var myJson = [];
    //Accordion-----------------------------------------------
    $(".accordion-desc").fadeOut(0);
    $(".accordion").click(function () {
        $(".accordion-desc").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });


    $(".accordion").click(function () {
        $(".accordion").not(this).find(".rotate").removeClass("down");
        $(this).find(".rotate").toggleClass("down");
    });
    //-----------------------------------------------------------

    //SearchFilter
    $("#searcheBar").on("keyup", function () {
        var g = $(this).val().toLowerCase();
        $(".accordion #title").each(function () {
            var s = $(this).text().toLowerCase();
            if (s.indexOf(g) !== -1) {
                $(this).parent().parent().fadeIn();
            }
            else {
                $(this).parent().parent().fadeOut();
            }
        });
    });
body {
    background-color: #eee;
    font-family: "Open Sans", sans-serif;
}

header {
    background-color: #2cc185;
    color: #fff;
    padding: 2em 1em;
    margin-bottom: 1.5em;
}

h1 {
    font-weight: 300;
    text-align: center;
}

.container {
    position: relative;
    margin: 0 auto;
}

button {
    background-color: #2cc185;
    color: #fff;
    border: 0;
    padding: 1em 1.5em;
}

button:hover {
    background-color: #239768;
    color: #fff;
}

button:focus {
    background-color: #239768;
    color: #fff;
}

.accordion {
    position: relative;
    background-color: #fff;
    display: inline-block;
    width: 100%;
    border-top: 1px solid #f1f4f3;
    border-bottom: 1px solid #f1f4f3;
    font-weight: 700;
    color: #74777b;
    vertical-align: middle;
}
/*Rotation-------------------------------------*/
.accordion .fa {
    position: relative;
    float: right;
}
.rotate {
    -moz-transition: all 0.3s linear;
    -webkit-transition: all 0.3s linear;
    transition: all 0.3s linear;
}

    .rotate.down {
        -moz-transform: rotate(90deg);
        -webkit-transform: rotate(90deg);
        transform: rotate(90deg);
    }
/*------------------------------------------*/
.link {

    text-align: right;
    margin-bottom: 20px;
    margin-right: 30px;
}

.accordion h4 {
    position: relative;
     /* top: 0.8em; */
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    float: left;
}

.accordion a {
    position: relative;
    display: block;
    color: #74777b;
    padding: 1em 1em 2.5em 1em;
    text-decoration: none;
}

.accordion a:hover {
    text-decoration: none;
    color: #2cc185;
    background-color: #e7ecea;
    transition: 0.3s;
}

.accordion-desc {
    background-color: #f1f4f3;
    color: #74777b;
    z-index: 2;
    padding: 20px 15px;
}
@media (min-width:480px) {

    .container { max-width: 80%; }
}
@media (min-width:768px) {

    .container { max-width: 1000px; }
}
.accordion-desc p {
    word-break: break-all;
}

.accordion .status {
    position: relative;
    float: right;
    right: 20%;
    vertical-align: middle;
}
.btn {
    margin-top: 10px;
}
.heading {
    margin:10px 0px 10px 0px;
    vertical-align: middle;
    display: inline-block;
    position: relative;
    width: 100%;
}

.heading h2 {
    float: left;
    position: relative;
    margin:auto;
    vertical-align: middle;
}
.heading .searcheBar {
    float: right;
    position: relative;
    margin: auto;
    vertical-align: middle;

}
.checkboxInput {
    float: right;
    position: relative;
    margin: auto;
    vertical-align: middle;
    right: 40%;
}
.semi {
    min-height: 50px;   

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<b>Type 1, 2 or 3 in the text box</b>

    <div class="heading">
        <input class="searcheBar form-control" id="searcheBar" type="text" />
    </div>
 
                <div class="accordion">
                    <a href="#">
                        <h4 id="title">1</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
                
                        <div class="accordion">
                    <a href="#">
                        <h4 id="title">2</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
                
                        <div class="accordion">
                    <a href="#">
                        <h4 id="title">3</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
 

    <div>

    </div><!-- end of container -->

</div><!-- end of accordion-container -->

2 个答案:

答案 0 :(得分:1)

试试这个:

var myJson = [];
    //Accordion-----------------------------------------------
    $(".accordion-desc").fadeOut(0);
    $(".accordion").click(function () {
        $(".accordion-desc").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });


    $(".accordion").click(function () {
      //$(".accordion").not(this).find(".rotate").removeClass("down");
       // $(this).find(".rotate").toggleClass("down");
    });
    //-----------------------------------------------------------

    //SearchFilter
    $("#searcheBar").on("keyup", function () {
         $(".accordion-desc").fadeOut(0);
        var g = $(this).val().toLowerCase();
        $(".accordion #title").each(function() {
           if( g && $(this).text().toLowerCase().indexOf(g)>-1) {
             $(this).closest('.accordion').click();
           }
        });
        return;
       
    });
body {
    background-color: #eee;
    font-family: "Open Sans", sans-serif;
}

header {
    background-color: #2cc185;
    color: #fff;
    padding: 2em 1em;
    margin-bottom: 1.5em;
}

h1 {
    font-weight: 300;
    text-align: center;
}

.container {
    position: relative;
    margin: 0 auto;
}

button {
    background-color: #2cc185;
    color: #fff;
    border: 0;
    padding: 1em 1.5em;
}

button:hover {
    background-color: #239768;
    color: #fff;
}

button:focus {
    background-color: #239768;
    color: #fff;
}

.accordion {
    position: relative;
    background-color: #fff;
    display: inline-block;
    width: 100%;
    border-top: 1px solid #f1f4f3;
    border-bottom: 1px solid #f1f4f3;
    font-weight: 700;
    color: #74777b;
    vertical-align: middle;
}
/*Rotation-------------------------------------*/
.accordion .fa {
    position: relative;
    float: right;
}
.rotate {
    -moz-transition: all 0.3s linear;
    -webkit-transition: all 0.3s linear;
    transition: all 0.3s linear;
}

    .rotate.down {
        -moz-transform: rotate(90deg);
        -webkit-transform: rotate(90deg);
        transform: rotate(90deg);
    }
/*------------------------------------------*/
.link {

    text-align: right;
    margin-bottom: 20px;
    margin-right: 30px;
}

.accordion h4 {
    position: relative;
     /* top: 0.8em; */
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    float: left;
}

.accordion a {
    position: relative;
    display: block;
    color: #74777b;
    padding: 1em 1em 2.5em 1em;
    text-decoration: none;
}

.accordion a:hover {
    text-decoration: none;
    color: #2cc185;
    background-color: #e7ecea;
    transition: 0.3s;
}

.accordion-desc {
    background-color: #f1f4f3;
    color: #74777b;
    z-index: 2;
    padding: 20px 15px;
}
@media (min-width:480px) {

    .container { max-width: 80%; }
}
@media (min-width:768px) {

    .container { max-width: 1000px; }
}
.accordion-desc p {
    word-break: break-all;
}

.accordion .status {
    position: relative;
    float: right;
    right: 20%;
    vertical-align: middle;
}
.btn {
    margin-top: 10px;
}
.heading {
    margin:10px 0px 10px 0px;
    vertical-align: middle;
    display: inline-block;
    position: relative;
    width: 100%;
}

.heading h2 {
    float: left;
    position: relative;
    margin:auto;
    vertical-align: middle;
}
.heading .searcheBar {
    float: right;
    position: relative;
    margin: auto;
    vertical-align: middle;

}
.checkboxInput {
    float: right;
    position: relative;
    margin: auto;
    vertical-align: middle;
    right: 40%;
}
.semi {
    min-height: 50px;   

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<b>Type 1, 2 or 3 in the text box</b>

    <div class="heading">
        <input class="searcheBar form-control" id="searcheBar" type="text" />
    </div>
 
                <div class="accordion">
                    <a href="#">
                        <h4 id="title">1</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
                
                        <div class="accordion">
                    <a href="#">
                        <h4 id="title">2</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
                
                        <div class="accordion">
                    <a href="#">
                        <h4 id="title">3</h4>
                        <h4 class="status">@Resource.AccordionStatus</h4>
                        <i class="fa fa-chevron-right rotate"></i>
                    </a>
                </div>
                <div class="accordion-desc">
                    <h3>@Resource.AccordionProjectLead</h3>
                    <h4>Kay Wiberg</h4>
                    <h3>@Resource.AccordionDescription</h3>
                    <p>
                        @item.Description
                    <p>
                        <div class="link">
                            <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
                        </div>
                </div>
 

    <div>

    </div><!-- end of container -->

</div><!-- end of accordion-container -->

答案 1 :(得分:1)

我在你的代码中修改了一些东西。

  1. id 必须是唯一的。所以我在标题上使用class
  2. .trim()搜索输入是个好主意。
  3. 然后,在关键字上,我关闭所有的手风琴描述,然后打开匹配的。
  4. 另外,我使用:contains()选择器而不是每个循环。
  5. 最后,我将效果时间调整为10毫秒,整数为fast,即200毫秒,用于闭幕手风琴。
  6. 看一下该片段。

    $(document).ready(function(){
      var myJson = [];
      //Accordion-----------------------------------------------
      $(".accordion-desc").fadeOut(0);
      $(".accordion").click(function () {
        $(".accordion-desc").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
      });
    
    
      $(".accordion").click(function () {
        $(".accordion").not(this).find(".rotate").removeClass("down");
        $(this).find(".rotate").toggleClass("down");
      });
      //-----------------------------------------------------------
    
      //SearchFilter
      $("#searcheBar").on("keyup", function () {
    
        // Close all accordions.
        $(".accordion").fadeIn(400);
        $(".accordion-desc").slideUp(10);
    
    
        // If g isn't empty
        var g = $(this).val().toLowerCase().trim();
        if(g!=""){
          $(".title:contains("+g+")").each(function () {
            $(this).closest(".accordion").next(".accordion-desc").slideDown(400);
            $(".accordion").not($(this).closest(".accordion")).fadeOut(10);
            $(this).closest(".accordion").fadeIn(400);
          });
        }
      });
    });
    body {
      background-color: #eee;
      font-family: "Open Sans", sans-serif;
    }
    
    header {
      background-color: #2cc185;
      color: #fff;
      padding: 2em 1em;
      margin-bottom: 1.5em;
    }
    
    h1 {
      font-weight: 300;
      text-align: center;
    }
    
    .container {
      position: relative;
      margin: 0 auto;
    }
    
    button {
      background-color: #2cc185;
      color: #fff;
      border: 0;
      padding: 1em 1.5em;
    }
    
    button:hover {
      background-color: #239768;
      color: #fff;
    }
    
    button:focus {
      background-color: #239768;
      color: #fff;
    }
    
    .accordion {
      position: relative;
      background-color: #fff;
      display: inline-block;
      width: 100%;
      border-top: 1px solid #f1f4f3;
      border-bottom: 1px solid #f1f4f3;
      font-weight: 700;
      color: #74777b;
      vertical-align: middle;
    }
    /*Rotation-------------------------------------*/
    .accordion .fa {
      position: relative;
      float: right;
    }
    .rotate {
      -moz-transition: all 0.3s linear;
      -webkit-transition: all 0.3s linear;
      transition: all 0.3s linear;
    }
    
    .rotate.down {
      -moz-transform: rotate(90deg);
      -webkit-transform: rotate(90deg);
      transform: rotate(90deg);
    }
    /*------------------------------------------*/
    .link {
    
      text-align: right;
      margin-bottom: 20px;
      margin-right: 30px;
    }
    
    .accordion h4 {
      position: relative;
      /* top: 0.8em; */
      margin: 0;
      font-size: 14px;
      font-weight: 700;
      float: left;
    }
    
    .accordion a {
      position: relative;
      display: block;
      color: #74777b;
      padding: 1em 1em 2.5em 1em;
      text-decoration: none;
    }
    
    .accordion a:hover {
      text-decoration: none;
      color: #2cc185;
      background-color: #e7ecea;
      transition: 0.3s;
    }
    
    .accordion-desc {
      background-color: #f1f4f3;
      color: #74777b;
      z-index: 2;
      padding: 20px 15px;
    }
    @media (min-width:480px) {
    
      .container { max-width: 80%; }
    }
    @media (min-width:768px) {
    
      .container { max-width: 1000px; }
    }
    .accordion-desc p {
      word-break: break-all;
    }
    
    .accordion .status {
      position: relative;
      float: right;
      right: 20%;
      vertical-align: middle;
    }
    .btn {
      margin-top: 10px;
    }
    .heading {
      margin:10px 0px 10px 0px;
      vertical-align: middle;
      display: inline-block;
      position: relative;
      width: 100%;
    }
    
    .heading h2 {
      float: left;
      position: relative;
      margin:auto;
      vertical-align: middle;
    }
    .heading .searcheBar {
      float: right;
      position: relative;
      margin: auto;
      vertical-align: middle;
    
    }
    .checkboxInput {
      float: right;
      position: relative;
      margin: auto;
      vertical-align: middle;
      right: 40%;
    }
    .semi {
      min-height: 50px;   
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
      <b>Type 1, 2 or 3 in the text box</b>
    
      <div class="heading">
        <input class="searcheBar form-control" id="searcheBar" type="text" />
      </div>
    
      <div class="accordion">
        <a href="#">
          <h4 class="title">1</h4>
          <h4 class="status">@Resource.AccordionStatus</h4>
          <i class="fa fa-chevron-right rotate"></i>
        </a>
      </div>
      <div class="accordion-desc">
        <h3>@Resource.AccordionProjectLead</h3>
        <h4>Kay Wiberg-1</h4>
        <h3>@Resource.AccordionDescription</h3>
        <p>
          @item.Description
        <p>
        <div class="link">
          <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
        </div>
      </div>
    
      <div class="accordion">
        <a href="#">
          <h4 class="title">2</h4>
          <h4 class="status">@Resource.AccordionStatus</h4>
          <i class="fa fa-chevron-right rotate"></i>
        </a>
      </div>
      <div class="accordion-desc">
        <h3>@Resource.AccordionProjectLead</h3>
        <h4>Kay Wiberg-2</h4>
        <h3>@Resource.AccordionDescription</h3>
        <p>
          @item.Description
        <p>
        <div class="link">
          <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
        </div>
      </div>
    
      <div class="accordion">
        <a href="#">
          <h4 class="title">3</h4>
          <h4 class="status">@Resource.AccordionStatus</h4>
          <i class="fa fa-chevron-right rotate"></i>
        </a>
      </div>
      <div class="accordion-desc">
        <h3>@Resource.AccordionProjectLead</h3>
        <h4>Kay Wiberg-3</h4>
        <h3>@Resource.AccordionDescription</h3>
        <p>
          @item.Description
        <p>
        <div class="link">
          <a class="btn btn-success" href="@item.Url">@Resource.AccordionGoTo</a>
        </div>
      </div>
    
    
      <div>
    
      </div><!-- end of container -->
    
    </div><!-- end of accordion-container -->