7

画面の左側に隠されたパネルがあり、画面の左側にある「タブ」をクリックするとスライドして表示されます。パネルを既存のページコンテンツの上にスライドさせる必要があり、タブを一緒に移動する必要があります。したがって、両方ともcssに絶対的に配置されます。タブ(したがってタブコンテナ)が表示されたときにパネルと一緒に左に移動する必要があることを除いて、すべてが正常に機能しているため、パネルの右側に固定されているように見えます。フロートを使用する場合は比較的単純ですが、もちろんこれは既存のコンテンツのレイアウトに影響するため、絶対的な配置になります。パネルコンテナの左側の位置をアニメーション化しようとしましたが(文書化されたjquery関数を参照)、動作させることができません。

これは私が変更した元のコードの例ですが、ボタン/タブをスライドさせるにはどうすればよいですか?

http://www.iamkreative.co.uk/jquery/slideout_div.html

私のHTML

<div><!--sample page content-->
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 
    </p>
</div>

<div id="panel" class="height"> <!--the hidden panel -->
    <div class="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore</p>
    </div>  
</div>

<!--if javascript is disabled use this link-->
<div id="tab-container" class="height">
    <a href="#" onclick="return()">
        <div id="tab"><!-- this will activate the panel. --></div> 
    </a>
</div>

私のjQuery

$(document).ready(function(){

$("#panel, .content").hide(); //hides the panel and content from the user

$('#tab').toggle(function(){ //adding a toggle function to the #tab
    $('#panel').stop().animate({width:"400px", opacity:0.8}, 100, //sliding the #panel to 400px

    // THIS NEXT FUNCTION DOES NOT WORK -->
    function() {
        $('#tab-container').animate({left:"400px"} //400px to match the panel width
        });

    function() {
        $('.content').fadeIn('slow'); //slides the content into view.
        });  
},

function(){ //when the #tab is next cliked
    $('.content').fadeOut('slow', function() { //fade out the content 
        $('#panel').stop().animate({width:"0", opacity:0.1}, 500); //slide the #panel back to a width of 0
    });
   });

  });

これはcssです

#panel {
position:absolute;
left:0px;
top:50px;
background-color:#999999;
height:500px;
display:none;/*hide the panel if Javascript is not running*/
}

#panel .content {
width:290px;
margin-left:30px;
}

#tab-container{
position:absolute;
top:20px;
width:50px;
height:620px;
background:#161616;
}

#tab {
width:50px;
height:150px;
margin-top:100px;
display:block;
cursor:pointer;
background:#DDD;
}

どうもありがとう

4

2 に答える 2

10

私は最初からやり直し、実際にjQueryのドキュメントを読みました。パネルとボタンの両方を絶対位置のdivに配置し、両方を左に浮かせます。コンテナを左負の位置にしてから、ボタンにjQueryトグルアクションを配置します。

$('#button').toggle(function() {

    $('#slider').animate({
        left: '+=200'
        }, 458, 'swing', function() {

        // Animation complete. CALLBACK?

    });

}, function() {

    $('#slider').animate({
        left: '-=200'
        }, 458, 'swing', function() {

        // Animation complete. CALLBACK?

    });

});
于 2010-04-19T10:43:23.963 に答える
0

同じタスクを達成しようとしたときに、この投稿に出くわしました。

残念ながら、Matt の回答は最新バージョンの jQuery では機能しなくなります。Matt の回答が書かれた時点で、jQuery には 2 つの .toggle 関数がありました。私がこれを書いている時点で、彼の回答で使用されているものは廃止されました。Matt のコードを使用すると、div 全体、ボタンなどすべてが消えます。(そして、あなたが私のような人なら、APIのドキュメントを掘り下げて変更について知るまで、非常に混乱するでしょう)

次のコードで機能を動作させることができました。

HTML:

<div id="siteMap">
 <div id="mapButton">Site Map</div>
 <div id="theMap">[The Site Map Goes Here]</div>
</div>

CSS (これはまだきれいではありません):

#siteMap {
  width:500px;
  position:fixed;
  left:-500px;
  top:157px;
  display:block;
  color:#FFF;
  z-index:2;
  opacity: 0.95;
}
#siteMap #mapButton {
  display:block;
  color:#333;
  background-color:#ACACAC;
  padding:2px 5px;
  height:20px;
  width:70px;
  text-align:center;
  position:relative;
  left: 100%;
  margin-top:80px;
  cursor:pointer;

  transform-origin:     0% 0%;
  -ms-transform-origin:     0% 0%;
  -webkit-transform-origin: 0% 0%;
  -moz-transform-origin:        0% 0%;
  -o-transform-origin:      0% 0%;

  transform:           rotate(-90deg);
  -ms-transform:     rotate(-90deg); 
  -webkit-transform: rotate(-90deg); 
  -moz-transform:    rotate(-90deg); 
  -o-transform:      rotate(-90deg);
}
#siteMap #theMap {
  width:100%;
  height:350px;
  background-color:#666;
  margin-top:-104px;
}

そして最後に JavaScript:

<script type='text/javascript'>
  $(document).ready(function() {
    $('#mapButton').click(function() {
      var mapPos = parseInt($('#siteMap').css('left'), 10);
      if (mapPos < 0) {
        $('#siteMap').animate({
          left: '+=500'
          }, 458, 'swing', function() {
            // Animation complete.
          });
      }
      else {
        $('#siteMap').animate({
          left: '-=500'
          }, 458, 'swing', function() {
            // Animation complete.
        });
      } 
    });
  });
</script>

Google からここに来た場合、私の投稿がお役に立てば幸いです :)

于 2013-01-23T04:35:14.493 に答える