显示/隐藏Div也是固定的

时间:2017-05-03 17:24:55

标签: javascript php css show-hide

我来回来回,我确信有一个简单的答案,但这开始让我感到沮丧。

我需要的是一个可以在单击按钮时切换的div框。这是一个始终保持在底部角落的聊天脚本。我希望按钮始终可见,然后如果单击该按钮,聊天脚本将打开。然后,您可以通过再次单击按钮来最小化它。我玩了很多次,但我需要2个div(因为第一个div将包含始终可见的按钮,第二个div将在点击时出现)。

更新:上面的当前脚本打开并关闭div没有问题。我只是不能在滚动时跟随页面的两个div,尽管它们是“position:fixed;”

$(function() {
  $('.button1').on('click', function() {
    $('.initiallyHidden').toggle();
  });
});
#calendar {
  position: fixed;
  bottom: 0;
  right: 0;
  float: right;
}


}
#initiallyHidden {
  position: fixed;
  bottom: 0;
  right: 0;
  float: right;
  width: 100px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="calendar">
  <button class="button1">Quick Chat</button>
</div>

<div class="initiallyHidden" style="display: none;">
  <iframe src="'.$url.'" marginheight="0" marginwidth="0" scrolling="yes" allowtransparency="yes" frameborder="0">
</div>

上面的当前脚本打开并关闭div没有问题。尽管它们是position:fixed;,但我不能在滚动时跟随页面两个div。

请帮帮我。

4 个答案:

答案 0 :(得分:0)

您的CSS选择器用于ID,但您的HTML元素具有类而不是ID。

答案 1 :(得分:0)

在您的CSS中,您有.initiallyHidden而不是#calendar。 日历相同:.calendar代替}

您的CSS中还有一个额外的float:right

将{{1}}属性移除到CSS

答案 2 :(得分:0)

$(window).on("load", function() {
   $("#initiallyHidden").hide();
});
$( "#calendar" ).click(function() {
  if($("#initiallyHidden").is(":visible")){
     $("#initiallyHidden").hide();
  }
  else{
    $("#initiallyHidden").show();
  }
});
#calendar { position:fixed;
  position: fixed;
  bottom: 0;
  right: 0;
  float: right;
}
}
#initiallyHidden { 
position:fixed;
 bottom:0;
 right:0;
float:right;
width:100px;
height:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id = "calendar" type="button">Show calender!</button> 
<div id = "initiallyHidden">
Calendr
</div>

答案 3 :(得分:0)

$("#button1").on("click", ()=>{ 
	$("#initiallyHidden").toggle();
});
#chatbox { 
  position:fixed;
  bottom:0;
  right:0;
  float:right;
}
#initiallyHidden { 
  bottom:10;
  right:0;
  float:right;
  width:100px;
  height:100px;
  border-style: solid;
  background-color:white;
}
#content {
  width:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chatbox">
<div id="initiallyHidden" style="display: none;">
    Chat box
</div>
<button id="button1">Quick Chat</button>
</div>
<div id="content">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</div>