javascript - 删除 div 部分下方的空白

标签 javascript jquery html css

我有一个包含两张图片的 div,当点击每张图片时,它们会显示一些文本。

$('span.a, span.b').click(function() {
  if (!$(this).hasClass('active')) {
    $('span.a, span.b').removeClass('active');
    $(this).addClass('active');
    $('div.a, div.b').toggle();
  }
  $('div.a, div.b').css("visibility","visible")
});
div.a,
div.b {
  visibility:hidden;
}

.footer {
  font-family:'Arial';
  font-size:13px;
  background-color:#000;
  color:#fff;
  text-align:center;
  padding:20px;
}
<div style="background-color:#fff;"><center><span class="a active"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/10/Customers.jpg" width="200"> </span>
  <span class="b"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/12/Landlords.jpg" width="200"></span>
  <div class="a">Cornerstone Parking provides value for money parking in Brisbane’s CBD and surrounding suburbs. Our turn up and park rates (ie no need to pre-book) are often cheaper than other car park’s online discount rates and you can always be sure of getting a
    bay in a Cornerstone car park. Our convenient and centrally located CBD car parks are run by our friendly staff and are predominantly located in the Adelaide Street, Ann Street and Creek Street areas. Our car parks offer discounted parking in large
    bays with ample height clearance. We offer hourly (visitor) parking as well as monthly parking, early bird parking and motorbike parking in most of our car parks.</div>
  <div class="b" style="display:none">Cornerstone Parking provides a high quality, professional and technology driven car park management service. A part of the Cornerstone Group, our property development and management heritage provides us with a true appreciation of landlord issues. Our
    parent company backing means that Cornerstone Parking has the appetite and ability to participate in larger parking projects, including the development of new car parks. We provide owners, investors and developers with our car park management, advisory
    and consultation services.</div>
</center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script></div>
<div class="footer">Footer content
</div>

我有两个问题:我需要将第一个 div 中两个图像下方的大空白区域最小化 - 这可以做到吗?

另外,当再次点击图像时,Javascript 代码是否可以再次隐藏文本?

谢谢!

最佳答案

额外的空间来自于使用 visibility: hidden; 这将保留页面上隐藏内容的布局,但在视觉上将其隐藏(因此它是不可见的)。使用 display: none; 而不是从页面中删除内容的布局,就好像它从未存在过一样。

然后您可以简化您的 jQuery,只需切换一个类来指定事件/非事件图像,然后在 CSS 中使用同级选择器来显示事件内容。

$('span.a, span.b').click(function() {
  $(this).siblings().removeClass('active');
  $(this).toggleClass('active');
});
div.a,
div.b {
  display: none;
}

.a.active ~ .a, .b.active ~ .b {
  display: block;
}

.footer {
  font-family:'Arial';
  font-size:13px;
  background-color:#000;
  color:#fff;
  text-align:center;
  padding:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="background-color:#fff;">
  <center><span class="a active"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/10/Customers.jpg" width="200"> </span>
    <span class="b"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/12/Landlords.jpg" width="200"></span>
    <div class="a">Cornerstone Parking provides value for money parking in Brisbane’s CBD and surrounding suburbs. Our turn up and park rates (ie no need to pre-book) are often cheaper than other car park’s online discount rates and you can always be sure of getting
      a bay in a Cornerstone car park. Our convenient and centrally located CBD car parks are run by our friendly staff and are predominantly located in the Adelaide Street, Ann Street and Creek Street areas. Our car parks offer discounted parking in
      large bays with ample height clearance. We offer hourly (visitor) parking as well as monthly parking, early bird parking and motorbike parking in most of our car parks.</div>
    <div class="b">Cornerstone Parking provides a high quality, professional and technology driven car park management service. A part of the Cornerstone Group, our property development and management heritage provides us with a true appreciation of landlord issues.
      Our parent company backing means that Cornerstone Parking has the appetite and ability to participate in larger parking projects, including the development of new car parks. We provide owners, investors and developers with our car park management,
      advisory and consultation services.</div>
  </center>
</div>
<div class="footer">Footer content
</div>

关于javascript - 删除 div 部分下方的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102243/

相关文章:

javascript - 什么是暂时死区?

javascript - 想要将 IF 语句与 JSON 一起使用

jQuery,在就绪时隐藏元素总是在页面加载时跳转。如何避免这种情况?

html - Bootstrap 表单输入框未对齐

javascript - 将函数绑定(bind)到按钮数组元素的事件 "onclick"

javascript - 在 Django 模板中使用通过 npm 安装的 JavaScript 库

javascript - 无法使用 angularjs 将 json 数据绑定(bind)到表

javascript - 试图为简单的图片库找到更好的方法

java - 如何在另一个 html 标签内选择带有 html 源的 web 元素 selenium web 驱动程序

javascript - 使用bumpbox、mootools.js实现pdf lightbox并确保用户滚动到文档末尾