首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切换标题栏

问切换标题栏
EN

Stack Overflow用户
提问于 2014-02-08 21:30:25
回答 3查看 1.4K关注 0票数 0

我的网站有问题,我想做切换完全像这个例子:

代码语言:javascript
复制
http://www.templatemonster.com/demo/36298.html

我有顶部栏(宽度50px)包括箭头切换和在页面的其余部分,我有iframe的网站。

所有我想做的就是切换动画效果-将顶部栏从绝对顶部0移动到绝对顶部50以隐藏,同时我想将iframe高度从100%-50px更改为100%。

我做了这个脚本,它可以完美地隐藏顶部栏,但不会再次将其显示为默认值。

http://jsfiddle.net/2WMtX/

你能帮帮我吗?谢谢

EN

回答 3

Stack Overflow用户

发布于 2014-02-08 21:42:20

你有没有用click代替live?live适用于您定义事件侦听器时不存在的元素,我猜您的.show-bar元素一开始就不存在。试试这个:

代码语言:javascript
复制
$('#demo').height($(window).height()-50); // default iframe height

$(".hide-bar").live('click',function(){ // hide top bar
  $("#header-bar").animate({"top":"-50px"},300);
  $("#demo").animate({"height":$(window).height()-0},300);
  $(this).removeClass();
  $(this).addClass("show-bar");
});

$(".show-bar").live('click',function(){ // show top bar
  $("#header-bar").animate({"top":"0px"},300);
  $("#demo").animate({"height":$(window).height()-50},300);
  $(this).removeClass();
  $(this).addClass("hide-bar");
}); 

注意:这将适用于jQueryv1.7(在1.9方法中删除了live )

以下是较新版本的jquery的相同代码:

代码语言:javascript
复制
    $('#demo').height($(window).height()-50);

    $(document).on('click',".hide-bar",function(){
      $("#header-bar").animate({"top":"-50px"},300);
      $("#demo").animate({"height":$(window).height()},300);
      $(this).removeClass();
      $(this).addClass("show-bar");
    });

    $(document).on('click',".show-bar", function(){
      $("#header-bar").animate({"top":"0px"},300);
      $("#demo").animate({"height":$(window).height()-50},300);
      $(this).removeClass();
      $(this).addClass("hide-bar");
    });
票数 0
EN

Stack Overflow用户

发布于 2014-02-08 21:55:53

我希望下面的解决方案能帮助你。

css代码:

代码语言:javascript
复制
#header-bar {position:fixed; top:0; left:0; width:200px; background-color:grey; height:50px;}
#close-bar { position:absolute; bottom:-20px; background:red; color:white;}

超文本标记语言代码:

代码语言:javascript
复制
<section id="header-bar" class="txt-highlight-color bg-color bg-pattern">
  <span id="close-bar" class="hide-bar">close button</span>
</section>

jQuery代码:

代码语言:javascript
复制
var speed = 300;
        $('#close-bar').on('click', function(){

            var $$ = $(this);

            if( $$.is('.hide-bar') ){
                $('#header-bar').animate({top:-50}, speed);
                $$.removeClass('hide-bar')
            } else {
                $('#header-bar').animate({top:0}, speed);
                $$.addClass('hide-bar')
            }

        });

示例代码:http://jsfiddle.net/Q3yR8/

票数 0
EN

Stack Overflow用户

发布于 2014-02-09 23:51:13

好了,伙计们,我已经知道了,这是我的最终代码:

代码语言:javascript
复制
$(function() {
  $('#demo').height($(window).height()-$("#header-bar").height());
  var speed = 300;
  $(document).on('click',".hide-bar",function(){
    $("#header-bar").animate({"top": - $("#header-bar").height()},speed);
    $("#demo").animate({"height":$(window).height()},speed);
    $(this).removeClass();
    $(this).addClass("show-bar");
  });

  $(document).on('click',".show-bar", function(){
    $("#header-bar").animate({"top":"0px"},speed);
    $("#demo").animate({"height":$(window).height()-$("#header-bar").height()},speed);
    $(this).removeClass();
    $(this).addClass("hide-bar");
  });
});

有了这个,我只能在CSSfile中设置#header-bar宽度,它会改变所有的宽度

谢谢你的帮忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21646587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档