2016-01-15 47 views
0

感谢您的关注第一, 我使用jquery这个目标`当我点击$('。icon')它必须做两件事先动画$(' .thediv')改为{top:of'90px and back 0px'},第二件事是改变$('。icon')类以获得V形图案并返回V形图案, 我不明白我做得对; 这里是小提琴jquery切换div动画和同一时间的类名更改

var fltro = function(){ 
 
\t var t = 200; 
 
\t $('.filter-tog .fa').click(function(){ 
 
\t \t $('.filter').animate({top: '70px'}, t); 
 
\t \t $('.filter-tog .fa').removeClass('fa-chevron-down').addClass('fa-chevron-up'); 
 
\t }); 
 
    $('.filter-tog .fa').click(function(){ 
 
\t \t $('.filter').animate({top: '20px'}, t); 
 
\t \t $('.filter-tog .fa').removeClass('fa-chevron-up').addClass('fa-chevron-down'); 
 
\t }); 
 
}; 
 
$(document).ready(fltro);
.header{ 
 
    height:50px; 
 
    width:300px; 
 
    background-color:red; 
 
} 
 
.filter{ 
 
\t width: 300px; 
 
    height: 40px; 
 
    position: absolute; 
 
    top: 20px; 
 
    background-color: rgb(16, 160, 122); 
 
    z-index: -4; 
 
} 
 
.filter-tog{ 
 
\t width: 20px; 
 
    height: 15px; 
 
    color: white; 
 
    background-color: rgb(27, 77, 88); 
 
    position: absolute; 
 
    top: 8px; 
 
    left: 308px; 
 
    z-index: 8; 
 
} 
 
.filter-tog i{ 
 
\t line-height: 15px; 
 
    font-size: 15px; 
 
    text-align: center; 
 
    color: white; 
 
    float: none; 
 
    margin: 0; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="header"> 
 
</div> 
 
<div class="filter-tog"> 
 
\t <i class="fa fa-chevron-down"></i> 
 
</div> 
 
<div class="filter"> 
 
</div>

+0

出去找的效果我想你忘了添加小提琴。 – Enjayy

+0

我在编辑之前标记过这篇文章 – Amir

回答

0

只需使用一个变量来保持的切换状态的状态轨迹:

var t = 200, st = 0; 
$('.filter-tog .fa').click(function() { 
    if (st==0){ 
     $('.filter').animate({ 
      top: '70px' 
     }, t); 
     $('.filter-tog .fa').switchClass('fa-chevron-down', 'fa-chevron-up'); 
     st++; 
    }else{ 
     $('.filter').animate({ 
      top: '20px' 
     }, t); 
     $('.filter-tog .fa').switchClass('fa-chevron-up', 'fa-chevron-down'); 
     st = 0; 
    } 
}); 

jsFiddle Demo

+0

但是我认为它只能用于第一次点击,或者不是? –

0

嘿提问者我花了一点时间来帮助和解释在重构中你有一点代码。这将有助于实现您与具有再添加库如jQuery UI

https://jsfiddle.net/kriscoulson/jocqqqb5/

jQuery的

$(document).ready(function(){ 
    var $button = $('.filter-tog'); // find the button for clicking 
    var $buttonIcon = $button.find('i'); // find the arrow for changing 
    var $filter = $('.filter'); // find the filter so we can animate 

    // Create a function to change the Icon 
    var changeArrow = function() { 
     $buttonIcon.toggleClass('fa-chevron-down').toggleClass('fa- chevron-up') 
    } 

    // Create a function to add animate class use css 
    var animate = function(){ 
     $filter.toggleClass('animate'); 
    } 

    // Anytime the button is clicked run the arrow and animate functions; 
    $button.on('click',function(){ 
     changeArrow(); 
     animate(); 
    }); 
}); 

CSS

.filter { 
    width: 300px; 
    height: 40px; 
    position: absolute; 
    top: 20px; 
    background-color: rgb(16, 160, 122); 
    z-index: -4; 
    /* Use the transition property so anytime the 
    top position is changed the animation will be applied */ 
    transition: top 1s; 
} 

.animate { 
    top: 90px; 
} 
+0

谢谢你的帮助,我明白了。非常感谢你。 –