18

クリックすると、div (.icons) を切り替えていくつかのアイコンを表示する Jquery Ui ボタン ( #toggleIcons) があります。また、Jquery Isotope と Infinitescroll を使用して新しい画像を動的に追加しています。私がやろうとしているのは、新しい画像が追加および更新されるときに、slideToggle の状態を保持する方法を用意することです。Ifinitescroll にはコールバック関数があるため、ページとアイコンの状態を更新できます。

//My button click function
$("#styleSwitcher #toggleIcons" ).click(function() {
   $('.icons').slideToggle('slow');
   //for Isotope to update the layout
   $('#container').isotope('reLayout') 
    return false;
});

//code I am working on to put in callback to test if div is open or closed
if($(".icons").is(":hidden"))
{
  $('.icons').hide();
}
else
{
  $('.icons').show();
}

動作していません。どんな助けや指示もいただければ幸いです。ありがとう

4

8 に答える 8

28

あなたの状態は逆です:

if ($(".icons").is(":visible")) { <-----
  $('.icons').hide(); 
} else {
  $('.icons').show(); 
}
于 2011-03-10T20:45:51.897 に答える
2

私は使うだろう:visible

if($(".icons:visible").length > 0)
    //item is visible
else
    //item is not visible

ただし、コードに固執したい場合

if($(".icons").is(":hidden"))

おそらく読むべき

if($(".icons").is(":hidden").length > 0)
于 2011-03-10T20:51:19.673 に答える
2

なぜそれをトグル またはスライドトグルしないのですか?

$(".icons").toggle();
于 2011-03-10T20:45:41.253 に答える
1

私は同じことをしていましたが、IDベースのセットアップの場合、代わりにこれを使用するとうまくいくことがわかりました

if ($(this).is(':hidden')) {
    var state = "closed";
} else {
    var state = "open";
}

alert($(this).attr('id') + ' is ' + state);
return false;           
于 2013-12-22T09:28:43.443 に答える
0

私はあなたが欲しいものを理解していると思います。画像の追加とは関係のないボタンから、ユーザーはアイコンを表示したり非表示にしたりできます。新しい画像が追加され、アイコンが「表示」されます。コールバックでは、アイコンを表示するか、ギャラリーの他の部分と一致するように非表示にするかがわかりません。

//My button click function
$("#styleSwitcher #toggleIcons" ).click(function() {
$('.icons').slideToggle('slow');
//for Isotope to update the layout
$('#container').isotope('reLayout').data({iconStateIsHidden:$('.icons').is(":hidden")}); 
 return false;
 });



 //code I am working on to put in callback to test if div is open or closed



       if($("#container").data()[iconStateIsHidden])
          {
// newly added images should match the rest of the gallery
        $('.icons').hide(); 
          }     
        else
          {
// newly added images should match the rest of the gallery
        $('.icons').show(); 
          } 
于 2011-03-10T20:57:04.970 に答える