2013-03-16 58 views
1

我正在编写一个简单的脚本,在单击按钮时向上/向下滑动工具栏div (position: fixed)。但是,当我点击按钮时没有任何反应,甚至没有发生,即使是alert向上/向下滑动div - 此代码有什么问题?

脚本的动画部分似乎出了问题,好像我将动画部分取出然后警报将会播放。

JS:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#hidebutton").click(function() { 
      alert('hide clicked'); 
      $("#toolbarcontainer").animate({ 
       bottom: '-50px' //slide toolbar down so it's hidden 
      }, 500); 
      $(".footer").animate({ 
       margin-bottom: '10px' //slide footer back 
      }, 500); 
      $("#hidebutton").fadeOut(); 
      $("#showbutton").fadeIn(); 
     }); 
     $("#showbutton").click(function() { 
      alert('show clicked'); 
      $("#toolbarcontainer").animate({ 
       bottom: '0' //slide toolbar back up 
      }, 500); 
      $(".footer").animate({ 
       margin-bottom: '90px' //slide footer up again 
      }, 500); 
      $("#showbutton").fadeOut(); 
      $("#hidebutton").fadeIn(); 
     }); 

    }); 
</script> 

CSS:

#toolbarcontainer { 
position:fixed; 
width:100%; 
height:80px; 
bottom:0; 
background-color: rgba(255,255,255,0.7); 
filter:alpha(opacity = 80); 
} 
#showbutton,#hidebutton { 
position:absolute; 
right:25px; 
top:5px; 
height:10px; 
width:10px; 
cursor:pointer; 
} 
#showbutton { 
display:none; //hide until toggled 
} 

HTML:

<div id="toolbarcontainer"> 
    //contents of toolbar 
    <div id="showbutton"><img src="../../site/pictures/showbutton.png"></div> 
    <div id="hidebutton"><img src="../../site/pictures/hidebutton.png"></div> 
</div> 
+0

你可以把它张贴在jsfiddle上吗? – kirugan 2013-03-16 04:35:13

回答

4

添加引号margin-bottom'margin-bottom'

$(document).ready(function() { 
    $("#hidebutton").click(function() { 
     alert('hide clicked'); 
     $("#toolbarcontainer").animate({ 
      bottom: '-50px' //slide toolbar down so it's hidden 
     }, 500); 
     $(".footer").animate({ 
      'margin-bottom': '10px' 
     }, 500); 
     $("#hidebutton").fadeOut(); 
     $("#showbutton").fadeIn(); 
    }); 
    $("#showbutton").click(function() { 
     alert('show clicked'); 
     $("#toolbarcontainer").animate({ 
      bottom: '0' 
     }, 500); 
     $(".footer").animate({ 
      'margin-bottom': '90px' //slide footer up again 
     }, 500); 
     $("#showbutton").fadeOut(); 
     $("#hidebutton").fadeIn(); 
    }); 

}); 
+0

谢谢你的工作。为什么边际需要围绕它进行报价? – Windbrand 2013-03-16 05:05:40

+2

它需要一个引号,因为它是作为参数传递的对象的属性。不带引号的短划线'-'不能正确解析。我认为这可能更准确,但这是我的头顶回答。 (Dash通常用作[减法运算符](http://msdn.microsoft.com/en-us/library/ie/9ty8kw3w(v = vs.94).aspx),因此需要将其定义为字符串文字)。 – 2013-03-16 05:08:11