2012-02-23 37 views
7

Đây là mã tôi đang sử dụng để ẩn và hiển thị div bằng hiệu ứng chuyển đổi trang trình bày.Cách chuyển đổi mũi tên thả xuống

jQuery(document).ready(function() { 
    jQuery(".option-content").hide(); 
    jQuery(".option-heading").click(function() 
     { 
      jQuery(this).next(".option-content").slideToggle(500); 
     }); 
    }); 

Tuy nhiên, tôi muốn thêm một số mũi tên toggled để cho thấy div được bật lên hoặc xuống.

Đây là những gì tôi có cho đến nay và nó nửa của những gì tôi muốn nó phải làm:

jQuery(document).ready(function() { 
    jQuery(".option-content").hide(); 
    jQuery("#arrow-up").hide(); 
    jQuery(".option-heading").click(function() 
     { 
      jQuery(this).next(".option-content").slideToggle(500); 
      jQuery("#arrow-up").toggle(); 
      jQuery("#arrow-down").toggle(); 
     }); 
}); 

này Toggles vào mũi tên nhưng có hai vấn đề:

  1. Mũi tên xuống vẫn hiển thị
  2. Mọi div đều chuyển đổi mỗi mũi tên để khi một số div tăng lên và một xuống, tất cả chúng đều hiển thị xuống.

Bất kỳ ý tưởng sẽ được appriciated

Trả lời

14

jQuery(function($) { 
 

 
    $(".option-heading").click(function() { 
 
    $(this).next(".option-content").stop().slideToggle(500); 
 
    $(this).find(".arrow-up, .arrow-down").toggle(); 
 
    }); 
 

 
});
.option-heading{background:#ddd; cursor:pointer;} 
 
.option-content{background:#eee;} 
 

 
/* Notice this class ! */ 
 
.is-hidden{ display: none; }
<div class="option-heading"> 
 
    <span class="arrow-up is-hidden">&#9650;</span> 
 
    <span class="arrow-down">&#9660;</span> 
 
    HEADING 
 
</div> 
 
<div class="option-content is-hidden"> 
 
    content<br>content<br>content<br>content<br>content<br>content<br>content 
 
</div> 
 

 
<script src="//code.jquery.com/jquery-3.1.0.js"></script>

2

Sử dụng các lớp học!

jQuery(document).ready(function() { 
    jQuery(".option-content").hide().removeClass("shown").addClass("hidden"); 
    jQuery(".option-heading").click(function() 
     { 
      jQuery(this).next(".option-content").slideToggle(500) 
        .removeClass("hidden").addClass("shown"); 
     }); 
}); 

Sau đó sử dụng CSS:

.option-content.hidden{ background-image:url(hidden.png); } 
.option-content.shown{ background-image:url(shown.png); } 
Các vấn đề liên quan