屏幕左侧有一个隐藏的面板,当屏幕左侧的“标签”点击时,该面板会滑入视图。我需要面板滑过现有页面内容的顶部,我需要选项卡随之移动。所以两者都绝对定位于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;
}
非常感谢
答案 0 :(得分:10)
我从头开始,实际上阅读了jQuery docs hehe。我将面板和按钮放在一个绝对定位的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?
});
});
答案 1 :(得分:0)
在尝试完成同样的任务时,我偶然发现了这篇文章。
不幸的是,Matt的答案将不再适用于最新版本的jQuery。在撰写Matt的答案时,jQuery有两个.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来到这里,我的帖子会有所帮助:)